2016-04-29 81 views
2

我有一些代碼,看起來像這樣如何正確構建使用elasticsearch python API的查詢?

from elasticsearch import Elasticsearch 

client = Elasticsearch(hosts = [myhost]) 
try: 
    results = es_client.search(
     body = { 
      'query' : { 
       'bool' : { 
        'must' : { 
         'term' : { 
          'foo' : 'bar', 
          'hello' : 'world' 
         } 
        } 
       } 
      } 
     }, 
     index = 'index_A,index_B', 
     size = 10, 
     from_ = 0 
    ) 
except Exception as e: 
    ## my code stops here, as there is an exception 
    import pdb 
    pdb.set_trace() 

檢查異常

SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; 

進一步回落

Parse Failure [Failed to parse source [{"query": {"bool": {"must": {"term": {"foo": "bar", "hello": "world"}}}}}]]]; nested: QueryParsingException[[index_A] [bool] query does not support [must]]; 

堆棧跟蹤是巨大的,所以我只是抓住的片段它,但主要的錯誤似乎是,「必須」不被支持,至少我構建我的查詢的方式。

我使用thisthis作爲構建查詢的指導。

我可以發佈一個更完整的堆棧跟蹤,但我希望有人能夠看到一個非常明顯的錯誤,我已經在「搜索」方法內的「body」參數內進行了。

任何人都可以看到任何我已經明確做錯了,只要構建python API的查詢正文?

回答

1

查詢的語法對我來說看起來不正確。試試這個:

results = es_client.search(
    body = { 
     "query": { 
     "bool": { 
      "must": [ 
      { 
       "term": { 
       "foo": { 
        "value": "bar" 
       } 
       } 
      }, 
      { 
       "term": { 
       "hello": { 
        "value": "world" 
       } 
       } 
      } 
      ] 
     } 
     } 
    }, 
    index = 'index_A,index_B', 
    size = 10, 
    from_ = 0 
) 
+0

謝謝你好先生。這段代碼做我需要它做的事情。 – Zack