NEST如何在子文档上查找字段

问题描述 投票:0回答:1

我有一个示例公司JSON文档结构,如下所示:

[
    {
        "id": "id1",
        "name": "company1",
        "employees": [
            {
                "name": "employee1",
                "education": "education1"
            },
            {
                "name": "employee2",
                "education": "education2"
            }
        ]
    }
]

而且我正在这样查询:

GET companies/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "employees.education": {
              "value": "education1"
            }
          }
        },
        {
          "term": {
            "employees.education": {
              "value": "education2"
            }
          }
        }
      ]
    }
  }
}

查询是使用NEST构建的:

var filters = new List<Func<QueryContainerDescriptor<Company>, QueryContainer>>();

foreach (var education in educationsToFilter)
{
    filters.Add(fq => fq.Term(f => f.Employees.Suffix("education"), education));
}

var searchResponse = _client.Search<Company>(s => s
    .Query(q => q
        .Bool(bq => bq
            .Filter(filters)
        )
    )
);

是否有更好的方法来查找术语字段而不是使用后缀方法?我想要一种更安全的类型。

c# elasticsearch nest
1个回答
0
投票

您可以使用此lambda表达式解析嵌套的字段名称:

© www.soinside.com 2019 - 2024. All rights reserved.