这是我的代码:
class SearchSuggest(View):
def get(self, request):
key_words = request.GET.get('s', '')
re_datas = []
if key_words:
s = ArticleType.search()
s = s.suggest('my_suggest', key_words, completion={
"field": "suggest", "fuzzy": {
"fuzziness": 1
},
"size": 5
})
suggestions = s.execute_suggest()
for match in suggestions.my_suggest[0].options:
source = match._source
re_datas.append(source["title"])
return HttpResponse(json.dumps(re_datas),
content_type="application/json")
这是django中的一段代码。当我运行这个项目时。它提出:
File "/home/yixuan/PycharmProjects/Scrapy/LcvSearch/search/views.py", line 20, in get
suggestions = s.execute_suggest()
AttributeError: 'Search' object has no attribute 'execute_suggest'
我不知道错误在哪里。如果你能解决它,我将不胜感激。
我的版本是:
答案 0 :(得分:0)
看起来elasticsearch-dsl已从Service {
config:
Config {
...
...
region: 'us-west-2',
logger: null,
apiVersions: {},
apiVersion: null,
endpoint: 'elasticmapreduce.us-west-2.amazonaws.com',
httpOptions: { timeout: 120000 },
maxRetries: undefined,
},
endpoint:
Endpoint {
protocol: 'https:',
host: 'elasticmapreduce.us-west-2.amazonaws.com',
port: 443,
hostname: 'elasticmapreduce.us-west-2.amazonaws.com',
pathname: '/',
path: '/',
href: 'https://elasticmapreduce.us-west-2.amazonaws.com/'
},
...
}
对象中删除了函数execute_suggest
。不得不检查源代码,因为它似乎没有记录在他们的更改日志或版本中。
我假设您可以使用Search
并根据您的需要解析响应,但以下是execute
的源代码,如果您想自己实现它。< / p>
execute_suggest
def execute_suggest(self):
es = connections.get_connection(self._using)
return SuggestResponse(
es.suggest(
index=self._index,
body=self._suggest,
**self._params
)
)
只是出现SuggestResponse
。
来源:
https://github.com/elastic/elasticsearch-dsl-py/blob/6.1.0/elasticsearch_dsl/search.py https://github.com/elastic/elasticsearch-dsl-py/blob/5.4.0/elasticsearch_dsl/search.py
希望这有帮助。
答案 1 :(得分:0)
检查您的elastucsearch-dsl版本
elasticsearch-dsl> = 6.0.0,<7.0.0
elasticsearch-dsl> = 5.0.0,<6.0.0
elasticsearch-dsl> = 2.0.0,<3.0.0