2016-06-07 106 views
0

我有3个字段,例如,field1field2field3。 我需要这样创造的东西:ElasticSearch带过滤器或布尔查询

if { field1 != 1 AND field2 != 1 AND field3 != 1 } OR if { field1 != 2 AND field2 != 2 AND field3 != 2 }

我试图通过两个boolquery做到这一点,但它返回我什么。 然后我试图将这些查询包装到一个filter中,但它总是引发我异常no query defined for [filter]]。但是我的boolquery这个过滤器总是空的,它什么都没有返回。我可以添加一些东西到这个boolquery检索所有结果,然后过滤它们?或者我需要以另一种方式创建查询,但是如何?

+0

您运行的是什么版本的ES的? – Val

+0

@Val ES版本1.5.2 – Squeez

回答

1

这里是你如何能做到这

{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "or": [ 
      { 
      "and": [ 
       { 
       "not": { 
        "term": { 
        "field1": "1" 
        } 
       } 
       }, 
       { 
       "not": { 
        "term": { 
        "field2": "1" 
        } 
       } 
       }, 
       { 
       "not": { 
        "term": { 
        "field3": "1" 
        } 
       } 
       } 
      ] 
      }, 
      { 
      "and": [ 
       { 
       "not": { 
        "term": { 
        "field1": "2" 
        } 
       } 
       }, 
       { 
       "not": { 
        "term": { 
        "field2": "2" 
        } 
       } 
       }, 
       { 
       "not": { 
        "term": { 
        "field3": "2" 
        } 
       } 
       } 
      ] 
      } 
     ] 
     } 
    } 
    } 
} 
+0

这对我不起作用。它没有找到任何东西。也许这是因为我使用1.5.2 ES? – Squeez

+0

@Squeez你能分享你的索引映射以及你试图否定的实际值吗? (我猜这不是1和2)。 –