编写脚本以从Elasticsearch获得独特的价值

时间:2018-10-31 04:32:50

标签: elasticsearch elasticsearch-painless

我想创建一个自定义的distinct函数,在该函数中,我可以编写具有多个字段名称的简单distincBy脚本,以将它们分隔在一起。因此,无论如何,ElasticSearch都可以实现这一目标。

1 个答案:

答案 0 :(得分:1)

我所做的是利用Terms Aggregation using Script从三个不同字段中构造键,然后在此 concatenated 字段中应用Terms Aggregation给你想要的。

我创建了一个样本索引,其中包含3个字段,(field1field2field3,类型为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的构造方式(键是唯一的)。

让我知道这是否有帮助!