c# - 从QueryContainer删除重复的UserID(字段)

标签 c# elasticsearch nest

我在 Elasticsearch C#中遇到某些查询问题。

我有这个QueryContainer,有一个内部QueryDescriptor和很多内部QueryContainers \ QueryDescriptors,

但是一个主QueryContainer => this._QueryContainer包含所有数据。

事实是,字段UserID在this._QueryContainer中不是唯一的,所以当我返回20个唯一用户时,第一次都可以,但是接下来的20个用户(对于下一页)我不知道从哪里开始this.From ...

因为this._QueryContainer具有重复项,但由于聚合而返回唯一。因此存在冲突。

有没有办法使查询从一开始就与众不同?

results = Client.Search<Users>(s => s
                .From(this.From)
                .Query(this._QueryContainer)
                .Aggregations(a => a
                    .Terms("unique", te => te
                        .Field(p => p.UserID)
                    )
                )
                .Size(20)
            );

最佳答案

查询中的.From().Size()不会影响您拥有的Terms聚合,它们仅适用于.Query()部分,并从中返回匹配。

如果您需要从“条款”聚合中返回很多值,这是我认为您想做的,则可以

1. Use partitioning过滤值,在多个请求中返回大量字词,例如

var response = client.Search<Users>(s => s
    .Aggregations(a => a
        .Terms("unique", st => st
            .Field(p => p.UserID)
            .Include(partition: 0, numberOfPartitions: 10)
            .Size(10000)
        )
    )
);

// get the next partition
response = client.Search<Users>(s => s
    .Aggregations(a => a
        .Terms("unique", st => st
            .Field(p => p.UserID)
            .Include(partition: 1, numberOfPartitions: 10)
            .Size(10000)
        )
    )
);

2. Use a Composite Aggregation with a Terms value source
var response = client.Search<Users>(s => s
    .Aggregations(a => a
        .Composite("composite", c => c
            .Sources(so => so
                .Terms("unique", st => st
                    .Field(p => p.UserID)                   
                )
            )
        )
    )
);

// the following would be in a loop, to get all terms
var lastBucket = response.Aggregations.Composite("composite").Buckets.LastOrDefault();

if (lastBucket != null)
{
    // get next set of terms
    response = client.Search<Users>(s => s
        .Aggregations(a => a
            .Composite("composite", c => c
                .Sources(so => so
                    .Terms("unique", st => st
                        .Field(p => p.UserID)
                    )
                )
                .After(lastBucket.Key)
            )
        )
    );
}

关于c# - 从QueryContainer删除重复的UserID(字段),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51241311/

相关文章:

c# - 比较 EF 时间戳值

javascript - 将 Javascript 对象转换为 C# 对象

Elasticsearch 索引在磁盘上占用的空间比显示的要大

database - Elastic Search 作为持久数据库

elasticsearch - 用基本查询数

c# - 错误消息 'The specified locale is not supported on this operating system.'

c# - 如何在 MailMessage 中正确添加 CSS

elasticsearch - Mapper_parsing_exception : Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes

elasticsearch - Elasticsearch.net索引设置+分析器

elasticsearch - 在elasticsearch中使用MinimumShouldMatch和术语查询