Elasticsearch返回字段的唯一值

4
我正在尝试构建一个Elasticsearch查询,只返回特定字段的唯一值。
我不想返回该字段的所有值或计数。
例如,如果该字段当前包含50个不同的值,并且我进行搜索以仅返回20个命中项(size=20),我希望每个结果都具有该字段的唯一结果,但我不关心结果中未表示的其他30个值。
例如,使用以下搜索(伪代码 - 未经检查):
{
    from: 0,
    size: 20,
    query: {
        bool: {
            must: {
                range: { field1: { gte: 50 }},
                term: { field2: 'salt' },

                /**
                * I want to return only unique values for "field3", but I
                * don't want to return all of them or count them.
                *
                * How do I specify this in my query?
                **/
                unique: 'field3',
            },
            mustnot: {
                match: { field4: 'pepper'},
            }
        }
    }
}
2个回答

3
你可以很容易地使用terms aggregation来完成这个操作。

以下是一个例子。我定义了一个简单的索引,其中包含一个字段,该字段具有"index": "not_analyzed",因此我们可以将每个字段的完整文本作为唯一值获取,而不是从标记生成的术语等。

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1
   },
   "mappings": {
      "doc": {
         "properties": {
            "title": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

然后我使用bulk API添加了一些文档。

POST /test_index/_bulk
{"index":{"_index":"test_index","_type":"doc","_id":1}}
{"title":"first doc"}
{"index":{"_index":"test_index","_type":"doc","_id":2}}
{"title":"second doc"}
{"index":{"_index":"test_index","_type":"doc","_id":3}}
{"title":"third doc"}
{"index":{"_index":"test_index","_type":"doc","_id":4}}
{"title":"third doc"}

现在我们可以运行术语聚合:
POST /test_index/_search?search_type=count
{
   "aggs": {
      "unique_vals": {
         "terms": {
            "field": "title"
         }
      }
   }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 4,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "unique_vals": {
         "buckets": [
            {
               "key": "third doc",
               "doc_count": 2
            },
            {
               "key": "first doc",
               "doc_count": 1
            },
            {
               "key": "second doc",
               "doc_count": 1
            }
         ]
      }
   }
}

1
谢谢你的时间Sloan,但我不认为术语“聚合”是我要找的。我不想要所有的值,也不想要计数它们。我只是想确保在搜索返回时,该字段对于该搜索是唯一的。我已经编辑了我的问题,希望能更好地解释清楚。 - Rudolf Vavruch
我不明白为什么计数很重要。如果你不想要计数,那就不要使用它们。你可以使用size参数来指定你想要的结果数量。我认为你不能通过查询获取唯一值;这就是术语聚合的作用。你可以使用facets,但它们基本上是聚合的特例,并且已经被弃用了。 - Sloan Ahrens
这是关于该主题的另一篇文章:https://dev59.com/-V8e5IYBdhLWcg3wssEV - Sloan Ahrens
1
我强调计数,因为我发现许多问题都是使用聚合术语(例如您链接的那个问题)解决的,但没有一个解决了我特定要求的查询,即返回特定字段的唯一值。我想确保读者知道我的问题与那些容易用聚合术语解决的问题不同。 - Rudolf Vavruch
我认为除了聚合/分面之外,没有其他方法可以完成你所要求的内容。但如果你发现其他方法,我会很感兴趣知道。 - Sloan Ahrens

1
我很惊讶 过滤聚合还没有被提出。它可以追溯到ES版本1.3。
过滤聚合类似于常规的过滤查询,但是可以嵌套到聚合链中,以过滤不符合特定条件的文档计数,并仅基于满足查询条件的文档给出子聚合结果。
首先,我们将放置我们的映射。
curl --request PUT \
  --url http://localhost:9200/items \
  --header 'content-type: application/json' \
  --data '{
  "mappings": {
    "item": { 
      "properties": { 
        "field1" :    { "type": "integer"  },
        "field2" :    { "type": "keyword"  },
        "field3" :    { "type": "keyword"  },
        "field4" :    { "type": "keyword"  }
      }
    }
  }
}
'

然后让我们加载一些数据。
curl --request PUT \
  --url http://localhost:9200/items/_bulk \
  --header 'content-type: application/json' \
  --data '{"index":{"_index":"items","_type":"item","_id":1}}
{"field1":50, "field2":["salt", "vinegar"], "field3":["garlic", "onion"], "field4":"paprika"}
{"index":{"_index":"items","_type":"item","_id":2}}
{"field1":40, "field2":["salt", "pepper"], "field3":["onion"]}
{"index":{"_index":"items","_type":"item","_id":3}}
{"field1":100, "field2":["salt", "vinegar"], "field3":["garlic", "chives"], "field4":"pepper"}
{"index":{"_index":"items","_type":"item","_id":4}}
{"field1":90, "field2":["vinegar"], "field3":["chives", "garlic"]}
{"index":{"_index":"items","_type":"item","_id":5}}
{"field1":900, "field2":["salt", "vinegar"], "field3":["garlic", "chives"], "field4":"paprika"}
'

请注意,只有ID为1和5的文档将通过标准,因此我们将对这两个field3数组进行聚合,并且总共有四个值。["大蒜", "韭菜"], ["大蒜", "洋葱"]。还要注意,数据中的field3可以是一个数组或单个值,但我将它们变成数组来说明计数的工作原理。
curl --request POST \
  --url http://localhost:9200/items/item/_search \
  --header 'content-type: application/json' \
  --data '{
    "size": 0,
    "aggregations": {
        "top_filter_agg" : {
            "filter" : { 
                "bool": { 
                    "must":[
                        {
                            "range" : { "field1" : { "gte":50} }
                        },
                        {
                            "term" : { "field2" : "salt" }
                        }
                    ],
                    "must_not":[
                        {
                            "term" : { "field4" : "pepper" }
                        }
                    ]
                } 

            },
            "aggs" : {
                "field3_terms_agg" : { "terms" : { "field" : "field3" } }
            }
        }
    }
}
'

在运行连接的过滤器/terms聚合后,我们只有一个关于field3的4个术语计数和总共三个唯一术语。
{
    "took": 46,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 5,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "top_filter_agg": {
            "doc_count": 2,
            "field3_terms_agg": {
                "doc_count_error_upper_bound": 0,
                "sum_other_doc_count": 0,
                "buckets": [
                    {
                        "key": "garlic",
                        "doc_count": 2
                    },
                    {
                        "key": "chives",
                        "doc_count": 1
                    },
                    {
                        "key": "onion",
                        "doc_count": 1
                    }
                ]
            }
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接