在ElasticSearch中按日期分组和按日期范围搜索时出现问题

brvekthn  于 2023-08-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(247)

我正在使用ElasticSearch来统计数据,但我遇到了一个问题,使用日期直方图的结果和使用日期范围的结果是不同的。
下面是我的查询和它的结果,使用日期直方图和按日期分组来计算数据。

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "range": {
                  "starttime": {
                    "from": 1686502800000,
                    "to": null,
                    "include_lower": true,
                    "include_upper": true,
                    "boost": 1
                  }
                }
              },
              {
                "range": {
                  "starttime": {
                    "from": null,
                    "to": 1688317199999,
                    "include_lower": true,
                    "include_upper": true,
                    "boost": 1
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "holdCall",
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "aggregations": {
    "group_by_starttime": {
      "date_histogram": {
        "field": "starttime",
        "calendar_interval": "1d",
        "offset": 0,
        "order": {
          "_key": "asc"
        },
        "keyed": false,
        "min_doc_count": 0
      },
      "aggregations": {
        "count_id": {
          "value_count": {
            "field": "id.keyword"
          }
        }
      }
    }
  }
}

个字符
如您在2023/06/25(1687651200000)中所见,结果为25032。
下面是我的查询结果,使用日期范围来计算25/06/2023的数据。

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "range": {
                  "starttime": {
                    "from": 1687626000000,
                    "to": null,
                    "include_lower": true,
                    "include_upper": true,
                    "boost": 1
                  }
                }
              },
              {
                "range": {
                  "starttime": {
                    "from": null,
                    "to": 1687712399999,
                    "include_lower": true,
                    "include_upper": true,
                    "boost": 1
                  }
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "holdCall",
                  "boost": 1
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  },
  "aggregations": {
    "count_id": {
      "value_count": {
        "field": "id.keyword"
      }
    }
  }
}
{
  "count_id": {
    "value": 29803
  }
}

的字符串
如您所见,结果为29803,与使用日期直方图的结果不同。
请问我该如何解决这个问题?我在网上搜索了一下,但什么也没找到。

knsnq2tg

knsnq2tg1#

文件总数的差异是由于指定的时间范围不同造成的。“开始”时间设置为1687626000000,对应于GMT中的“2023年6月24日17:00:00”,而“结束”时间设置为GMT中的“2023年6月25日16:59:59.999”。
使用日期直方图聚合时,由于calendar_interval设置为1天,因此每天都会创建存储桶。因此,创建了两个桶:一个是6月24日,另一个是6月25日。
总计数4771表示6月24日桶内的文档,而计数25032表示6月25日桶内的文档。将这些值加在一起得到总计数29803。
另一方面,当运行没有日期直方图聚合的范围查询时,查询将检索给定时间范围内的所有数据作为一个整体,而不为每一天创建单独的桶。因此,结果计数将不同于聚合计数,因为它不考虑数据的每日细分。

相关问题