Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django-elasticsearch-dsl: how to search across multiple documents?

I have several ElasticSearch documents in Django describing each a different type of object: 'MovieDoc,' 'CartoonDoc,' etc. For now on, I can search across every such document individually:

document = MovieDoc
results = document.search().query('some phrase')

But what if I want to search across all documents at once and get the results altogether sorted by relevance (i.e. not searching every individual document and merging thereafter)?

I have tried something like this based on the documentation of elasticsearch-dsl, but this did not yield any results:

from elasticsearch_dsl import Search    
results = Search(index=['movie_docs', 'cartoon_docs']).query('some phrase')
like image 855
Edmond Avatar asked Dec 12 '25 02:12

Edmond


1 Answers

You can use a wildcard operator (*) as the index to search across all documents.

Implementation details may change over time. See the documentation on Multi-target syntax for information pertaining to your version of Elasticsearch.

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search

client = Elasticsearch()

s = Search(using=client, index="*")

response = s.execute()

for hit in response:
    print(hit)
like image 76
Taylor Steinberg Avatar answered Dec 14 '25 17:12

Taylor Steinberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!