【问题标题】:NEST Returns Nothing on Fuzzy SearchNEST 在模糊搜索中不返回任何内容
【发布时间】:2015-08-18 19:39:38
【问题描述】:

我在 Sense 中使用以下查询并得到一些结果:

POST myindex/mytype/_search
{
  "query": {
        "fuzzy_like_this_field" : {
            "BookLabel" : {
                "like_text" : "myBook",
                "max_query_terms" : 12
            }
        }
    }
}

但是使用 Nest 的以下代码我什么也得不到:

    var docs = client.Search<dynamic>(b => b
            .Index("myindex")
            .Type("mytype")
                .Query(q => q
                    .Fuzzy(fz => fz
                        .OnField("BookLabel")
                        .Value("myBook")
                    )
                )
        ).Documents.ToList(); 

我看不出它们之间的区别。我错过了什么?

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

    您上面的 NEST 查询会生成以下查询 DSL

    {
      "query": {
        "fuzzy": {
          "BookLabel": {
            "value": "myBook"
          }
        }
      }
    }
    

    要获得与 fuzzy_like_this_field 查询 (which is deprecated in Elasticsearch 1.6.0 and will be removed in 2.0) 最接近的等价物,您可以仅在您感兴趣的字段上运行 fuzzy_like_this 查询

    void Main()
    {
        var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
        var connection = new InMemoryConnection(settings);
        var client = new ElasticClient(connection: connection);
    
        var docs = client.Search<dynamic>(b => b
            .Index("myindex")
            .Type("mytype")
            .Query(q => q
                .FuzzyLikeThis(fz => fz
                    .LikeText("myBook")
                    .MaxQueryTerms(12)
                    .OnFields(new [] { "BookLabel" })                           
                )
            )
        );
    
        Console.WriteLine(Encoding.UTF8.GetString(docs.RequestInformation.Request));
    }
    

    这会输出以下查询 DSL

    {
      "query": {
        "flt": {
          "fields": [
            "BookLabel"
          ],
          "like_text": "myBook",
          "max_query_terms": 12
        }
      }
    }
    

    这应该会产生与您在 Sense 中看到的结果相同的结果。

    【讨论】:

    • 谢谢拉斯。我还注意到之前生成的 DSL 不正确。既然 flt 将被弃用,我应该改用什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2013-04-07
    相关资源
    最近更新 更多