gpt4 book ai didi

elasticsearch - Elasticsearch 5.6 multi_match搜索,edge_ngram返回零结果

转载 作者:行者123 更新时间:2023-12-03 02:13:16 25 4
gpt4 key购买 nike

我在Elasticsearch中有如下文件:

{
"_index" : "demo_index",
"_type" : "doc",
"_id" : "user_122",
"_version" : 4,
"found" : true,
"_source" : {
"id" : 1520488,
"doc_type" : "user",
"user_email" : "neeraj@test.com",
"user_first_name" : "Neeraj",
"user_last_name" : "Goel"
}
}
接下来是返回零结果的查询。
注意:仅在将全名分为索引中的user_first_name和user_last_name字段时,才出现这种情况。
{
"index": "demo_index",
"type": "doc",
"body": {
"from": 0,
"size": "200",
"query": {
"bool": {
"must": [
{
"term": {
"doc_type": "user"
}
},
{
"term": {
"user_status": 1
}
},
{
"multi_match": {
"query": "neeraj goel",
"operator": "AND",
"fuzziness": "AUTO",
"fields": [
"user_first_name.edge_ngram",
"user_last_name.edge_ngram"
]
}
}
],
"should": {
"prefix": {
"user_first_name.sort": "neeraj goel"
}
}
}
},
"sort": {
"_score": {
"order": "desc"
}
}
}
}
索引中使用的edge_ngram设置为:
{
"edge_ngram_tokenizer": {
"token_chars": [
"letter"
],
"min_gram": "2",
"type": "edge_ngram",
"max_gram": "8"
}
}
对应:
{
"user_first_name": {
"type": "text",
"fields": {
"edge_ngram": {
"type": "text",
"analyzer": "edge_ngram_analyzer"
},
"ngram": {
"type": "text",
"analyzer": "ngram_analyzer"
},
"raw": {
"type": "keyword"
}
},
"analyzer": "standard"
}
}
我无法弄清楚我的查询出了什么问题,因为如上所述,当文档中没有user_last_name时,查询工作正常。
谁能帮我解决这个问题。
谢谢。

最佳答案

由于您的索引设置和映射还不完整,并且不清楚要实现的目标,因此我添加了一个示例,该示例在您的用例附近有效并且可以理解,一旦添加更多数据,该示例便会更改。
索引定义以及映射和设置

{
"settings": {
"analysis": {
"analyzer": {
"edge_ngram_analyzer": {
"tokenizer": "edge_ngram_tokenizer"
},
"ngram_analyzer": {
"tokenizer": "ngram_tokenizer"
}
},
"tokenizer": {
"edge_ngram_tokenizer": {
"token_chars": [
"letter"
],
"min_gram": "2",
"type": "edge_ngram",
"max_gram": "8"
},
"ngram_tokenizer": {
"token_chars": [
"letter"
],
"min_gram": "2",
"type": "ngram",
"max_gram": "8"
}
}
},
"index.max_ngram_diff": 10 // note this
},
"mappings": {
"properties": {
"user_first_name": {
"type": "text",
"fields": {
"edge_ngram": {
"type": "text",
"analyzer": "edge_ngram_analyzer"
},
"ngram": {
"type": "text",
"analyzer": "ngram_analyzer"
},
"raw": {
"type": "keyword"
}
},
"analyzer": "standard"
},
"user_last_name": {
"type": "text",
"fields": {
"edge_ngram": {
"type": "text",
"analyzer": "edge_ngram_analyzer"
},
"ngram": {
"type": "text",
"analyzer": "ngram_analyzer"
},
"raw": {
"type": "keyword"
}
},
"analyzer": "standard"
}
}
}
}
并将示例文档添加到索引
{
"user_first_name" : "Neeraj",
"user_last_name" : "Goel"
}
并搜索查询
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "neeraj goel",
"operator": "or", // note this
"fuzziness": "AUTO",
"fields": [
"user_first_name.edge_ngram",
"user_last_name.edge_ngram"
]
}
}
],
"should": {
"prefix": {
"user_first_name.sort": "neeraj goel"
}
}
}
}
}
和搜索结果
"hits": [
{
"_index": "myindexedge",
"_type": "_doc",
"_id": "1",
"_score": 1.0500396,
"_source": {
"user_first_name": "Neeraj",
"user_last_name": "Goel"
}
}
]

关于elasticsearch - Elasticsearch 5.6 multi_match搜索,edge_ngram返回零结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64156800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com