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

我有一个示例公司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)
        )
    )
);

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

xaut2008 回答:NEST如何在子文档上查找字段

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

fq.Term(f => f.Employees.FirstOrDefault().Education,education)

希望有帮助。

本文链接:https://www.f2er.com/3154414.html

大家都在问