代码之家  ›  专栏  ›  技术社区  ›  Dinosaurius

如何在elasticsearch中执行特定的搜索查询?

  •  1
  • Dinosaurius  · 技术社区  · 8 年前

    我有以下索引 mytestindex mytesttype 在Elasticsearch 5.5中:

    PUT /mytestindex
    {
        "mappings": {
          "mytesttype": {
          "_source": {
            "enabled": true
          },
          "_all": {
            "enabled": true
          },
            "properties": {
              "Id": {
                "type":"text"
              },
              "RadarId": {
                "type":"keyword"
              },
              "VehicleId": {
                "type":"keyword"
              },
              "Datetime": {
                "type":"date",
            "format": "yyyy-MM-dd HH:mm:ss.SSS"
              },
              "Hour": {
                "type":"integer"
              }
           }
        }
      }
    }
    

    我是Elasticsearch的初学者。我想知道是否有可能在11:00到12:00之间获得每小时平均车辆数?我应该使用哪种类型的查询?例如,在MySQL中,我会这样做(简化版,不考虑11:00-12:00):

    SELECT    AVG( value ), datetime
    FROM      mytable
    GROUP BY  HOUR( datetime )
    

    是否可以在Elasticsearch中执行类似的查询?

    1 回复  |  直到 8 年前
        1
  •  0
  •   Amityo    8 年前

    我不太明白你是否对每小时的元素数或它们的平均值感兴趣? 如果需要元素的数量,可以使用 Date Range Aggregation

    {
    "aggs": {
        "range": {
            "date_range": {
                "field": "Datetime",
                "format": "yyyy-MM-dd HH:mm:ss.SSS",
                "ranges": [
                    { "to": "now-1H/H" }, 
                    { "from": "now-1H/H" } 
                ]
            }
        }
    }
    }