ElasticSearch - NEST 等同于 Sense 中的 "nested Filter Query"?
ElasticSearch - NEST equivalent for the "nested Filter Query" in Sense?
我是 Elastic 搜索新手,
我有 object 结构,
class Student
{
int schoolId;
List<Subject> Subjects;
}
class Subject
{
string title;
string type;
}
我正在尝试列出所有独特的主题标题。到目前为止,我有以下查询在 Sense 中运行并给出了我想要的结果。
curl -XGET "http://localhost:9200/school-v0.1/student/_search?search_type=count&routing=5" -d '{
"filter":
{
"term": { "schoolId": 5 }
},
"aggs":
{
"SubjectsAggr":
{
"nested": { "path": "Subjects" },
"aggs":
{
"TitlesAggr":
{
"terms": { "field": "subject.title" }
}
}
}
}
}'
但我没有得到相同的 NEST 等价物。请参阅下文。
我错过了什么吗?
var result = this.ElasticClient.Search<Student>(q => q
.Index(this.ElasticClient.Index)
.Routing(schoolId)
.SearchType(SearchType.Count)
.Filter(q1 => q1.Term(a => a.schoolId, schoolId))
.Aggregations(student => student.Nested("SubjectsAggr", b => b.Path("subjects")
.Aggregations(sub => sub.Terms("TitlesAggr", s => s.Field("subject.title"))))));
//Below is what I hope to do, but the I get compilation errors because there seems to be something wrong with above NEST query that I have written
var subjectsAggregation = esResult.Aggs.Nested("SubjectsAggr");
var titlesAggregation = subjectsAggregation.Aggs.Terms("TitlesAggr");
var subjects = new List<string>();
foreach (var s in titlesAggregation.Items)
{
subjects.Add(s.Key);
}
你能帮我找到吗?谢谢。
我的 NEST 查询确实是正确的。我只是没有以适当的方式获取结果。我现在可以从搜索结果中提取所需的信息,如下所示。
var subjectsAggregation = result.Aggs.Nested("SubjectsAggr");
var titlesAggregation = subjectsAggregation.Aggregations["TitlesAggr"];
var titlesBucket = titlesAggregation as Bucket;
var titles = new List<string>();
if (titlesBucket != null)
{
foreach (var title in titlesBucket.Items)
{
titles.Add((((KeyItem) title).Key));
}
}
我是 Elastic 搜索新手,
我有 object 结构,
class Student
{
int schoolId;
List<Subject> Subjects;
}
class Subject
{
string title;
string type;
}
我正在尝试列出所有独特的主题标题。到目前为止,我有以下查询在 Sense 中运行并给出了我想要的结果。
curl -XGET "http://localhost:9200/school-v0.1/student/_search?search_type=count&routing=5" -d '{
"filter":
{
"term": { "schoolId": 5 }
},
"aggs":
{
"SubjectsAggr":
{
"nested": { "path": "Subjects" },
"aggs":
{
"TitlesAggr":
{
"terms": { "field": "subject.title" }
}
}
}
}
}'
但我没有得到相同的 NEST 等价物。请参阅下文。 我错过了什么吗?
var result = this.ElasticClient.Search<Student>(q => q
.Index(this.ElasticClient.Index)
.Routing(schoolId)
.SearchType(SearchType.Count)
.Filter(q1 => q1.Term(a => a.schoolId, schoolId))
.Aggregations(student => student.Nested("SubjectsAggr", b => b.Path("subjects")
.Aggregations(sub => sub.Terms("TitlesAggr", s => s.Field("subject.title"))))));
//Below is what I hope to do, but the I get compilation errors because there seems to be something wrong with above NEST query that I have written
var subjectsAggregation = esResult.Aggs.Nested("SubjectsAggr");
var titlesAggregation = subjectsAggregation.Aggs.Terms("TitlesAggr");
var subjects = new List<string>();
foreach (var s in titlesAggregation.Items)
{
subjects.Add(s.Key);
}
你能帮我找到吗?谢谢。
我的 NEST 查询确实是正确的。我只是没有以适当的方式获取结果。我现在可以从搜索结果中提取所需的信息,如下所示。
var subjectsAggregation = result.Aggs.Nested("SubjectsAggr");
var titlesAggregation = subjectsAggregation.Aggregations["TitlesAggr"];
var titlesBucket = titlesAggregation as Bucket;
var titles = new List<string>();
if (titlesBucket != null)
{
foreach (var title in titlesBucket.Items)
{
titles.Add((((KeyItem) title).Key));
}
}