2017-07-28 675 views
1

是否有可能查詢2個不同標籤具有指定列表中的值的記錄?Elasticsearch - 搜索標籤是否包含列表中的值

matches_list = ["4000000031265595", "4000000031265596", "4000000030004305", "4000000029975772"]

query = { 
    "query": { 
    "bool": { 
     "must": [ 
     {"match": {"id_1": matches_list}}, 
     {"match": {"id_2": matches_list}} 
     ] 
    } 
    } 
} 

上述返回: elasticsearch.exceptions.TransportError: TransportError(500, u'illegal_state_exception', u"Can't get text on a START_ARRAY at 1:57")

更新使用大列表時

錯誤:

File "/usr/local/lib/python2.7/site-packages/elasticsearch/connection/http_urllib3.py", line 128, in perform_request self._raise_error(response.status, raw_data) File "/usr/local/lib/python2.7/site-packages/elasticsearch/connection/base.py", line 122, in _raise_error raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info) elasticsearch.exceptions.RequestError: TransportError(400, u'search_phase_execution_exception', u'failed to create query: {\n "bool" : {\n "must" : [\n {\n "terms" : {\n "id_1" : my list in here ],\n "boost" : 1.0\n }\n }\n ],\n "disable_coord" : false,\n "adjust_pure_negative" : true,\n "boost" : 1.0\n }\n}')

+0

哪些類型id_1'和'id_2'的'。 – khachik

回答

1

match查詢不適用於數組,但使用單個字符串輸入。使用terms代替:

query = { 
    "query": { 
    "bool": { 
     "must": [ 
     {"terms": {"id_1": matches_list}}, 
     {"terms": {"id_2": matches_list}} 
     ] 
    } 
    } 
} 
+0

謝謝@val。這個列表的大小是否有限制?任何超過100的長度似乎失敗 – LearningSlowly

+1

很高興它有幫助。 「失敗」如何?你看到什麼錯誤? – Val

+0

再次感謝Val。我已經將它添加到問題以使其可讀。說實話不是一個很好的錯誤。我知道ES默認爲10個結果,所以我可能會改變這部分,看看它是否會影響到更深的下游 – LearningSlowly

0

試試這個:

{  
     "query": { 
      "bool": { 
       "must": [ 
        {"bool": { 
        "should": [ 
         {"match": {"id_1": "4000000031265595"}}, 
         {"match": {"id_1": "4000000031265596"}} 
        ] 
        }}, 
        {"bool": { 
        "should": [ 
         {"match": {"id_2": "4000000031265595"}}, 
         {"match": {"id_2": "4000000031265596"}} 
        ] 
        }} 

       ] 

      } 
     } 


    }