2013-06-12 204 views
0

我想寫一個查詢,要求「區域」爲530,「開始」爲06192013或「區域」爲「530」,「開始」爲「空白」 。此外,在這兩種場景中,「空間」爲「頂部」或「空間2」爲「底部」。這個查詢似乎在抓取任何匹配這些場景的任何東西,我如何改變它以使其像我想要的那樣工作?創建彈性搜索和或查詢

{ 
    "size":25, 
    "from":0, 
    "query": { 
    "custom_filters_score": { 
     "query": { 
     "filtered": { 
      "query": { 
      "bool": { 
       "must": [ 
       {"term":{"type":"ghost"}}, 
       {"term":{"area":"530"}} 
       ] 
      } 
      }, 
      "filter" : { 
      "or": [ 
       {"terms":{"space": ["top"]}}, 
       {"terms":{"space2":["bottom"]}}, 
       { 
       "and": [ 
        {"term":{"area":"530"}}, 
        {"term":{"start":"06192013"}} 
       ] 
       }, 
       { 
       "and": [ 
        {"term":{"area":"530"}}, 
        {"term":{"starts":"blank"}} 
       ] 
       } 
      ] 
      } 
     } 
     }, 
     "filters": [ 
     {"filter":{"term":{"filter1":5743}}, "boost":"1000"}, 
     {"filter":{"term":{"filter2":4451}}, "boost":"64"}, 
     {"filter":{"term":{"filter3":["tech"]}}, "boost":"16"}, 
     {"filter":{"terms":{"filter4":[]}}, "boost":"8"}, 
     {"filter":{"terms":{"filter5":[]}}, "boost":"5"}, 
     {"filter":{"term":{"access":"1"}}, "boost":"2"} 
     ], 
     "score_mode":"total" 
    } 
    } 
} 

回答

1

你幾乎在那裏!試試這個:

... 
"query" : { "match_all":{} }, 
"filter" : { 
    "and": [ 
     { 
      "or": [ 
       {"term":{"space": "top"}}, 
       {"term":{"space2":"bottom"}} 
      ] 
     }, 
     { 
      "and": [ 
       {"term":{"area":"530"}}, 
       { 
        "or": [ 
         {"term":{"start":"06192013"}}, 
         {"term":{"starts":"blank"}} 
        ] 
       } 
      ] 
     } 
    ] 
} 
... 

,除非你需要進球(其中它並不像),你應該做到這一切有過濾器,他們不計算分數,並因爲這個得更快。

"query" : { "match_all":{} }將最終給所有文檔女巫匹配篩選器相同得分

注意:我也將您的terms查詢與一個元素的數組轉換爲term查詢...