elasticsearch - 如何使用 Elasticsearch 过滤最后 5 分钟的日期直方图?

标签 elasticsearch aggregation date-range

使用 Elasticsearch 1.1.1

我正在尝试为所有帐户构建一个“页面”每秒浏览量查询(因此匹配所有帐户)。

映射是...

"xxx-20140526": {
    "mappings": {
      "xxx": {
        "properties": {
          "accountId": {
            "type": "long"
          },
          "hitTime": {
            "type": "date",
            "format": "dateOptionalTime"
          },
        }
      }
    }
  }

查询...

POST /xxx-20140526/xxx/_search
{
  "filter": {
    "range": {
      "timeHit": {
        "gte": "2014-05-26T13:40", //Date generated dynamically now - 5mins
        "lt": "2014-05-26T13:45" //Date generated dynamically now
      }
    }
  },
  "aggs": {
    "views_per_sec": {
      "date_histogram": {
        "field": "timeHit",
        "interval": "second"
      }
    }
  }
}

但是聚合也会返回以前的值...

"aggregations": {
    "trx_per_sec": {
        "buckets": [
        {
            "key_as_string": "2014-05-26T13:36:46.000Z",
            "key": 1401111166000,
            "doc_count": 72
        },
        ... Other dates in the 30 mins range here...
        {
           "key_as_string": "2014-05-26T13:42:47.000Z",
           "key": 1401111167000,
           "doc_count": 5013
        }
     }
}

1- 聚合是否考虑过滤器? 2- 过滤最后 5 分钟的方法是否正确,还是我应该查看日期聚合?

我也试过...

{
  "aggs": {
    "range": {
      "date_range": {
        "field": "timeHit",
        "format": "yyyy-MM-dd HH:mm:ss",
        "ranges": [
          {
            "from": "now-5m"
          }
        ]
      }
    }
  }
}

但这似乎没有返回正确数量的文档。

最佳答案

好的,我让它在这里工作是查询...

{
  "size": 0, <--- Size zero. Don't return any docs we only care about the aggregation.
  "aggs": {
    "last_5_mins": {
      "filter": {
        "range": {
          "hitTime": {
            "gte": "now-5m",
            "lte": "now"
          }
        }
      },
      "aggs": {
        "tps": {
          "date_histogram": {
            "field": "hitTime",
            "interval": "second"
          }
        }
      }
    }
  }
}

关于elasticsearch - 如何使用 Elasticsearch 过滤最后 5 分钟的日期直方图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23872135/

相关文章:

mysql - 酒店日期过滤器 : return only hotels that respect minimum stay period specified for range (SQL or Elasticsearch)

elasticsearch无法解析elasticsearch.yml中的环境变量

python - 如何使 Wagtail 搜索不区分大小写

php - 日期范围搜索

c# - 在Elasticsearch中跨多个地址字段搜索

sorting - elasticsearch 聚合对存储桶键进行排序

arrays - elasticsearch将数据转换为数组

mysql - 处理/压缩多个表中的大型数据集

iphone - 从 NSArray 获取特定范围

MYSQL员工在日期范围内每天的工作时间