我想创建一个自定义的distinct函数,在该函数中,我可以编写具有多个字段名称的简单distincBy脚本,以将它们分隔在一起。无论如何,ElasticSearch中有实现此目标的方法。
最佳答案
我所做的是利用Terms Aggregation using Script从三个不同的字段构造键,然后将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
的构造方式(键是唯一的)。让我知道这是否有帮助!
关于elasticsearch - 编写脚本以从Elasticsearch获得独特的值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076349/