2016-01-13 58 views
1

我试图在满足以下条件时从elasticsearch索引中提取数据。在弹性搜索查询中使用两个检查

  1. stream_id是空字符串(例如, “”)
  2. source_id是一个特定的字符串(例如, “unknown_source”)

我可以单独使用这两个以下查询: 对于检查空字符串:

{ 
"query": { 
    "filtered": { 
    "filter": { 
     "script": { 
     "script": "_source.stream_id.length() == 0" 
     } 
    } 
    } 
} 
} 

检查source_id

{ 
"query": { 
    "bool" : { 
    "must" : { 
     "term" : { "source_id" : "unknown_source" } 
    } 
    } 
} 
} 

但是现在我看到'过滤'查询在ES 2.0版中已弃用,而且我使用的是版本2.1。那么,我怎么和这两个条件?我看着到过滤查询页面的文档,它说

使用布尔查询,而不是与查询和过滤器的过滤器 条款必须的条款。

所以,然后我试着下面的查询,但它不工作。

{ 
"query": { 
    "bool": { 
    "must" : { 
     "term" : { "source_id" : "unknown_source" } 
    }, 
    "filter": { 
     "script": { 
      "script": "_source.stream_id.length() == 0" 
     } 
    } 
} 
} 
} 

回答

2

您可以用bool/mustfilter部分里面做这样的(因为你不关心得分)

{ 
    "query": { 
    "bool": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "script": { 
       "script": "_source.stream_id.length() == 0" 
       } 
      }, 
      { 
       "term": { 
       "source_id": "unknown_source" 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
} 

,或者您也可以腾出两个层面都已经记下来查询上下文(即使你不关心评分)

{ 
    "query": { 
    "bool": { 
     "must": [ 
     { 
      "script": { 
      "script": "_source.stream_id.length() == 0" 
      } 
     }, 
     { 
      "term": { 
      "source_id": "unknown_source" 
      } 
     } 
     ] 
    } 
    } 
} 
+0

第一个代码得到一个错误,说'QueryParsingException [[apache_combined] [bool]查询不支持[filter]]'。此外,我需要这两个条件相匹配。 – Bitswazsky

+0

我的uri是'http:// localhost:9200/apache_combined/_search?fields = message'。第二个查询会抛出一个错误,说'QueryParsingException [[apache_combined]没有注册[script]]的查询; }]“,'我在elasticsearch.yml中添加了'script.disable_dynamic:false'条目 – Bitswazsky

+0

我已经更新了我的答案,例如'must'而不是'should',对此很抱歉,2.1中需要[启用动态脚本](https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html#enable-dynamic-scripting)with'script.inline:on' – Val