吉岡です。
こんな感じでいかがでしょうか。
データ投入と検索
DELETE sample
PUT sample
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 0
},
"mappings": {
"_doc": {
"properties": {
"user_id": {
"type": "keyword"
},
"action_datetime": {
"type": "date",
"format":"yyyy-MM-dd HH:mm:ss"
},
"action_type": {
"type": "keyword"
}
}
}
}
}
PUT sample/_doc/_bulk
{"index":{"_id":"1"}}
{"user_id":"1","action_datetime":"2015-03-28 11:22:33","action_type":"AA"}
{"index":{"_id":"2"}}
{"user_id":"2","action_datetime":"2015-03-30 14:12:47","action_type":"BA"}
{"index":{"_id":"3"}}
{"user_id":"1","action_datetime":"2015-03-31 13:10:05","action_type":"CD"}
{"index":{"_id":"4"}}
{"user_id":"3","action_datetime":"2015-04-01 10:23:04","action_type":"CC"}
{"index":{"_id":"5"}}
{"user_id":"3","action_datetime":"2015-04-01 19:56:34","action_type":"BB"}
{"index":{"_id":"6"}}
{"user_id":"1","action_datetime":"2015-04-02 08:31:24","action_type":"AB"}
{"index":{"_id":"7"}}
{"user_id":"2","action_datetime":"2015-04-03 21:58:07","action_type":"BD"}
{"index":{"_id":"8"}}
{"user_id":"2","action_datetime":"2015-04-04 23:10:19","action_type":"DA"}
GET sample/_search
{
"size": 0,
"aggs": {
"user_id": {
"terms": {
"field": "user_id",
"size": 10
},
"aggs": {
"latest": {
"top_hits": {
"size": 1,
"sort": [
{
"action_datetime": {
"order": "desc"
}
}
]
}
}
}
}
}
}
レスポンス
{
"took" : 143,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 8,
"max_score" : 0.0,
"hits" : [ ]
},
"aggregations" : {
"user_id" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "1",
"doc_count" : 3,
"latest" : {
"hits" : {
"total" : 3,
"max_score" : null,
"hits" : [
{
"_index" : "sample",
"_type" : "_doc",
"_id" : "6",
"_score" : null,
"_source" : {
"user_id" : "1",
"action_datetime" : "2015-04-02 08:31:24",
"action_type" : "AB"
},
"sort" : [
1427963484000
]
}
]
}
}
},
{
"key" : "2",
"doc_count" : 3,
"latest" : {
"hits" : {
"total" : 3,
"max_score" : null,
"hits" : [
{
"_index" : "sample",
"_type" : "_doc",
"_id" : "8",
"_score" : null,
"_source" : {
"user_id" : "2",
"action_datetime" : "2015-04-04 23:10:19",
"action_type" : "DA"
},
"sort" : [
1428189019000
]
}
]
}
}
},
{
"key" : "3",
"doc_count" : 2,
"latest" : {
"hits" : {
"total" : 2,
"max_score" : null,
"hits" : [
{
"_index" : "sample",
"_type" : "_doc",
"_id" : "5",
"_score" : null,
"_source" : {
"user_id" : "3",
"action_datetime" : "2015-04-01 19:56:34",
"action_type" : "BB"
},
"sort" : [
1427918194000
]
}
]
}
}
}
]
}
}
}