# 多元化的采样器聚集(Diversified Sampler Aggregation)

> 这个功能是实验性的，可以在未来的版本中完全改变或删除。 Elastic 将采取最大的努力来解决任何问题,但实验功能不受SLA
>
> 官方GA特性的支持。

像“sampler”聚合一样，这是一个过滤聚合，用于将任何子聚合的处理限制为最高评分文档的样本。diversified\_sampler聚合增加了限制共享一个值的匹配数量的能力，例如“author”。

> 任何好的市场研究人员都会告诉你，在使用数据样本时，重要的是样本表示健康的意见，而不是任何单一的声音。 聚合和采样同样如此，通过这些多样化设置可以提供一种方法来消除您内容中的偏见（人口过多的地理位置，时间轴上的大量峰值或过多的垃圾邮件发送者）。

## Example use cases: <a href="#diversifiedsampleraggregation-exampleusecases" id="diversifiedsampleraggregation-exampleusecases"></a>

* 将分析的重点放在高度相关性匹配上,而不是低质量的长尾词的匹配上
* 通过确保来自不同来源的内容的公平表示来消除分析中的偏见
* Reducing the running cost of aggregations that can produce useful results using only samples e.g.&#x20;

  `significant_terms`

`field或script设置的选择用于提供用于重复数据删除的值，而max_docs_per_value设置控制在任何共享一个公共值的shard上所收集的文档的最大数量。max_docs_per_value的默认设置为1`

如果字段或脚本的选择为单个文档生成多个值，则聚合将抛出错误（由于效率问题，不支持使用多值字段的重复数据删除）。

例子：

我们可能会想看看哪些标签在StackOverflow论坛上有很强的关联，但忽略了一些多数用户的拼写错误， 他们倾向把#Kibana误拼写为#Cabana。

```
POST /stackoverflow/_search?size=0
{
    "query": {
        "query_string": {
            "query": "tags:elasticsearch"
        }
    },
    "aggs": {
        "my_unbiased_sample": {
            "diversified_sampler": {
                "shard_size": 200,
                "field" : "author"
            },
            "aggs": {
                "keywords": {
                    "significant_terms": {
                        "field": "tags",
                        "exclude": ["elasticsearch"]
                    }
                }
            }
        }
    }
}
```

返回：

```
{
    ...
    "aggregations": {
        "my_unbiased_sample": {
            "doc_count": 1000,  ＃1
            "keywords": { ＃2
                "doc_count": 1000,
                "buckets": [
                    {
                        "key": "kibana",
                        "doc_count": 150,
                        "score": 2.213,
                        "bg_count": 200
                    }
                ]
            }
        }
    }
}
```

＃1 因为我们从5个碎片的索引中询问了最多200个文件，所以共采集了1000份文件。 因此，执行嵌套的significant\_terms聚合的成本是有限的，而不是无限制的。

＃2 significant\_terms聚合结果不会被任何一个作者的怪癖所扭曲，因为我们在我们的示例中要求从任何一个作者那里获得最多的一个帖子

### Scripted example: <a href="#diversifiedsampleraggregation-scriptedexample" id="diversifiedsampleraggregation-scriptedexample"></a>

在这种情况下，我们可能希望将字段值的组合多样化。我们可以使用一个script来产生一个标签字段中的多个值的散列，以确保我们没有由相同的重复标签组合组成的样本。

```
POST /stackoverflow/_search?size=0
{
    "query": {
        "query_string": {
            "query": "tags:kibana"
        }
    },
    "aggs": {
        "my_unbiased_sample": {
            "diversified_sampler": {
                "shard_size": 200,
                "max_docs_per_value" : 3,
                "script" : {
                    "lang": "painless",
                    "inline": "doc['tags'].values.hashCode()"
                }
            },
            "aggs": {
                "keywords": {
                    "significant_terms": {
                        "field": "tags",
                        "exclude": ["kibana"]
                    }
                }
            }
        }
    }
}
```

结果：

```
{
    ...
    "aggregations": {
        "my_unbiased_sample": {
            "doc_count": 1000,
            "keywords": {
                "doc_count": 1000,
                "buckets": [
                    {
                        "key": "logstash",
                        "doc_count": 3,
                        "score": 2.213,
                        "bg_count": 50
                    },
                    {
                        "key": "elasticsearch",
                        "doc_count": 3,
                        "score": 1.34,
                        "bg_count": 200
                    },
                ]
            }
        }
    }
}
```

### shard\_size <a href="#diversifiedsampleraggregation-shard_size" id="diversifiedsampleraggregation-shard_size"></a>

shard\_size参数限制在每个分片处理的样本中收集的顶级评分文档数量。 默认值为100。

### max\_docs\_per\_value <a href="#diversifiedsampleraggregation-max_docs_per_value" id="diversifiedsampleraggregation-max_docs_per_value"></a>

max\_docs\_per\_value是一个可选参数，并限制每个选择de-duplicating值允许的文档数量。 默认设置为“1”。

### execution\_hint <a href="#diversifiedsampleraggregation-execution_hint" id="diversifiedsampleraggregation-execution_hint"></a>

可选的execution\_hint设置可以影响用于de-duplication的管理，每个选项在执行de-duplication时都会保持内存中的shard\_size值，但是可以如下控制持有的值的类型：

* 直接保存字段值(map)
* 根据Lucene索引(global\_ordinals)确定字段的序名
* 保持字段值的哈希值 - 具有哈希冲突的可能性（bytes\_hash）

默认设置是使用global\_ordinals，如果该信息可以从Lucene索引中获得，并返回到map。在某些情况下，bytes\_hash设置可能会更快一些，但由于哈希冲突的可能性，引入了de-duplication逻辑中的错误肯定的可能性。 请注意，Elasticsearch将忽略执行提示的选择，如果不适用，并且这些提示没有向后兼容性保证。

### Limitations <a href="#diversifiedsampleraggregation-limitations" id="diversifiedsampleraggregation-limitations"></a>

#### Cannot be nested under `breadth_first` aggregations <a href="#diversifiedsampleraggregation-cannotbenestedunderbreadth_firstaggregations" id="diversifiedsampleraggregation-cannotbenestedunderbreadth_firstaggregations"></a>

作为基于质量的过滤器，diversified\_sampler聚合需要访问为每个文档生成的相关性分数。因此，它不能嵌套在将默认depth\_first模式切换到breadth\_first的collect\_mode的条件聚合，因为这丢弃了分数。 在这种情况下，将会抛出一个错误。

#### Limited de-dup logic. <a href="#diversifiedsampleraggregation-limitedde-duplogic." id="diversifiedsampleraggregation-limitedde-duplogic."></a>

de-duplication逻辑只适用于shard级别，因此不适用于across shards.

#### No specialized syntax for geo/date fields <a href="#diversifiedsampleraggregation-nospecializedsyntaxforgeo-datefields" id="diversifiedsampleraggregation-nospecializedsyntaxforgeo-datefields"></a>

目前用于定义多样化值的语法由field或script的选择来定义－没有添加语法糖来表达地理或日期单位，如“7d”（7天）。这种支持可能会在稍后的版本中添加，用户现在需要使用script创建这些类型的值。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/36aggregationsju-he-fen-679029/362tong-ju540828-bucketaggregations/duo-yuan-hua-de-caiyang-qi-ju-96c628-diversified-sampler-aggregation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
