elasticsearch查詢模板


最近在公司又用到了elasticsearch,也用到了查詢模板,順便寫篇文章記錄一下查詢模板的使用。

以1個需求為例講解es模板的使用:

頁面上某個按鈕在一段時間內的點擊次數統計,並且可以以小時,天,月為單位進行匯總,並且需要去重。

創建索引,只定義3個字段,user_id, user_name和create_time:

-POST /$ES/event_index

{
  "mappings": {
    "event": {
      "_ttl": {
        "enabled": false
      },
      "_timestamp": {
        "enabled": true,
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "properties": {
        "user_id": {
          "type": "string",
          "store": "no",
          "index": "not_analyzed"
        },
        "create_time": {
          "type": "date",
          "store": "no",
          "index": "not_analyzed",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "user_name": {
          "type": "string",
          "store": "no"
        }
      }
    }
  }
}

定義對應的查詢模板,模板名字stats,使用了Cardinality和DateHistogram這兩個Aggregation
,其中Date Histogram嵌套在Cardinality里。在定義模板的時候,{ { } } 的表示是個參數,需要調用模板的時候傳遞進來:

  -POST /$ES/_search/template/stats
{
    "template": {
        "query": {
            "bool": {
                "must": [
                    {
                        "range": {
                            "create_time": {
                                "gte": "{{earliest}}",
                                "lte": "{{latest}}"
                            }
                        }
                    }
                ]
            }
        },
        "size": 0,
        "aggs": {
            "stats_data": {
                "date_histogram": {
                    "field": "create_time",
                    "interval": "{{interval}}"
                },
                "aggs": {
                    "time": {
                        "cardinality": {
                            "field": "user_id"
                        }
                    }
                }
            }
        }
    }
}

Cardinality Aggregation的作用就是類似sql中的distinct,去重。

Date Histogram Aggregation的作用是根據時間進行統計。內部有個interval屬性表面統計的范疇。

下面加幾條數據到event_index里:

-POST $ES/event_index/event
{
    "user_id": "1",
    "user_name": "format1",
    "create_time": "2015-11-07 12:00:00"
}

-POST $ES/event_index/event
{
    "user_id": "2",
    "user_name": "format2",
    "create_time": "2015-11-07 13:30:00"
}

-POST $ES/event_index/event
{
    "user_id": "3",
    "user_name": "format3",
    "create_time": "2015-11-07 13:30:00"
}

-POST $ES/event_index/event
{
    "user_id": "1",
    "user_name": "format1",
    "create_time": "2015-11-07 13:50:00"
}

-POST $ES/event_index/event
{
    "user_id": "1",
    "user_name": "format1",
    "create_time": "2015-11-07 13:55:00"
}

11-07 12-13點有1條數據,1個用戶
11-07 13-14點有4條數據,3個用戶

使用模板查詢:

curl -XGET "$ES/event_index/_search/template" -d'{
  "template": { "id": "stats" }, 
  "params": { "earliest": "2015-11-07 00:00:00", "latest": "2015-11-07 23:59:59", "interval": "hour" }
}'    

結果:

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "stats_data": {
            "buckets": [
                {
                    "key_as_string": "2015-11-07 12:00:00",
                    "key": 1446897600000,
                    "doc_count": 1,
                    "time": {
                        "value": 1
                    }
                },
                {
                    "key_as_string": "2015-11-07 13:00:00",
                    "key": 1446901200000,
                    "doc_count": 4,
                    "time": {
                        "value": 3
                    }
                }
            ]
        }
    }
}

12點-13點的只有1條數據,1個用戶。13-14點的有4條數據,3個用戶。

以天(day)統計:

curl -XGET "$ES/event_index/_search/template" -d'{
  "template": { "id": "stats" }, 
  "params": { "earliest": "2015-11-07 00:00:00", "latest": "2015-11-07 23:59:59", "interval": "day" }
}'    

結果:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "stats_data": {
            "buckets": [
                {
                    "key_as_string": "2015-11-07 00:00:00",
                    "key": 1446854400000,
                    "doc_count": 5,
                    "time": {
                        "value": 3
                    }
                }
            ]
        }
    }
}

11-07這一天有5條數據,3個用戶。

本文只是簡單說明了es查詢模板的使用,也簡單使用了2個aggregation。更多內容可以去官網查看相關資料。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM