Python elasticsearch dsl 搜索多字段和排序结果

Python elasticsearch dsl search multi fields and order result

我如何在 elasticsearch dsl python 模块中搜索多字段,例如 titlebody 字段并按 created_at 字段 DESC 排序。

我有这个例子,只搜索 title 字段:

q = request.GET.get('q', None)
s = Search(using=elastic_client, index='post').query('match', title=q)
response = s.execute()

我该怎么做?

找到解决方案:

from elasticsearch_dsl.query import MultiMatch

q = request.GET.get('q', None)
query = MultiMatch(query=q, fields=['title', 'body'], fuzziness='AUTO')
s = Search(using=elastic_client, index='post').query(query)
response = s.execute()

现在还有MultiSearchclass。所以你有可能:

from elasticsearch_dsl import MultiSearch, Search

ms = MultiSearch(index='post')

ms = ms.add(Search().filter('term', tags='title'))
ms = ms.add(Search().filter('term', tags='body'))

responses = ms.execute()

然后您可以对其进行分组、排序等等。但结果是每个过滤器的响应集合。