2016-04-29 74 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