这是我使用的索引和 doc_type 的设置和映射。
es_mapping = {
"settings": {
"analysis": {
"filter": {
"edgeNGram_filter": {
"type": "edgeNGram",
"min_gram": 2,
"max_gram": 20,
"side": "front"
}
},
"analyzer": {
"edge_nGram_analyzer": {
"type": "custom",
"tokenizer": "edge_ngram_tokenizer",
"filter": [
"lowercase",
"asciifolding",
"edgeNGram_filter"
]
},
"whitespace_analyzer": {
"type": "custom",
"tokenizer": "whitespace",
"filter": [
"lowercase",
"asciifolding"
]
}
},
"tokenizer": {
"edge_ngram_tokenizer": {
"type": "edgeNGram",
"min_gram": "2",
"max_gram": "5",
"token_chars": [
"letter",
"digit"
]
}
}
}
},
"mappings": {
"videos": {
"properties": {
"fileName": {
"type": "string"
},
"path": {
"type": "string",
"index": "no"
}
}
}
}
}
# creating index with the above schema.
es.create_index('media', settings = es_mapping)
因此,只有两个字段,即 文件名 和 路径 , 我需要对 进行自动完成查询文件名 .
我已添加所有 ngram 分析器 和 空白分析器
我的查询是匹配短语
"query": {
"match_phrase": {
"fileName": {
"query": "a"
}
}
}
上面的查询给了我所有匹配的短语,基本上是全文搜索,但我想要 替代 , 苹果 , 宏基作为建议,只要这些论文在索引中。
如何更改查询?
在 val 建议后编辑
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 20
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
},
"mappings": {
"videos": {
"properties": {
"fileName": {
"type": "string",
"analyzer": "autocomplete",
"search_analyzer": "standard"
},
"path": {
"type": "string",
"index": "no"
}
}
}
}
}
现在,这是自动完成的设置和映射。
查询的结构如下:
{
"query": {
"match": {
"fileName": "a"
}
},
"highlight" : {
"fields" : {
"fileName" : {"force_source" : true}
}
}
}
我用 亮点 ,这增加了用户的交互性。
最佳答案
您没有使用您定义的分析器。您需要将它们设置为默认分析器或专门设置为 analyzer
或 search_analyzer
在你的领域。
然后,您需要删除索引,重新创建它并重新索引您的数据。
之后,您将获得您期望的结果。
关于elasticsearch - 如何在 Elasticsearch 中查询自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36806081/