Python Team
Python Team

Reputation: 1171

Search query doesn't return anything in elasticsearch

I am working on elasticsearch. I created and indexed json file content in elasticsearch.Now am trying to search text using search query.When i try to execute the below query in elasticsearch, it doesn't return anything in the result. But values are in it. Because i checked using the curl command,it return result.

I executed elasticsearch query command

ES_HOST = {"host":"xxx.xx.xx.xx","port":"9200"}
ES_INDEX = 'sugan'
ES_TYPE = 'doc'

es = Elasticsearch(hosts=[ES_HOST])

res = es.search(index=ES_INDEX,body={"query":{"match_all":{}}})

when i print **res**, it's response like below

Search Response:'{u'hits': {u'hits': [], u'total': 0, u'max_score': None}, u'_shards': {u'successful': 1, u'failed': 0, u'total': 1}, u'took': 1, u'timed_out': False}'

 for hit in result['hits']['hits']:
    print "results:"
    print (hit["_source"])

it doesn't call the for loop. I don't know why

It doesn't return anything

I executed curl command

curl -XGET 'xxx.xx.xx.xx:9200/sugan/_search?'

It's getting proper result

Upvotes: 0

Views: 1445

Answers (1)

Yaron
Yaron

Reputation: 10450

The problem with your code is "port" as string

ES_HOST = {"host":"xxx.xx.xx.xx","port":"9200"}

It should be:

ES_HOST = {"host":"xxx.xx.xx.xx","port": 9200}

This is the needed python code (you need to replace host_ip with your host IP)

from elasticsearch import Elasticsearch
es =  Elasticsearch(hosts = [{"host" : host_ip, "port" : 9200}])
ES_INDEX='sugan'
es.search(index=ES_INDEX,body={"query":{"match_all":{}}})

Upvotes: 1

Related Questions