gpt4 book ai didi

database - ElasticSearch 术语聚合但需要输出中的其他字段

转载 作者:搜寻专家 更新时间:2023-10-30 23:26:41 25 4
gpt4 key购买 nike

我的 Elastic 搜索文档如下所示。

{
doc_id: 1,
type: "type-1",
user: "user1",
color: "red",
timestamp: epoch time here
},
{
doc_id: 2,
type: "type-2",
user: "user2",
color: "blue",
timestamp: epoch time here
},
{
doc_id: 3,
type: "type-3",
user: "user3",
color: "red",
timestamp: epoch time here
},

每种颜色可以有多个文档。我的要求是根据时间戳获取每种颜色的最新文档,因此每种颜色(红色、蓝色等...)最多只有一个文档。最后,我想过滤掉那些时间戳超过 2 天的带有颜色的文档。所以这最后一步进一步过滤掉颜色。我请求帮助使用 Elastic Search 执行此操作。

最佳答案

映射:

{
"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" : [ ]
}
}
}
]
}
}
]
}
}

关于database - ElasticSearch 术语聚合但需要输出中的其他字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56263929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com