2014-05-13 48 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