我正在使用Elasticsearch。我使用下面给出的映射创建了索引自动完成功能。我正在尝试多个基于上下文的自动完成。当我检查结果时,发现建议不正确。
我有一个层次自动完成的情况。 我是第一位成为独立自动完成领域的经销商。现场状态将经销商作为自动完成建议的上下文。现场县有经销商和州作为自动完成领域的上下文。
mapping ={
"mappings": {
"panels": {
"properties": {
"dealer": {
"type": "completion"
},
"county": {
"type": "completion",
"contexts": [{
"name": "dealername",
"type": "category",
"path": "dealername"
},
{
"name": "statename",
"type": "category",
"path": "statename"
}
]
},
"state": {
"type": "completion",
"contexts": [{
"name": "dealername",
"type": "category",
"path": "dealername"
}]
},
"dealername":{
"type": "keyword"
},
"statename":{
"type": "keyword"
},
"countyname":{
"type": "keyword"
},
"meid": {
"type": "text"
}
}
}
}
}
res = es.indices.create(index="autocomplete", ignore=[400], body=mapping)
print(res)
def insertstatewise():
rows = session.execute("SELECT dealer_name, statename, county_name, meid from analytics.nearest_reco_output_1")
i = 0
for row in rows:
i += 1
yield {
"_id": row["meid"],
"_index": "autocomplete",
"_type": 'panels',
"_source":
{
"dealer": {
"input": [row["dealer_name"]]
},
"state": {
"input": [row["statename"]],
"contexts": {
"dealername": [row["dealer_name"]]
}
},
"county": {
"input": [row["county_name"]],
"contexts": {
"dealername": [row["dealer_name"]],
"statename": [row["statename"]]
}
},
"dealername": row["dealer_name"],
"statename": row["statename"],
"countyname": row["county_name"],
"meid": row["meid"]
},
}
xp = bulk(es, insertstatewise())
print(xp)
当我使用curl请求查询索引时
curl -X POST "########:9200/autocomplete/_search?pretty" -H 'Content-Type: application/json' -d'
{
"suggest": {
"state_suggestion_for_dealer" : {
"prefix" : "dealer2",
"completion" : {
"field" : "dealer",
"size": 10
}
}
}
}
预期结果应为delaer2
对于完成样本,我得到了错误的结果
{
"text" : "dealer3",
"_index" : "autocomplete",
"_type" : "panels",
"_id" : "#############",
"_score" : 1.0,
"_source" : {
"dealer" : {
"input" : [
"dealer3"
]
},
"state" : {
"input" : [
"Virginia"
],
"contexts" : {
"dealername" : [
"dealer3"
]
}
},
"county" : {
"input" : [
"Park"
],
"contexts" : {
"dealername" : [
"dealer3"
],
"statename" : [
"Virginia"
]
}
},
"dealername" : "dealer3",
"statename" : "Virginia",
"countyname" : "Park",
"meid" : "#########"
}
}
正如我查询的前缀为Dealer2的完成那样,那么为什么完成建议中包含有Dealer3文档。