0

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

1
  • 3
    Can you show how you are setting up your es client in your Python code? Commented May 31, 2016 at 7:37

1 Answer 1

1

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":{}}})
Sign up to request clarification or add additional context in comments.

6 Comments

yes, already i have done that. I just metioned the syntax for you to come to know that. But i didn't get the result
please copy/paste your 4 lines of python - just to make sure that you don't have any typo.
@PythonTeam : I think that I found you bug... the port was defined as string instead of int
I changed like as you said. Now also am not getting the result.
try running again the "curl" command, to make sure that the elasticsearch server is up. and try both, your code with port = 9200, and the 4 lines of code in my answer (modify the value of host_ip). Those 4 lines are working for me...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.