elasticsearch - ElasticSearch搜索查询中的多个条件

标签 elasticsearch

我需要使用以下规则集来构建搜索查询。

  • 获取列/节点处于 Activity 状态的所有数据= true
  • 获取列/节点active = false且已存在一周的的所有数据。

  • 例如,如果我在索引中有1000条记录,其中70%的记录具有active = true,而30%的记录具有active = false。在30%的记录中,有10%最近更新。所以我只需要获取记录的80%,即70%(active = true)+ 10%(active = false)。

    我尝试使用过滤器查询,但它只返回结果的10%,即仅满足规则2,即使我在应该阻止的规则中提到了规则1也不会接受。
       {
      "query": {
        "bool": {
          "filter": {
            "bool": {
              "must": [
                {
                  "range": {
                    "UpdatedOn": {
                      "gte": "now-7d/d",
                      "lte": "now/d"
                    }
                  }
                },
                {
                  "match": {
                    "Active": "false"
                  }
                }
              ]
            }
          },
          "should": [
            {
              "match": {
                "Active": "true"
              }
            }
          ]
        }
      }
    }
    

    ElasticSearch 5.4版

    提前致谢。

    最佳答案

    您需要这样做:

    {
      "query": {
        "bool": {
          "minimum_should_match": 1,
          "should": [
            {
              "match": {
                "Active": "true"
              }
            },
            {
              "bool": {
                "filter": [
                  {
                    "range": {
                      "UpdatedOn": {
                        "gte": "now-7d/d",
                        "lte": "now/d"
                      }
                    }
                  },
                  {
                    "match": {
                      "Active": "false"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    关于elasticsearch - ElasticSearch搜索查询中的多个条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50969880/

    相关文章:

    xml - 数千个文档(pdf 和/或 xml)的可搜索存档的最佳实践

    database - ElasticSearch如何删除文档(如果字段存在)?

    elasticsearch - 如何在 YAML 文件中安装期间配置 Elasticsearch Index Lifecycle Management (ILM)

    apache - 语法错误,使用fq(过滤查询)时无法从lucene StandardQueryparser进行解析

    elasticsearch - Elasticsearch分析器配置

    java - Elasticsearch - 映射异常

    elasticsearch - 在Elasticsearch中创建数字字段的倒排索引

    http - 在Logstash中按时间戳查询项目

    c# - 将日志重定向到logstash实例而不是ElasticSearch

    elasticsearch - Couchbase-Elasticsearch运输插件。使用_class字段映射到类型