NEST如何在子文档上查找字段
NEST How to find field on subdocument
我有一个示例公司 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)
)
)
);
有没有比使用后缀法更好的查找术语字段的方法?我想要一种更安全的方式。
您可以使用此 lambda 表达式解析嵌套字段名称:
fq.Term(f => f.Employees.FirstOrDefault().Education, education)
希望对您有所帮助。
我有一个示例公司 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)
)
)
);
有没有比使用后缀法更好的查找术语字段的方法?我想要一种更安全的方式。
您可以使用此 lambda 表达式解析嵌套字段名称:
fq.Term(f => f.Employees.FirstOrDefault().Education, education)
希望对您有所帮助。