elasticsearch - 使用ElasticSearch查询进行过滤和匹配

标签 elasticsearch

我在将辅助过滤器应用于以下我的elasticsearch查询时遇到问题。仅第一个过滤器匹配。我希望两个过滤器都适用于查询。

  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "filter": {
              "range": {
                "@timestamp": {
                  "gte": "2019-03-12",
                  "lte": "2019-03-13"
                }
              }
            }
          }
        },
        {
          "bool": {
            "filter": {
              "bool": {
                "must": {
                  "match": {
                    "msg_text": "foo AND bar"
                  }
                }
              }
            }
          }
        }
      ]
    }
  }

最佳答案

我已经提到了两种解决方案,第一种使用Match Query,第二种使用Query String

我还假设msg_text字段的类型为text

区别在于, query_string 使用解析器,该解析器将根据AND, OR等运算符来解析您提到的文本。

虽然 match query 会读取文本,但会分析文本并基于该文本构造bool查询。从某种意义上说,您无需提及运算符,它将不起作用

您可以在我提到的链接中阅读有关它们的更多信息。

1.使用匹配查询

POST <your_index_name>/_search
{  
   "query":{  
      "bool":{  
         "filter":{  
            "bool":{  
               "must":[  
                  {  
                     "range":{  
                        "@timestamp":{  
                           "gte":"2019-03-12",
                           "lte":"2019-03-13"
                        }
                     }
                  },
                  {  
                     "match":{  
                        "msg_text":"foo bar"         
                     }
                  }
               ]
            }
         }
      }
   }
}

2.使用查询字符串
POST <your_index_name>/_search
{  
   "query":{  
      "bool":{  
         "filter":{  
            "bool":{  
               "must":[  
                  {  
                     "range":{  
                        "@timestamp":{  
                           "gte":"2019-03-12",
                           "lte":"2019-03-13"
                        }
                     }
                  },
                  {  
                     "query_string":{  
                        "fields": ["msg_text"],    <----- You can add more fields here using comma as delimiter
                        "query":"foo AND bar"
                     }
                  }
               ]
            }
         }
      }
   }
}

从技术上讲,从某种意义上讲,您的解决方案没有问题,这是可行的,但是我希望我的答案清楚,可以简化查询并帮助您了解您要做什么。

让我知道是否有帮助!

关于elasticsearch - 使用ElasticSearch查询进行过滤和匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56246709/

相关文章:

elasticsearch - 对于没有数据的字段,对嵌套 bool 查询返回空结果

elasticsearch - 搜索结果的ElasticSearch阈值

java - 使用@Query 在 ElasticSearch 中设置大小

elasticsearch - 获得所有聚合结果的最有效方法是

elasticsearch - 我可以定义要快照的碎片吗?

kibana - 在 Kibana 中显示 n-greatest

elasticsearch - 判断索引中是否存在术语的最快方法

elasticsearch - 如何处理Elasticsearch脚本除法零错误?

elasticsearch - 5.x中的Elasticsearch索引大小比1.x中的大40%

mysql - Elasticsearch 查询: Sum + Case Equivalent?