在Elastic Search Nest Client .NET中实现搜索查询的异步方式

时间:2019-05-02 04:27:30

标签: elasticsearch nest elastic-stack

我已经通过NEST客户端实现了搜索查询,并且能够获取记录。代码如下。

docker run --rm -it -v $(pwd):/source somedocker

我将响应映射到我的项目模型,如下所示。

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

当我尝试以异步方式实现相同的搜索

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

执行它时没有任何错误。但是我无法获取 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)) ); 对象来将其映射回我的原始项目模型。 提前谢谢

1 个答案:

答案 0 :(得分:0)

SearchAsync<T>()中,responseTask<ISearchResponse<T>>,因此您可能想await