2016-06-28 110 views
1

我需要用某种条件逻辑(我认为)过滤掉文档,但我无法实现这个工作。Elasticsearch条件筛选器

我的文件是这样的: {type: 'A'} {type: 'B', foo: ['bar']} {type: 'B', foo: []}

现在我需要过滤掉type“B”,其中foo是空的所有文件。 伪代码查询将是: if(type == 'B' && foo == []) { // filter out this document } 我的主查询看起来是这样的: query: { filtered: { query: { bool: { must: { match_all: [] } } }, filter: { bool:{ must_not: { term: {'_hidden': true} } } } } } Elasticsearch版本是1.5.0

+0

Elasticsearch不存储空字段。我想你说的是一个不存在的领域,对吧?你可以在must_not中使用exists过滤器吗? https://www.elastic.co/guide/en/elasticsearch/reference/1.4/query-dsl-exists-filter.html 你尝试过吗? –

+0

Hi @WaldemarNeto。是的,我确实尝试使用“存在”和/或“丢失”过滤器。 这对于'B'类型的所有文件都有效。但它也会过滤所有类型为'A'的文档,因为从来没有“property”foo。 –

+0

为了理解,如果输入==“A”foo无关紧要,对吧?如果type ==“B”foo需要为空? –

回答

0

如果我的理解,是什么? 如果键入== a,则所有具有a的文档都将返回。 如果type == b和字段foo不在那里,他们会返回,有意义吗?

{ 
    "query": { 
     "filtered": { 
     "query": { 
      "match_all": {} 
     }, 
     "filter": { 
      "bool": { 
       "should": [ 
        { 
        "term": { 
         "type": "a" 
        } 
        }, 
        { 
         "bool": { 
          "must": [ 
          { 
           "term": { 
            "type": "b" 
           } 
          } 
          ], 
          "must_not": [ 
          { 
           "missing": { 
            "field": "foo" 
           } 
          } 
          ] 
         } 
        } 
       ] 
      } 
     } 
     } 
    } 
} 
+0

我想他想排除缺少foo字段的b型文件不包含在结果中。 –

+1

这是正确的。另外我不太关心其他文件的类型。可能有很多不同的类型。我只想排除'foo'丢失的'B'类文件。 –

+0

立即看看:) –