简体   繁体   中英

Sorting with multiple type with Elastic search using NEST

I am using NEST 6.0 with C# and want to apply sorting on multiple types. For example, I have two indexes, I1 and I2 for type T1 and type T2 respectively. I have an API which returns a result for a search for both types, so in result, there are some records of T1 and some are of T2 types.

Now I want to apply sorting on a field say CreatedDate, how can I do that? Both types have this same column name with the same type. Below is the query I am using which works fine but it is without sorting condition.

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

Based on the APIs documentation, I guess the code below is what you want.

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)
                        )
                    )
                );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM