Отображение:
{
"textindex" : {
"mappings" : {
"properties" : {
"color" : {
"type" : "keyword",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"doc_id" : {
"type" : "long"
},
"timestamp" : {
"type" : "date"
},
"type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"user" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
Данные:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "textindex",
"_type" : "_doc",
"_id" : "kN_D4moBLcHQX6h0M1GZ",
"_score" : 1.0,
"_source" : {
"doc_id" : 1,
"type" : "type-1",
"user" : "user1",
"color" : "red",
"timestamp" : "2019-05-23"
}
},
{
"_index" : "textindex",
"_type" : "_doc",
"_id" : "kd_D4moBLcHQX6h0PFHp",
"_score" : 1.0,
"_source" : {
"doc_id" : 2,
"type" : "type-2",
"user" : "user2",
"color" : "blue",
"timestamp" : "2019-05-23"
}
},
{
"_index" : "textindex",
"_type" : "_doc",
"_id" : "kt_D4moBLcHQX6h0QlHi",
"_score" : 1.0,
"_source" : {
"doc_id" : 3,
"type" : "type-3",
"user" : "user3",
"color" : "red",
"timestamp" : "2019-05-22"
}
},
{
"_index" : "textindex",
"_type" : "_doc",
"_id" : "k9_I4moBLcHQX6h0M1GF",
"_score" : 1.0,
"_source" : {
"doc_id" : 4,
"type" : "type-4",
"user" : "user4",
"color" : "yello",
"timestamp" : "2019-05-20"
}
}
]
}
}
Запрос: - Я использую "top_hits" для выборки верхней записи, отсортированной по отметке времени в порядке убывания
GET textindex/_search
{
"size": 0,
"aggs": {
"top_color": {
"terms": {
"field": "color"
},
"aggs": {
"discard_old_dates": {
"date_range": {
"field": "timestamp",
"ranges": [
{
"from": "now-2d/d",
"to": "now"
}
]
},
"aggs": {
"top_team_hits": {
"top_hits": {
"sort": [
{
"timestamp": {
"order": "desc"
}
}
],
"_source": {
"include": [
"doc_id",
"type",
"user",
"color",
"timestamp"
]
},
"from": 0,
"size": 1
}
}
}
}
}
}
}
}
Результат:
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 4,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
},
"aggregations" : {
"top_color" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : "red",
"doc_count" : 2,
"discard_old_dates" : {
"buckets" : [
{
"key" : "2019-05-21T00:00:00.000Z-2019-05-23T03:55:29.019Z",
"from" : 1.5583968E12,
"from_as_string" : "2019-05-21T00:00:00.000Z",
"to" : 1.558583729019E12,
"to_as_string" : "2019-05-23T03:55:29.019Z",
"doc_count" : 2,
"top_team_hits" : {
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "textindex",
"_type" : "_doc",
"_id" : "kN_D4moBLcHQX6h0M1GZ",
"_score" : null,
"_source" : {
"color" : "red",
"type" : "type-1",
"doc_id" : 1,
"user" : "user1",
"timestamp" : "2019-05-23"
},
"sort" : [
1558569600000
]
}
]
}
}
}
]
}
},
{
"key" : "blue",
"doc_count" : 1,
"discard_old_dates" : {
"buckets" : [
{
"key" : "2019-05-21T00:00:00.000Z-2019-05-23T03:55:29.019Z",
"from" : 1.5583968E12,
"from_as_string" : "2019-05-21T00:00:00.000Z",
"to" : 1.558583729019E12,
"to_as_string" : "2019-05-23T03:55:29.019Z",
"doc_count" : 1,
"top_team_hits" : {
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : null,
"hits" : [
{
"_index" : "textindex",
"_type" : "_doc",
"_id" : "kd_D4moBLcHQX6h0PFHp",
"_score" : null,
"_source" : {
"color" : "blue",
"type" : "type-2",
"doc_id" : 2,
"user" : "user2",
"timestamp" : "2019-05-23"
},
"sort" : [
1558569600000
]
}
]
}
}
}
]
}
},
{
"key" : "yello",
"doc_count" : 1,
"discard_old_dates" : {
"buckets" : [
{
"key" : "2019-05-21T00:00:00.000Z-2019-05-23T03:55:29.019Z",
"from" : 1.5583968E12,
"from_as_string" : "2019-05-21T00:00:00.000Z",
"to" : 1.558583729019E12,
"to_as_string" : "2019-05-23T03:55:29.019Z",
"doc_count" : 0,
"top_team_hits" : {
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
}
]
}
}
]
}
}