PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] How to search for multiple queries in ElasticSearch using Python

 October 25, 2022     elasticsearch, python     No comments   

Issue

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)
      

Solution

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())


Answered By - Bloomstar
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing