编写脚本以从 Elasticsearch 获取 Distinct 值
Script writing to get Distinct value from Elasticsearch
我想创建一个自定义的 distinct 函数,我可以在其中编写多个字段名称的简单 distinctBy 脚本,以便一起区分。那么 ElasticSearch 中是否有实现此目的的方法。
我所做的是在这个 concatenated 字段上使用 Terms Aggregation using Script to construct keys from three different fields and then apply Terms Aggregation 来提供您想要的内容。
我创建了一个包含 3 个字段(field1
、field2
和 field3
类型 keyword
)的示例索引,文档如下。您可以检查查询和结果部分以查看它们的显示方式。关键是结果部分的 keys
是不同的。
示例文档
POST myfieldindex/mydocs/1
{
"field1": "Football",
"field2": "Premier League",
"field3": "Chelsea"
}
POST myfieldindex/mydocs/3
{
"field1": "Football",
"field2": "Premier League",
"field3": "Liverpool"
}
POST myfieldindex/mydocs/3
{
"field1": "Football",
"field2": "Premier League",
"field3": "ManCity"
}
查询
POST myfieldindex/_search
{
"size":0,
"aggs":{
"myagg":{
"terms":{
"script":{
"source":"doc['field1'].value + params.param + doc['field2'].value + params.param + doc['field3'].value",
"lang":"painless",
"params":{
"param":","
}
}
}
}
}
}
查询结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"myagg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Football,Premier League,Chelsea",
"doc_count": 1
},
{
"key": "Football,Premier League,Liverpool",
"doc_count": 1
},
{
"key": "Football,Premier League,ManCity",
"doc_count": 1
}
]
}
}
}
因此您可以在结果中看到 key
是如何构造的(键是唯一的)。
如果有帮助请告诉我!
我想创建一个自定义的 distinct 函数,我可以在其中编写多个字段名称的简单 distinctBy 脚本,以便一起区分。那么 ElasticSearch 中是否有实现此目的的方法。
我所做的是在这个 concatenated 字段上使用 Terms Aggregation using Script to construct keys from three different fields and then apply Terms Aggregation 来提供您想要的内容。
我创建了一个包含 3 个字段(field1
、field2
和 field3
类型 keyword
)的示例索引,文档如下。您可以检查查询和结果部分以查看它们的显示方式。关键是结果部分的 keys
是不同的。
示例文档
POST myfieldindex/mydocs/1
{
"field1": "Football",
"field2": "Premier League",
"field3": "Chelsea"
}
POST myfieldindex/mydocs/3
{
"field1": "Football",
"field2": "Premier League",
"field3": "Liverpool"
}
POST myfieldindex/mydocs/3
{
"field1": "Football",
"field2": "Premier League",
"field3": "ManCity"
}
查询
POST myfieldindex/_search
{
"size":0,
"aggs":{
"myagg":{
"terms":{
"script":{
"source":"doc['field1'].value + params.param + doc['field2'].value + params.param + doc['field3'].value",
"lang":"painless",
"params":{
"param":","
}
}
}
}
}
}
查询结果
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0,
"hits": []
},
"aggregations": {
"myagg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Football,Premier League,Chelsea",
"doc_count": 1
},
{
"key": "Football,Premier League,Liverpool",
"doc_count": 1
},
{
"key": "Football,Premier League,ManCity",
"doc_count": 1
}
]
}
}
}
因此您可以在结果中看到 key
是如何构造的(键是唯一的)。
如果有帮助请告诉我!