在索引中,我们存储信息,例如学生及其部门信息。 查找查询以获取与搜索子句中传递的给定学生姓名属于同一部门的所有学生。
在普通的RDBMS中,这是可以实现的
SELECT
*
FROM
student_dept
WHERE
dept_name in(
SELECT
dept_name FROM student_dept
WHERE
student_name = 'John')
我们是否可以选择在 ES 中实现类似的结果?
注意:搜索和嵌套查询也发生在同一索引上。
期望 Elastic 搜索查询能够提供所需的结果
top_hits
和 bucket_selector
您可以使用具有两个嵌套聚合的聚合,并按其中之一对存储桶进行排序
我使用映射
PUT /students_from_depts
{
"mappings": {
"properties": {
"student_name": {
"type": "text",
"fields": {
"to_keyword": {
"type": "keyword"
}
}
},
"dept_name": {
"type": "keyword"
}
}
}
}
和样本文件
PUT /students_from_depts/_bulk
{"create":{"_id":1}}
{"student_name":"John","dept_name":"Economics"}
{"create":{"_id":2}}
{"student_name":"Peter","dept_name":"Economics"}
{"create":{"_id":3}}
{"student_name":"Ann","dept_name":"Economics"}
{"create":{"_id":4}}
{"student_name":"John","dept_name":"Art"}
{"create":{"_id":5}}
{"student_name":"Margaux","dept_name":"Art"}
{"create":{"_id":6}}
{"student_name":"Justin","dept_name":"Computer Sciences"}
{"create":{"_id":7}}
{"student_name":"Mary","dept_name":"Computer Sciences"}
聚合查询有两个嵌套聚合。
student_names
列出某个部门的学生姓名。 by_student_name
按学生姓名过滤。 include
参数存储用于搜索的学生姓名。如果需要,请更改它。 by_student_name_bucket_count_filter
筛选包含名为 John 的学生的院系
GET /students_from_depts/_search?filter_path=aggregations.by_dept_name.buckets.key,aggregations.by_dept_name.buckets.student_names.hits.hits._source
{
"aggs": {
"by_dept_name": {
"terms": {
"field": "dept_name"
},
"aggs": {
"student_names": {
"top_hits": {
"size": 10
}
},
"by_student_name": {
"terms": {
"field": "student_name.to_keyword",
"include": "John"
}
},
"by_student_name_bucket_count_filter": {
"bucket_selector": {
"buckets_path": {
"student_name_bucket_count": "by_student_name._bucket_count"
},
"script": "params.student_name_bucket_count > 0"
}
}
}
}
}
}
回应
{
"aggregations" : {
"by_dept_name" : {
"buckets" : [
{
"key" : "Economics",
"student_names" : {
"hits" : {
"hits" : [
{
"_source" : {
"student_name" : "John",
"dept_name" : "Economics"
}
},
{
"_source" : {
"student_name" : "Peter",
"dept_name" : "Economics"
}
},
{
"_source" : {
"student_name" : "Ann",
"dept_name" : "Economics"
}
}
]
}
}
},
{
"key" : "Art",
"student_names" : {
"hits" : {
"hits" : [
{
"_source" : {
"student_name" : "John",
"dept_name" : "Art"
}
},
{
"_source" : {
"student_name" : "Margaux",
"dept_name" : "Art"
}
}
]
}
}
}
]
}
}
}