【发布时间】:2020-08-21 06:48:47
【问题描述】:
我有一个要求,用户将输入一些字符并期望获得类似于 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"
}
}
这是该字段的映射方式:
"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
]
}
【问题讨论】:
-
您或许应该展示您迄今为止所尝试的内容,并提供一个案例来重现您所看到的内容。
-
嗨@Val,如果我搜索
0004会在结果中得到C-200820-01。我的 min_gram 为 3,max_gram 为 10。 -
请显示您的设置(索引设置、映射、分析器等)。具体代码值一千字
-
嗨@Val,我已经更新了
-
我无法使用上述信息重现您的案例。搜索
0004不会产生C-200820-01
标签: elasticsearch full-text-search n-gram