Groupの中からソートした情報を取得するクエリについて

【ESバージョン】
v6.2.4
【kibanaバージョン】
6.2.4

【目的】
以下のようなindexがあったとき、条件に該当するdocumentを取得したい。
条件「それぞれのuserの最終行動datetime」

index:action_logs
+----+----------+----------------------+-------------+
| id | user_id | action_datetime | action_type |
+----+----------+----------------------+-------------+
| 1 | 1 | 2015-03-28 11:22:33 | AA |
| 2 | 2 | 2015-03-30 14:12:47 | BA |
| 3 | 1 | 2015-03-31 13:10:05 | CD |
| 4 | 3 | 2015-04-01 10:23:04 | CC |
| 5 | 3 | 2015-04-01 19:56:34 | BB |
| 6 | 1 | 2015-04-02 08:31:24 | AB |
| 7 | 2 | 2015-04-03 21:58:07 | BD |
| 8 | 2 | 2015-04-04 23:10:19 | DA |
+----+----------+----------------------+-------------+

【期待値】
| 5 | 3 | 2015-04-01 19:56:34 | BB |
| 6 | 1 | 2015-04-02 08:31:24 | AB |
| 8 | 2 | 2015-04-04 23:10:19 | DA |

【質問】
action_logsから期待値が取得できるようなESへのクエリについてご教示お願いします。

吉岡です。

こんな感じでいかがでしょうか。

データ投入と検索

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
                  ]
                }
              ]
            }
          }
        }
      ]
    }
  }
}
1 Like

吉岡様
返信が遅れ、大変申し訳ありません。
こちらが期待した動作が確認できました。
ご回答いただき誠にありがとうございました。

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.