gpt4 book ai didi

c# - 如何使用值名称获取重复值的计数

转载 作者:行者123 更新时间:2023-12-03 00:02:57 24 4
gpt4 key购买 nike

我是Elasticsearch的新手,我正在使用NEST库。如何获得重复值的计数?

这是我的课:

public class Book
{
public string BookName {get;set;}
public string Author {get;set;}
}

这是我的数据:
BookName=X, Author=a<br>
BookName=Y, Author=a<br>
BookName=Z, Author=b<br>
BookName=C, Author=b<br>
BookName=T, Author=c<br>

查询结果应为:
a- 2
b- 2
c- 1

我尝试了以下查询,但不起作用:
 client.Search<Book>(s => s
.Aggregations(a => a
.Terms("group_by_auth", ts => ts
.Field(o => o.Author)
.Size(10)
.Aggregations(aa => aa
.Sum("sum_value", sa => sa
.Field(o => o.Author)
)
)
)
));

映射为:
client= new ElasticClient(connectionSettings);
client.CreateIndex("books", c => c
.Mappings(m => m
.Map<Book>(mm => mm
.Properties(ps=>ps
.Text(s=>s
.Name(a=>a.Author)
)))
)

最佳答案

如果您想计算条件,可以这样

var uri = new Uri("http://localhost.fiddler:9200");

ElasticClient db = new ElasticClient(uri);

var data = new[] {
new{ BookName= "X", Author="a" },
new{ BookName= "Y", Author="a" },
new{ BookName= "Z", Author="b" },
new{ BookName= "C", Author="b" },
new{ BookName= "T", Author="c" },
};

db.DeleteIndex("test");

foreach (var d in data)
{
db.Index(d, id => id.Index("test"));
}

System.Threading.Thread.Sleep(1000);

var items = db.Search<dynamic>(s => s.Size(0).Aggregations(aggr => aggr.Terms("group_by_auth", ts => ts.Field("author.keyword"))));

foreach (var item in items.Aggs.Terms("group_by_auth").Buckets)
{
Console.WriteLine(item.Key + "-" + item.DocCount);
}

Console.WriteLine("DONE");

Console.ReadLine();

关于c# - 如何使用值名称获取重复值的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47383050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com