以下是我使用的索引和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)
因此,只有两个字段,即 fileName 和路径,我需要对文件名进行自动完成查询。
我添加了所有 ngram-analyzers 和空白分析器
我的查询是 match_phrase
"query": {
"match_phrase": {
"fileName": {
"query": "a"
}
}
}
以上查询为我提供了所有匹配的短语,基本上是全文搜索,但我想要像 alt , apple , acer 作为建议,只要论文中有论文。
有人可以帮我改变查询吗?
提前致谢。
在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}
}
}
}
我使用突出显示,这会增加用户的互动性。
答案 0 :(得分:1)
您没有使用已定义的分析仪。您需要将它们设置为默认分析器,或者专门设置为字段上的analyzer
或search_analyzer
。
然后,您需要擦除索引,重新创建索引并重新索引数据。
之后,您将获得您期望的结果。