How to specify document type when using elasticsearch_dsl (Python)? Similarly, how do I specify multiple indexes?

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 indicates the host, port, and index.

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

but how to specify the type of document - is it "products"? Looks like there should be an argument in the Search () function.

A similar question, suppose I want to run the same query, but I want it to work on the indices "my_store" and "her_store". How to indicate this?

+4
source share
2 answers

You can use this.

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

Index parameter accepts types list, tuple, string.

In the search constructor you can see

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

I just started working like this:

client = connections.create_connection(hosts=['myNode01:9200'])
s = Search().using(client)
      .index('myindex')
      .doc_type('mytype1')
      .doc_type('mytype2')
      .query('match', name='arandomname')
+3
source

Source: https://habr.com/ru/post/1616300/


All Articles