【问题标题】:How to get search results that contain given subword with NEST?如何使用 NEST 获取包含给定子词的搜索结果?
【发布时间】:2017-07-13 14:12:21
【问题描述】:

我们在 c# .NET 项目中使用 NEST 通过以下查询从 Elasticsearch 获取搜索结果:

var response = await client.SearchAsync<Document>(
        s => s.Query(q => q.Match(m => m
                               .Field(f => f.displayName)
                               .Query(s))));

问题是它返回所有在 displayName 中包含单独单词的结果。我们希望得到 displayName 包含 subword 的所有结果。例如,如果 s="ca" 我们想获取带有 displayName="my cat" 的文档。如何在 NEST 或 Sense 中做到这一点?

【问题讨论】:

    标签: c# asp.net elasticsearch nest


    【解决方案1】:

    查看使用包含edgengram token filtercustom analyzer 索引displayName 字段。

    使用这样的标记过滤器,它将从标记器生成的每个标记的开头生成 ngram。

    【讨论】:

      【解决方案2】:

      我用这个查询解决了我的问题:

      // Make transformation "my cat" => "*my* *cat*"
      var splited = searchTerms.Split(new char[] { ' ' });
      var transformedSearchTerms = splited.Aggregate("", (w1, w2) => w1 + (w1 == "" ? "" : " ") + "*" + w2 + "*");
      
      // Search for documents that contain all given subwords
      var response = await client.SearchAsync<Document>(
                      s => s.Query(q =>
                              q.QueryString(qs => qs
                                  .DefaultField(f => f.displayName)
                                  .Query(transformedSearchTerms)
                                  .DefaultOperator(Operator.And)
                              )));
      

      【讨论】:

      • 请记住,与在索引时生成 ngram 标记相比,这种方法的搜索性能相对较慢。
      猜你喜欢
      • 1970-01-01
      • 2012-11-25
      • 2020-05-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多