在 Elasticsearch 中,可以使用 bool 查询来实现多条件查询。bool 查询可以包含多个子查询,每个子查询可以是 must、must_not 或 should。其中:

  • must:必须匹配的条件,相当于 AND。
  • must_not:必须不匹配的条件,相当于 NOT。
  • should:可选匹配的条件,相当于 OR。

下面是一个示例,使用 bool 查询实现多条件查询:

POST /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "Elasticsearch" } },
        { "range": { "timestamp": { "gte": "2021-01-01", "lte": "2021-12-31" } } }
      ],
      "must_not": [
        { "term": { "status": "deleted" } }
      ],
      "should": [
        { "match": { "content": "search engine" } },
        { "match": { "author": "John" } }
      ],
      "minimum_should_match": 1
    }
  }
}

这个查询包含以下条件:

  • 必须包含 title 字段匹配 "Elasticsearch"。
  • 必须包含 timestamp 字段在指定时间范围内。
  • 必须不包含 status 字段匹配 "deleted"。
  • 可选包含 content 字段匹配 "search engine" 或 author 字段匹配 "John",但至少要匹配一个。
  • 返回匹配的文档。

可以根据实际需求修改查询条件和参数。

NET core ES 多条件查询

原文地址: https://www.cveoy.top/t/topic/bQv3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录