Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 944
travelingbones Avatar asked Nov 18 '15 01:11

travelingbones


People also ask

What is dsl query?

Query DSLedit. Elasticsearch provides a full Query DSL (Domain Specific Language) based on JSON to define queries. Think of the Query DSL as an AST (Abstract Syntax Tree) of queries, consisting of two types of clauses: Leaf query clauses.

What is elastic search DSL?

Elasticsearch DSL is a high-level library whose aim is to help with writing and running queries against Elasticsearch. It is built on top of the official low-level client ( elasticsearch-py ). It provides a more convenient and idiomatic way to write and manipulate queries.

What is Elasticsearch Doc_type?

Elasticsearch is a powerful open source search and analytics engine that makes data easy to explore. Doc_type is a field in Elasticsearch that allows you to specify the type of document you are indexing. This is useful for when you have multiple types of documents in the same index.


1 Answers

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]
like image 145
Isabek Tashiev Avatar answered Nov 07 '22 11:11

Isabek Tashiev