简体   繁体   中英

How to search for multiple queries in ElasticSearch using Python

I am trying to send multiple queries to ElasticSearch using Python. I have all my queries gathered in a list, where queries are type of dict. I am able to send them separately to Elastic using:

def send_query(query):
es = Elasticsearch([uri])
res = es.search(index="index", body=query, size=100)
return res

Could you advice how to send all queries from the list simultaneously? I was trying msearch like this, but it doesn't work:

es = Elasticsearch([uri])
res = es.msearch(index="index", body=query_list_all)
print(res)
      

can you try below code snippet.

from elasticsearch_dsl.connections import connections
from elasticsearch_dsl import Search, Q

client = connections.create_connection(hosts=['https://user:password@es_ip:9200'],use_ssl=True, verify_certs=False)
q1 = Q("match",fruit="apple")
q2 = Q("match",color="red")
es_search = Search(index='fruit-index').using(client).query("bool", must=[q1, q2])
es_response = es_search.execute()
print("hits count: ", es_response.to_dict()['hits']['total']['value'])
print("response: ",es_response.to_dict())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM