gpt4 book ai didi

elasticsearch - 弹性短语前缀工作短语isnt

转载 作者:行者123 更新时间:2023-12-02 23:08:53 24 4
gpt4 key购买 nike

我正在尝试返回所有在userName和documentName中包含字符串的文档。

数据:

{
"userName" : "johnwick",
"documentName": "john",
"office":{
"name":"my_office"
}
},
{
"userName" : "johnsnow",
"documentName": "snowy",
"office": {
"name":"Abraham deVilliers"
}
},
{
"userName" : "johnnybravo",
"documentName": "bravo",
"office": {
"name":"blabla"
}
},
{
"userName" : "moana",
"documentName": "disney",
"office": {
"name":"deVilliers"
}
},
{
"userName" : "stark",
"documentName": "marvel",
"office": {
"name":"blabla"
}
}

我可以使用以下方式执行完全匹配的字符串:
}   
_source": [ "userName", "documentName"],
"query": {
"multi_match": {
"query": "johnsnow",
"fields": [ "userName", "documentName"]
}
}
}

这将成功返回:
{
"userName" : "johnsnow",
"documentName": "snowy",
"office": {
"name":"Abraham deVilliers"
}
}

如果我将 type: phrase_fixjohn一起使用,我也会成功返回3个结果。

但是然后我尝试:
{   
"query": {
"multi_match": {
"query": "ohn", // <---- match all docs that contain 'ohn'
"type": "phrase_prefix"
"fields": [ "userName", "documentName"]
}
}
}

返回零结果。

最佳答案

您正在寻找的是中缀搜索,您需要将ngram tokenizersearch time analyzer配合使用才能实现。

带有示例数据的完整示例

索引映射和设置

{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "Ingram", --> note this
"min_gram": 1,
"max_gram": 10
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
},
"index.max_ngram_diff" : 10 --> this you can reduce based on your requirement.
},
"mappings": {
"properties": {
"userName": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"documentName": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}

采样您的文档,然后使用相同的搜索查询,为了简洁起见,我仅索引了第一个文档和最后一个文档,并且它返回了我的第一个文档
"hits": [
{
"_index": "infix",
"_type": "_doc",
"_id": "1",
"_score": 5.7100673,
"_source": {
"userName": "johnwick",
"documentName": "john"
}
}
]

关于elasticsearch - 弹性短语前缀工作短语isnt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62401269/

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