2014-05-13 47 views
1

我有一個查詢,我正在ElasticSearch中掙扎。事實上,我有兩個疑問,第一個作品,但第二個沒有。ElasticSearch嵌套[AND OR]篩選器

這裏的基礎知識第一次查詢,這是很好的(查詢實際上比這更大的不少,我已經剝離下來,爲了便於理解):

{ 
    "query": { 
     "filtered": { 
      "query": { 
       "match_all":{} 
      }, 
      "filter": { 
       "and": [{ 
        "term":{ 
         "relatedID":"214" 
        } 
       }, 
       { 
        "term":{ 
         "relatedType":"deal" 
        } 
       }] 
      } 
     } 
    } 
} 

所以基本上,抓住所有的項目其中relatedID == 214 & & relatedType == 「交易」

不過,我也想這樣做,是這樣的:

{ 
    "query": { 
     "filtered": { 
      "query": { 
       "match_all":{} 
      }, 
      "filter": { 
       "and": [{ 
        { 
         "or":[{ 
           "and":[{ 
            "relatedID":"528" 
           }, 
           { 
            "relatedType":"company" 
           }] 
          },{ 
           "and":[{ 
            "relatedID":"214" 
           }, 
           { 
            "relatedType":"deal" 
            } 
            ]} 
          ]} 
         ]} 
        } 
       }     
      } 
     } 
    } 
} 

小號o基本上,抓住一切交易AND 214,或公司AND 528.

這是我似乎陷入困境。

正如我所說的,有一個在查詢了很多(見下文完整的查詢,這可能有助於理解爲什麼我試圖構建它上面):

{ 
"query":{ 
    "filtered":{ 
     "query":{ 
      "match_all":{} 
     }, 
     "filter":{ 
      "and":[ 
       { 
        "or":[ 
         { 
          "term":{ 
           "timeSensitive":false 
          } 
         } 
         , 
         { 
          "range":{ 
           "validTo":{ 
            "gt":"20140513T153030Z" 
           }, 
           "validFrom":{ 
            "lt":"20140513T153030Z" 
           } 
          } 
         } 
         ] 
        } 
        , 
        { 
         "term":{ 
          "categoryTree.NAME":"Promotions" 
         } 
        } 
        , 
        { 
         "or":[ 
          { 
           "and":[ 
            { 
             "relatedID":"528" 
            } 
            , 
            { 
             "relatedType":"company" 
            } 
           ] 
          } 
          , 
          { 
           "and":[ 
            { 
             "relatedID":"214" 
            } 
            , 
            { 
             "relatedType":"deal" 
            } 
            ] 
          } 
         ] 
        } 
        , 
        { 
         "terms":{ 
          "permissions": ["member","supplier"] 
         } 
        } 
        , 
        { 
         "term":{ 
          "siteID":6 
         } 
         } 
        ] 
       } 
      } 
      } 
     } 
    } 

}

+0

你知道'bool'濾波器是由ES文件建議,而不是'和'和'或'過濾器? –

回答

3

它的語法錯誤。問題是你沒有提到內部(和,或)過濾條件過濾器。

嘗試使用布爾過濾器並查詢它比(和或)過濾器更快。

[AND OR]過濾

{ 
"query": { 
    "filtered": { 
    "query": { 
     "match_all": {} 
    }, 
    "filter": { 
     "and": [ 
      { 
       "or": [ 
       { 
        "and": [ 
         { 
          "term": { 
          "relatedID": "528" 
          } 
         }, 
         { 
          "term": { 
          "relatedType": "company" 
          } 
         } 
        ] 
       }, 
       { 
        "and": [ 
         { 
          "term": { 
          "relatedID": "214" 
          } 
         }, 
         { 
          "term": { 
          "relatedType": "deal" 
          } 
         } 
        ] 
       } 
       ] 
      } 
     ] 
    } 
    } 
    } 
    } 

相同的查詢我轉換到布爾過濾

curl -XPOST "http://localhost:9200/try/_search" -d' 
{ 
"query": { 
"filtered": { 
    "query": { 
    "match_all": {} 
    }, 
    "filter": { 
     "bool": { 
      "should": [ 
       { 
        "bool": { 
         "must": [ 
         { 
          "term": { 
           "relatedID":"528" 
          } 
         }, 
          { 
          "term": { 
           "relatedType":"company" 
          } 
         } 
         ] 
        } 
       }, 
       { 
        "bool": { 
         "must": [ 
         { 
          "term": { 
           "relatedID":"214" 
          } 
         }, 
          { 
          "term": { 
           "relatedType":"deal" 
          } 
         } 
         ] 
        } 
       } 
      ] 
     } 
    } 
} 
} 
}' 
+0

是的 - 我的錯。在我創建結構體時是不正確的! – Tom