【问题标题】:Sorting with multiple type with Elastic search using NEST使用 NEST 使用弹性搜索对多种类型进行排序
【发布时间】:2019-02-10 18:43:14
【问题描述】:

我正在使用带有 C# 的 NEST 6.0,并希望对多种类型应用排序。例如,我有两个索引,I1 和 I2 分别用于 T1 类型和 T2 类型。我有一个 API,它返回搜索这两种类型的结果,因此结果中,有一些 T1 的记录和一些 T2 类型的记录。

现在我想对 CreatedDate 字段应用排序,我该怎么做?两种类型都具有相同类型的相同列名。下面是我正在使用的查询,它工作正常,但没有排序条件。

await _client.SearchAsync<dynamic>(s => s
                    .AllIndices()
                    .Type(types)
                    .From(from)
                    .Size(pageSize)
                    .Query(q => q
                        .MultiMatch(m => m
                            .Query(searchText)
                            )
                    )
                );

【问题讨论】:

    标签: c# elasticsearch


    【解决方案1】:

    根据 APIs 文档,我猜下面的代码就是你想要的。

    await _client.SearchAsync<dynamic>(s => s
                        .AllIndices()
                        .Type(types)
                        .From(from)
                        .Size(pageSize)
                        .Query(q => q
                            .MultiMatch(m => m
                                .Query(searchText)
                            )
                        )
                        .Sort(ss => ss                            
                            //.Descending(s => s.CreatedDate)
                            .Field(f => f
                               .Field(ff => ff.CreatedDate)
                               .Type("T1")
                               .Order(SortOrder.Descending)
    
                            )
                            .Field(f => f
                               .Field(ff => ff.CreatedDate)
                               .Type("T2")
                               .Order(SortOrder.Descending)
                            )
                        )
                    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-30
      • 1970-01-01
      • 2019-10-22
      • 2016-12-09
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      相关资源
      最近更新 更多