Search code examples
elasticsearchnestelastic-stack

Async way of implementing search query in Elastic Search Nest Client .NET


I have implemented a Search Query through NEST client and was able to get the records. The code is as follows.

 var response = clientProvider.Client.Search<ProjectModel>(s => s
                   .Index("project_index")
                   .Type("projects")
                   .Source(so => so.Excludes(f => f.Field(x => x.FileInfo.FileBase64Data)))
                   .Size(100)
                   .Query(q => q
                       .Bool(b => b
                           .Should(
                               m => m.QueryString(qs => qs
                                    .Query(searchOptions.SearchTerm)
                                    .Fields(ff => ff.Fields(fields))
                                    .Fuzziness(Fuzziness.Auto)

                                    ),
                               m => m.MultiMatch(qs => qs
                                    .Query(searchOptions.SearchTerm)
                                    .Type(Nest.TextQueryType.PhrasePrefix)
                                    .Fields(ff => ff.Fields(fields))

                                    )
                                  )
                            )
                         )
                           .Sort(ss => ss.Descending(SortSpecialField.Score))
                         );

And I am mapping the response to my Project Model as follows.

 var project = response.Hits.Select(h =>
                {
                    h.Source._id = h.Id;
                    h.Source.Score = h.Score;
                    return h.Source;
                }).ToList();

When I am trying to implement the same Search in Async way that is

 var response = clientProvider.Client.SearchAsync<ProjectModel>(s => s
                   .Index("project_index")
                   .Type("projects")
                   .Source(so => so.Excludes(f => f.Field(x => x.FileInfo.FileBase64Data)))
                   .Size(100)
                   .Query(q => q
                       .Bool(b => b
                           .Should(
                               m => m.QueryString(qs => qs
                                    .Query(searchOptions.SearchTerm)
                                    .Fields(ff => ff.Fields(fields))
                                    .Fuzziness(Fuzziness.Auto)

                                    ),
                               m => m.MultiMatch(qs => qs
                                    .Query(searchOptions.SearchTerm)
                                    .Type(Nest.TextQueryType.PhrasePrefix)
                                    .Fields(ff => ff.Fields(fields))

                                    )
                                  )
                            )
                         )
                           .Sort(ss => ss.Descending(SortSpecialField.Score))
                         );

I am not getting any errors while executing it. But I am not able to get the response.Hits objects to map it back to my original Project Model. Thanks In advance


Solution

  • In SearchAsync<T>(), response is Task<ISearchResponse<T>>, so you probably want to await it