【发布时间】:2015-08-18 14:08:16
【问题描述】:
我是 Elastic 搜索的新手,
我有类似的对象结构,
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);
}
你能帮我找到它吗?谢谢。
【问题讨论】:
-
.Index(this.ElasticClient.Index)似乎错误Index()需要一个字符串或 CLRType解析为school-v0.1 -
这是我班上的一个属性。所以它确实可以正确解决。但我认为我形成 NEST 聚合的方式有问题。
-
刚刚发现我的 NEST 查询是正确的。只有我尝试获取数据的方式不正确。请看下面我的回答。谢谢。
标签: elasticsearch nest