Elasticsearch NEST翻译

时间:2015-04-10 15:39:37

标签: elasticsearch nest elasticsearch-net

我有一个搜索,它给出了正确的结果,并尝试让它在.NET Nest中运行,但我似乎无法获得正确的语法。这是我的elastricsearch查询:

{
  "query": {
    "filtered": { 
      "query": {
        "match": { "formattedName": "Michael" }
      },
      "filter": {
        "bool": {   
            "must": [
                { "term": { "projectId": "5022" } },
                { "term": { "isInvalid": "false" } }
            ]
        }
      }
    }
  }
}

在我的解决方案中,我有以下内容:

var lst = client.Search<EntitySearchItem>(s => s
    .Size(recordCount)
    .Index("entitysearch")
    .Filter(f => f
        .Bool(b => b
            .Must(m => m.Term("projectId", projectId), 
                m => m.Term("isInvalid", "false"))))
    .Query(q => q
        .Match(p => p.OnField(f => f.FormattedName).Query(name))));

熟悉Nest的人是否知道如何获得相同的结果?谢谢!

1 个答案:

答案 0 :(得分:2)

示例中的elasticsearch查询使用filtered query。您可以使用NEST创建一个:

var searchResponse = client.Search<EntitySearchItem>(s => s
    .Index("entitysearch")
    .Query(q => q
        .Filtered(f => f
            .Query(qq => qq
                .Match(m => m.OnField(field => field.FormattedName).Query(name)))
            .Filter(ff => ff.Bool(b => b
                .Must(m => m.Term(t => t.ProjectId, projectId), m => m.Term(t => t.IsInvalid, "false")))))));

我的EntitySearchItem班级

public class EntitySearchItem
{
    public string ProjectId { get; set; }
    public string IsInvalid { get; set; }
    public string FormattedName { get; set; }
}