`FirstOrDefault()` 在嵌套的 `Term()` 查询中有什么用途?
What purpose does `FirstOrDefault()` have in a nested NEST `Term()` Query?
我正在使用 NEST 使用嵌套查询来查询 ElasticSearch 索引并且一切正常,但对查询中一行代码的用途感到困惑。在继续之前,我将提供所有对象和查询本身。
这是映射到索引 "country"
的对象。
[ElasticsearchType(Name = "country")]
public class CountrySearchDto : ISearchIndex {
public int Id {get; set;}
public string Name { get; set; }
public string IsoCode {get; set;}
public string CountryCode{get; set;}
[Nested]
public List<AreaSearchDto> Areas {get; set;}
}
}
这是映射到索引 "area"
的对象。
public class AreaSearchDto : ISearchIndex{
public int Id {get; set;}
public string Name { get; set; }
public string GuideUrl { get; set; }
}
如您所见,country
索引中嵌套了 areas
。我所做的查询用于按嵌套在其中的区域搜索国家/地区。
这是查询。
var searchResults = await _searchClient.SearchAsync<CountrySearchDto>(s => s
.Index("country")
.Query(q => q
.Nested(c => c
.Path(p => p.Areas)
.Query(nq => nq
.Term(t => t.Areas.FirstOrDefault().Name, searchTerm) // Where searchTerm is a string like "Sydney" or "London"
))
)
);
我了解嵌套查询的工作原理,它正在正确获取我需要的所有信息。但是这行代码在做什么?
.Term(t => t.Areas.FirstOrDefault().Name, searchTerm) // Where searchTerm is a string like "Sydney" or "London"
FirstOrDefault()
不会将查询限制为仅匹配第一个区域值,而不是所有区域值吗?显然情况并非如此,但我想知道是否有人可以解释这里发生的事情。
t => t.Areas.FirstOrDefault().Name
是一个 lambda 表达式,它将解析为要在 Elasticsearch 查询中使用的字段名称。它本身并不是 执行 来检索第一个区域名称的值,而是遍历以构建术语查询所针对的字段名称的字符串。正如 aHochstein 在评论中指出的那样,嵌套查询中的术语查询的目标是 AreaSearchDto
的字段,作为从 CountrySearchDto
类型表示的文档根部的路径,搜索查询是范围为。使用 lambda 表达式执行此遍历允许您利用类型。
searchTerm
为 "searchTerm"
,生成的查询是
POST http://localhost:9200/country/country/_search
{
"query": {
"nested": {
"path": "areas",
"query": {
"term": {
"areas.name": {
"value": "searchTerm"
}
}
}
}
}
}
lambda 表达式已计算为字符串 "areas.name"
我正在使用 NEST 使用嵌套查询来查询 ElasticSearch 索引并且一切正常,但对查询中一行代码的用途感到困惑。在继续之前,我将提供所有对象和查询本身。
这是映射到索引 "country"
的对象。
[ElasticsearchType(Name = "country")]
public class CountrySearchDto : ISearchIndex {
public int Id {get; set;}
public string Name { get; set; }
public string IsoCode {get; set;}
public string CountryCode{get; set;}
[Nested]
public List<AreaSearchDto> Areas {get; set;}
}
}
这是映射到索引 "area"
的对象。
public class AreaSearchDto : ISearchIndex{
public int Id {get; set;}
public string Name { get; set; }
public string GuideUrl { get; set; }
}
如您所见,country
索引中嵌套了 areas
。我所做的查询用于按嵌套在其中的区域搜索国家/地区。
这是查询。
var searchResults = await _searchClient.SearchAsync<CountrySearchDto>(s => s
.Index("country")
.Query(q => q
.Nested(c => c
.Path(p => p.Areas)
.Query(nq => nq
.Term(t => t.Areas.FirstOrDefault().Name, searchTerm) // Where searchTerm is a string like "Sydney" or "London"
))
)
);
我了解嵌套查询的工作原理,它正在正确获取我需要的所有信息。但是这行代码在做什么?
.Term(t => t.Areas.FirstOrDefault().Name, searchTerm) // Where searchTerm is a string like "Sydney" or "London"
FirstOrDefault()
不会将查询限制为仅匹配第一个区域值,而不是所有区域值吗?显然情况并非如此,但我想知道是否有人可以解释这里发生的事情。
t => t.Areas.FirstOrDefault().Name
是一个 lambda 表达式,它将解析为要在 Elasticsearch 查询中使用的字段名称。它本身并不是 执行 来检索第一个区域名称的值,而是遍历以构建术语查询所针对的字段名称的字符串。正如 aHochstein 在评论中指出的那样,嵌套查询中的术语查询的目标是 AreaSearchDto
的字段,作为从 CountrySearchDto
类型表示的文档根部的路径,搜索查询是范围为。使用 lambda 表达式执行此遍历允许您利用类型。
searchTerm
为 "searchTerm"
,生成的查询是
POST http://localhost:9200/country/country/_search
{
"query": {
"nested": {
"path": "areas",
"query": {
"term": {
"areas.name": {
"value": "searchTerm"
}
}
}
}
}
}
lambda 表达式已计算为字符串 "areas.name"