2016-05-31 113 views
0

我正在研究elasticsearch。我在elasticsearch中創建並索引了json文件內容。現在我試圖使用搜索查詢來搜索文本。當我嘗試在elasticsearch中執行下面的查詢時,它不會返回結果中的任何內容。但價值在其中。因爲我使用curl命令進行檢查,所以它返回結果。搜索查詢不會返回elasticsearch中的任何內容

我執行elasticsearch查詢命令

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"]) 

它不調用for循環。我不知道爲什麼

它不返回任何

我執行捲曲命令

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

它越來越正確的結果

+3

你能告訴你如何設置您'es'您的客戶端Python代碼? – Val

回答

1

與您的代碼的問題是「口」 as string

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

It應該是:

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

這是需要Python代碼(你需要與你的主機IP更換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":{}}}) 
+0

是的,我已經做到了。我只是提到你的語法來了解這一點。但我沒有得到結果 –

+0

請複製/粘貼你的4行python - 只是爲了確保你沒有任何錯字。 – Yaron

+0

我編輯過,請檢查一次。 –

相關問題