elasticsearch-使用n-gram搜索通配符

gab6jxml  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(409)

我有一个要求,用户需要输入一些字符,并期望得到类似sql的查询结果。我使用n-gram是因为我看到很多人建议避免使用通配符搜索。然而,返回数据有时是非常不相关的,因为它包含文本中的字符,但混淆了。我加了分数,但没用。有人有什么建议吗?谢谢。
更新
以下是索引设置:

"settings": {
    "index": {
        "lifecycle": {
            "name": "audit_log_policy",
            "rollover_alias": "audit-log-alias-test"
        },
        "analysis": {
            "analyzer": {
                "abi_analyzer": {
                    "tokenizer": "n_gram_tokenizer"
                }
            },
            "tokenizer": {
                "n_gram_tokenizer": {
                    "token_chars": [
                        "letter",
                        "digit"
                    ],
                    "min_gram": "3",
                    "type": "ngram",
                    "max_gram": "10"
                }
            }
        },
        "number_of_shards": "1",
        "number_of_replicas": "1",
        "max_ngram_diff": "10",
        "max_result_window": "100000"
    }
}

下面是字段的Map方式:

"resourceCode": {
    "type": "text",
    "fields": {
        "ngram": {
            "analyzer": "abi_analyzer",
            "type": "text"
        },
        "keyword": {
            "ignore_above": 256,
            "type": "keyword"
        }
    }
},
"logDetail": {
    "type": "text",
    "fields": {
        "ngram": {
            "analyzer": "abi_analyzer",
            "type": "text"
        },
        "keyword": {
            "ignore_above": 8191,
            "type": "keyword"
        }
    }
}

下面是我的提问方式:

query_string: {
    fields: ["logDetail.ngram", "resourceCode.ngram"],
    query: data.searchInput.toLowerCase(),
}

样品
以下是示例查询:

{
    "query": {
        "bool": {
            "must": [
                {
                    "terms": {
                        "organizationIds": [
                            ...
                        ]
                    }
                },
                {
                    "range": {
                        "createdAt": {
                            "gte": "2020-08-11T17:00:00.000Z",
                            "lte": "2020-08-31T16:59:59.999Z"
                        }
                    }
                },
                {
                    "multi_match": {
                        "fields": [
                            "logDetail.ngram",
                            "resourceCode.ngram"
                        ],
                        "query": "0004"
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "createdAt": "desc"
        }
    ],
    "track_scores": true,
    "size": 20,
    "from": 0
}

这是一个不相关的分数

{
    "_index": "user-interaction-audit-log-test-000001",
    "_type": "_doc",
    "_id": "ae325b4a6b45442cbf8a44d595e9a747",
    "_score": 3.4112902,
    "_source": {
        "logOperation": "UPDATE",
        "resource": "CUSTOMER",
        "resourceCode": "Htest11211",
        "logDetail": "<div>Updated Mobile Number from <var isolate><b>+84966123451000<\/b><\/var> to <var isolate><b>+849<\/b><\/var><\/div>",
        "organizationIds": [
            "5e72ea0e4019f01fad0d91c9",
        ],
        "createdAt": "2020-08-20T08:13:36.026Z",
        "username": "test_user",
        "module": "PARTNER",
        "component": "WEB_APP"
    },
    "sort": [
        1597911216026
    ]
}
qij5mzcb

qij5mzcb1#

问题是您没有指定任何搜索分析器。所以你的搜索输入也会被 abi_analyzer 以及 0004 被标记为 000 以及 004 . 前一个标记,即。 000 匹配一个 logDetail.ngram 现场。
您需要做的是指定
standard search_analyzer 对于Map中的两个字段,您不必分析搜索输入,只需尝试将其与相同的索引标记匹配:

"resourceCode": {
    "type": "text",
    "fields": {
        "ngram": {
            "analyzer": "abi_analyzer",
            "search_analyzer": "standard",           <--- here
            "type": "text"
        },
        "keyword": {
            "ignore_above": 256,
            "type": "keyword"
        }
    }
},
"logDetail": {
    "type": "text",
    "fields": {
        "ngram": {
            "analyzer": "abi_analyzer",
            "search_analyzer": "standard",           <--- here
            "type": "text"
        },
        "keyword": {
            "ignore_above": 8191,
            "type": "keyword"
        }
    }
}

如果因为不想重新索引数据而不想更改Map,也可以在查询时指定搜索分析器:

{
                "multi_match": {
                    "fields": [
                        "logDetail.ngram",
                        "resourceCode.ngram"
                    ],
                    "analyzer": "standard",          <--- here
                    "query": "0004"
                }
            }

更新:

"analyzer": {
            "abi_analyzer": {
                "tokenizer": "n_gram_tokenizer"
                "filter": ["lowercase"]
            }
        },

相关问题