【问题标题】:How can I use X.PagedList with ElasticSearch Nest?如何将 X.PagedList 与 ElasticSearch Nest 一起使用?
【发布时间】:2018-08-01 09:26:55
【问题描述】:

背景

我正在使用 ElasticSearch 作为我正在开发的新 ASP.Net Core 2.1 网站的搜索引擎。我正在使用 Nest API 与之集成。我想使用 X.PagedList 为我处理分页。

我在其他 ASP.Net Core 项目中使用过它,它在 MS SQL Server 中查询数据效果很好。

代码

ISearchResponse<Foo> searchResponse = 
        _elasticSearchClient.Search<Foo>(s => s
                    .Query(q => q
                        .Bool(b => b.Filter(distanceFilters))
                        )
                    .Source(src => src
                                .Includes(i => i
                                        .Fields(
                                                f => f.Field1,
                                                f => f.Field2,
                                                f => f.Field3
                                                )
                                            )
                            )
                    .From(options.From)
                    .Size(options.Size)
                );

var hitsMD = searchResponse.HitsMetadata;
var results = hitsMD?.Hits.Select(s => new Hit()
{
    Index = s.Index,
    Id = s.Id,
    Score = s.Score,
    Job = s.Source
}
).ToPagedList(PageNumber, PageSize);

问题

当我对 ElasticSearch 返回的搜索结果调用 .ToPagedList 时,它只显示一页结果。

问题在于 ElasticSearch 有自己的分页机制,因此它只返回一页命中。

我的想法是,因为 ElasticSearch 传回了点击总数,我可以通过设置 PagedList.TotalItemCount 属性告诉 PagedList 列表中有多少项目。但是,我不能这样做,因为它是私人套装。

我尝试删除 from 和 size 但这会返回 10 个命中,这是 ElasticSearch 的默认大小,他们显然出于性能原因将其放置在适当的位置。

问题

如何在使用 Nest API 集成到 ElasticSearch 的同时使用 X.PagedList 包?

【问题讨论】:

    标签: elasticsearch asp.net-core .net-core pagedlist


    【解决方案1】:

    你基本上已经在这里得到了所有的部分。你所缺少的只是StaticPagedList&lt;T&gt;。由于分页已经由 Elasticsearch 处理,您需要简单地定义一个静态分页设置,即:

    var pagedResults = new StaticPagedList<Foo>(results, PageNumber, PageSize, total);
    

    【讨论】:

    • 太好了,谢谢@Chris Pratt。分页列表寻呼机使用页码作为查询字符串的一部分。基于 From & Size 的 ElasticSearch 页面。有没有一种方法可以使用 From 按大小递增来呈现 PagedListPager?我可以在我的模型上对 From 属性进行编程,以根据 page*size 进行计算,但我宁愿挂钩 From 并将其传递给 ElasticSearch。我正在使用的示例 PagedListPager。 @Html.PagedListPager(Model, page =&gt; Url.Action("Search", new { page, keywords = options.Keywords, location = options.Location, distance = options.Distance, size = options.Size }))
    猜你喜欢
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-19
    • 2017-01-13
    • 2021-01-20
    • 1970-01-01
    相关资源
    最近更新 更多