Elasticsearch聚合是否将空值作为0返回?

ttvkxqim  于 2022-11-02  发布在  ElasticSearch
关注(0)|答案(2)|浏览(212)

我可以用这段代码计算每天匹配查询字符串的点击数,但是如果一整周都没有点击数,那么查询将不返回任何结果--而不是每天返回0。有没有办法可以“默认”为0?

GET index/_search
    {
  "size": 0,
  "query": {
    "bool": {
      "must": [
          {"match_phrase": {
            "message": "Cannot login"
            }
          },
          {"range": {
            "@timestamp":{
              "gte":"2021-07-01",
              "lte":"2021-07-07"
            }
          }
        }
      ]
    }
  }, 
  "aggs": {
    "hit_count_per_day": {
      "date_histogram": {
        "field": "@timestamp",
        "calendar_interval": "day"
      }
    }
  } 
}
7rtdyuoh

7rtdyuoh1#

对于此建议,您需要将extended_bounds添加到聚合中,如下所示:

GET index/_search
    {
  "size": 0,
  "query": {
    "bool": {
      "must": [
          {"match_phrase": {
            "message": "Cannot login"
            }
          },
          {"range": {
            "@timestamp":{
              "gte":"2021-07-01",
              "lte":"2021-07-07"
            }
          }
        }
      ]
    }
  }, 
  "aggs": {
    "hit_count_per_day": {
      "date_histogram": {
        "field": "@timestamp",
        "calendar_interval": "day",
        "extended_bounds": {
          "min": "2021-07-01",
          "max": "2021-07-07"
        }
      }
    }
  } 
}

如果它不能解决您的问题,请告诉我。

um6iljoc

um6iljoc2#

当聚合响应中的elasticsearch返回0时,表示由于筛选器的原因,有值未显示。
例如,我有一个关于“region”的术语过滤器:“mexico”,它返回给我:

  • “哥伦比亚”:0
  • “阿根廷”:0
  • “墨西哥”:7

前两个是因为它们在数据集(索引)中,但经过筛选。
希望能有所帮助。

相关问题