gpt4 book ai didi

elasticsearch - 查询被索引词有空格的Elastic Search

转载 作者:行者123 更新时间:2023-12-03 01:04:53 25 4
gpt4 key购买 nike

我最近尝试使用 flex 搜索。但是,我正在努力查询以下情况:
我为此设置了索引:

"analysis": {
"index_analyzer": {
"my_index_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["standard", "lowercase", "nGram"],
"char-filter": ["my_pattern"]
}
},
"search_analyzer": {
"my_search_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["standard", "lowercase", "nGram"],
"char-filter": ["my_pattern"]
}
},
"filter": {
"nGram": {
"type": "nGram",
"min_gram": 3,
"max_gram": 40
}
},
"char_filter" : {
"my_pattern":{
"type":"pattern_replace",
"pattern":"\u0020",
"replacement":""
}
}

被索引的文档是:
{
name:'My self'
},
{
name:'Hell o'
}

如果我搜索“我自己”,则期望它返回第一个JSON对象,但是这没有发生。

我正在使用此搜索(其中术语只是被搜索的字符串):
var query = {
match: {
location: term

}
};
client.search({
index: 'requests',
analyzer:'my_search_analyzer',
body: {
query:query
}
})

我真的很感谢一些指导!

亲切的问候
JB

最佳答案

您快到了,您的索引定义只有一些小问题和错别字,我们将解决:

  • 您不需要index_analyzersearch_analyzer,只需直接在my_index_analyzer元素下定义my_search_analyzeranalyzer即可。
  • char-filter应该读取char_filter(带下划线)
  • 您的空间模式需要额外的反斜线

  • 这是我使用的更正的设置/映射:
    {
    "settings": {
    "analysis": {
    "analyzer": {
    "my_index_analyzer": { <--- 1. directly under analyzer
    "type": "custom",
    "tokenizer": "standard",
    "filter": [
    "standard",
    "lowercase",
    "nGram"
    ],
    "char_filter": [ <--- 2. underscore
    "my_pattern"
    ]
    },
    "my_search_analyzer": { <--- 1. directly under analyzer
    "type": "custom",
    "tokenizer": "standard",
    "filter": [
    "standard",
    "lowercase",
    "nGram"
    ],
    "char_filter": [ <--- 2. underscore
    "my_pattern"
    ]
    }
    },
    "filter": {
    "nGram": {
    "type": "nGram",
    "min_gram": 3,
    "max_gram": 40
    }
    },
    "char_filter": {
    "my_pattern": {
    "type": "pattern_replace",
    "pattern": "\\u0020", <--- 3. additional backslash
    "replacement": ""
    }
    }
    }
    },
    "mappings": {
    "request": {
    "properties": {
    "location": {
    "type": "string",
    "index_analyzer": "my_index_analyzer"
    }
    }
    }
    }
    }

    然后,您可以为两个样本文档建立索引:
    curl -XPUT localhost:9200/requests/request/1 -d '{"location":"My self"}'
    curl -XPUT localhost:9200/requests/request/2 -d '{"location":"Hell o"}'

    您将得到您所期望的:
    curl -XPOST localhost:9200/requests/request/_search -d '{
    "query": {
    "match": {
    "location": "Myself"
    }
    }
    }'

    将返回带有 My self的文档

    关于elasticsearch - 查询被索引词有空格的Elastic Search,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32017746/

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