Search code examples
pythonelasticsearch-dsl

How to specify the document type when using elasticsearch_dsl (Python)? Similarly, how to specify a few indices?


I want to create this example

GET /my_store/products/_search
{
    "query" : {
        "filtered" : { 
            "query" : {
                "match_all" : {} 
            },
            "filter" : {
                "term" : { 
                    "price" : 20
                }
            }
        }
    }
}

using Pythons elasticsearch_dsl.

import elasticsearch as ES
import elasticsearch_dsl as dsl
from elasticsearch_dsl import Search

client = ES.Elasticsearch() # i'm using the localhost default client
s = Search(using = client, index = "my_store") 

ok, this specifies the host, port, and index.

s = s.filter("term", price = 20)
results = s.execute().to_dict()

but how do I specify the document type is "products"? Seems like there should be an argument in the Search() function.

Similar question, suppose I want to run the same query, but I want it to run over the indices "my_store" and "her_store". How do I specify this?


Solution

  • You can use like this.

    s = Search(using=client, index=('my_report', 'my_store'), doc_type=('products'))

    Index param takes list, tuple, string types.

    In the Search constructor you can see

    if isinstance(index, (tuple, list)):
        self._index = list(index)
    elif index:
        self._index = [index]