简体   繁体   English

带过滤器的布尔查询不返回任何文档

[英]bool query with filter does not return any documents

The simple query 简单查询

"query": {
    "simple_query_string": { "query":"great guide" } 
},

returns my document as expected, containing 按预期返回我的文档,其中包含

"groups": [
        "Local Business"
],

But if I use a filter, it returns no documents: 但是,如果使用过滤器,则不会返回任何文档:

"query": {
        "bool":{
            "must":[
                 {"simple_query_string": { "query":"great guide" }} 
            ],
            "filter":{
              "terms":{
                "groups":["Local Business"]
              }
            }
        }
    },

If I remove the "filter" key and values, then the document is retrieved. 如果删除“过滤器”键和值,则将检索文档。

Why isn't the filter matching the document ? 过滤器为什么不匹配文档?

If the groups field is of type keyword , then the query you've mentioned works as expected. 如果groups字段的类型为keyword ,那么您提到的查询将按预期工作。

However it wouldn't work if the field groups if of type text . 但是,如果该字段groupstext类型,则将不起作用。 In that case the below query would actually fit what you are looking for. 在这种情况下,以下查询实际上将满足您的需求。

Query for group - Type text 查询组-输入文字

POST <your_index_name>/_search
{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "simple_query_string":{  
                  "query":"great guide"
               }
            }
         ],
         "filter":{  
            "match":{  
               "groups":"Local Business"
            }
         }
      }
   }
}

The reason the query you've mentioned doesn't work for the field of type text is because this field goes through Analysis phase making use of Standard Analyzer by default where it would first convert Local Business into small cases and then saves local and business as two individual words in the inverted index. 您提到的查询不适用于类型text字段的原因是,默认情况下,该字段会使用标准分析器来进行“ 分析”阶段,该阶段首先将“ Local Business转换为小写形式,然后将“ local和“ business倒排索引中的两个单词。

Elasticsearch would only give you results if the words you query match what's available in the index. 仅当您查询的单词与索引中可用的单词匹配时,Elasticsearch才会为您提供结果。

And what keyword does is, it saves Local Business as is in inverted index. keyword作用是,它可以将Local Business保存为倒排索引。

Note: You can try the query you have by replacing groups with groups.keyword if mapping hasn't been defined and is created dynamically. 注意:如果尚未定义映射并且是动态创建的,则可以通过将groups替换为groups.keyword来尝试查询。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM