我想创建一个自定义的distinct函数,在该函数中,我可以编写具有多个字段名称的简单distincBy脚本,以将它们分隔在一起。因此,无论如何,ElasticSearch都可以实现这一目标。
答案 0 :(得分:1)
我所做的是利用Terms Aggregation using Script从三个不同字段中构造键,然后在此 concatenated 字段中应用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
的构造方式(键是唯一的)。
让我知道这是否有帮助!