Search requests using Python
All the queries that we have discussed can be performed with the Elasticsearch Python client using the search
function. To do this, first store the query inside a variable that is query
in the following example:
query = { "query": { "match_all": {} }, }
Call the search
function with all the parameters including the index name, document type, and query. The size parameter used in the following search request can also be included inside the query itself:
response = es.search(index='twitter', doc_type='tweets', body=query, size=2, request_timeout=20)
Note
To search against more than one index, instead of using a string value, you need to use a list of index names. The same applies for document types too.
The response data comes in the following format:
{ "hits": { "hits": [ { "_score": 1, "_type": "tweets", "_id": "649956033515773953", "_source": { "contributors": null, "truncated": false, ...