1

How can i search in elasticsearch dsl python module on multi fields, example on title and body field and order it by created_at field DESC.

I have this example that search only on title field:

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

how can i do this?

Mirza Delic
  • 4,119
  • 12
  • 55
  • 86

2 Answers2

5

Found solution:

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()
Mirza Delic
  • 4,119
  • 12
  • 55
  • 86
3

There is also MultiSearch class now. So you it's possible:

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()

and then you can and group it, and order, and so on. But result is a collection of responses by every filter.