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?
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.
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.
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.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With