gpt4 book ai didi

c# - 在 Nest ElasticSearch 中重复使用计数和搜索查询

转载 作者:行者123 更新时间:2023-11-30 20:25:46 29 4
gpt4 key购买 nike

有没有办法通过 Nest 在 ElasticSearch 中为 Client.Search 和 Client.Count 重用流畅的查询?

这是定义搜索流畅定义的片段的开头。

 System.Func<SearchDescriptor<Documents.City>, ISearchRequest> x = s => 
s.Index(IndexNames.Cities).From(0)
.Size(100)
.Query(q => q.Bool(.....

count 的变化是 SearchDescriptor -> CountDescriptor 和 ISearchRequest -> ICountRequest。看起来查询需要写两次,因为如果没有预先了解正在使用的类型,流畅的标记将无法编译。

基本问题是,是否有使用 Nest 查询来执行 Search 和 Count 请求的简洁且可维护的方法?

最佳答案

与其重复使用搜索部分,不如尝试使用查询来完成:

Func<QueryContainerDescriptor<T>, QueryContainer> query =
q => q.MatchAll();

var searchResponse = await ElasticClient().SearchAsync<T>(s => s
.Query(query));

var countResponse = await ElasticClient().CountAsync<T>(s => s.Query(query));

希望对您有所帮助。

关于c# - 在 Nest ElasticSearch 中重复使用计数和搜索查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50912134/

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