【问题标题】:Elasticsearch composite query and aggregation results different doc_count valueElasticsearch复合查询和聚合结果不同的doc_count值
【发布时间】:2021-07-04 23:04:53
【问题描述】:

我正在尝试使用复合查询来查询我的数据集。 这是我的

查询 1:

curl -X POST "localhost:9200/index1-202103/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
{
  "query":{
    "bool":{
      "filter":[
        {
          "range":{
            "date":{
              "gte":"20210330",
              "lte":"20210330"
            }
          }
        },
        {
          "term":{
            "userid":"16114"
          }
        },
        {
          "exists":{
            "field":"opens"
          }
        },
        {
          "exists":{
            "field":"tags"
          }
        }
      ]
    }
  },
  "aggs":{
    "my_buckets":{
      "composite":{
        "sources":[
          {
            "from_domain_wise":{
              "terms":{
                "field":"domain"
              }
            }
          },
          {
            "msp_wise":{
              "terms":{
                "field":"msp"
              }
            }
          },
          {
            "fromaddress_wise":{
              "terms":{
                "field":"fromaddress"
              }
            }
          },
          {
            "tag_wise":{
              "terms":{
                "field":"tags"
              }
            }
          },
          {
            "rate_over_time":{
              "date_histogram":{
                "field":"opens.time",
                "interval":"1h"
              }
            }
          }
        ]
      }
    }
  }
}'

查询 2

curl -X POST "localhost:9200/index1-202103/_search?size=0&pretty" -H 'Content-Type: application/json' -d'
{
  "query":{
    "bool":{
      "filter":[
        {
          "range":{
            "date":{
              "gte":"20210330",
              "lte":"20210330"
            }
          }
        },
        {
          "term":{
            "userid":"16114"
          }
        },
        {
          "exists":{
            "field":"opens"
          }
        },
        {
          "exists":{
            "field":"tags"
          }
        }
      ]
    }
  },
  "aggs":{
    "my_buckets":{
      "composite":{
        "sources":[
          {
            "from_domain_wise":{
              "terms":{
                "field":"domain"
              }
            }
          },
          {
            "msp_wise":{
              "terms":{
                "field":"msp"
              }
            }
          },
          {
            "fromaddress_wise":{
              "terms":{
                "field":"fromaddress"
              }
            }
          },
          {
            "tag_wise":{
              "terms":{
                "field":"tags"
              }
            }
          }
        ]
      },
      "aggs":{
        "rate_over_time":{
          "date_histogram":{
            "field":"opens.time",
            "interval":"1h"
          }
        }
      }
    }
  }
}'

两个结果都给出了具有不同计数的日期直方图的输出。当我检查时,我的发现是 Query1 正在计算 opens.time (FORMAT: 2021-03-30 15:15:45) 字段重复值,而 Query2 只计算 opens.time 一次,如果小时在单个文档中相同。

例如:如果 doc 包含打开:[{ "time": "2021-03-30 15:20:25" }, { "time": "2021-03-30 15:45:30" }],则 Query1 将 doc_count 作为 2 返回,而 Query2 将 doc_count 作为 1 返回。

任何人都可以解释为什么我的查询会出现这样的行为,尽管这两个查询具有相同的目标。我想要 Query2 使用 Query1 给出的结果。

PS: Elasticsearch 版本是 7.10

【问题讨论】:

    标签: elasticsearch elastic-stack elasticsearch-aggregation elasticsearch-query


    【解决方案1】:

    这两个查询都“有目标”,但请注意您在哪里应用 date_histogram

    在第一个查询中,它用作composite sub-aggregation,在第二个查询中用作composite value source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-22
      • 2018-12-11
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 2016-12-02
      • 2019-10-09
      • 2016-08-14
      相关资源
      最近更新 更多