如何在elasticsearch pyes中使用“suggest”?
如何在pyes中使用“suggest”功能?由于文档不太清楚,我似乎搞不明白。有人能提供一个有效的例子吗?我尝试的都没有效果。在文档中,它被列在查询下,但使用:
query = Suggest(fields="fieldname")
connectionobject.search(query=query)
2 个回答
7
从5.0版本开始:
以前的_suggest接口已经不再推荐使用,现在建议通过_search接口来进行建议查询。在5.0版本中,_search接口已经专门优化过,只用于建议搜索请求。
(来源于 https://www.elastic.co/guide/en/elasticsearch/reference/5.5/search-suggesters.html)
更好的做法是使用搜索API,并选择suggest
选项。
from elasticsearch import Elasticsearch
es = Elasticsearch()
text = 'ra'
suggest_dictionary = {"my-entity-suggest" : {
'text' : text,
"completion" : {
"field" : "suggest"
}
}
}
query_dictionary = {'suggest' : suggest_dictionary}
res = es.search(
index='auto_sugg',
doc_type='entity',
body=query_dictionary)
print(res)
确保你在每个文档中都包含了suggest
字段。
sample_entity= {
'id' : 'test123',
'name': 'Ramtin Seraj',
'title' : 'XYZ',
"suggest" : {
"input": [ 'Ramtin', 'Seraj', 'XYZ'],
"output": "Ramtin Seraj",
"weight" : 34 # a prior weight
}
}
5
这是我的代码,运行得非常好。
from elasticsearch import Elasticsearch
es = Elasticsearch()
text = 'ra'
suggDoc = {
"entity-suggest" : {
'text' : text,
"completion" : {
"field" : "suggest"
}
}
}
res = es.suggest(body=suggDoc, index="auto_sugg", params=None)
print(res)
我使用了在elasticsearch网站上提到的同一个客户端,具体可以在这里找到。
我通过使用这里的completion suggester
将数据索引到elasticsearch索引中。