Elasticsearch做"关联查询"——nested-parent关系
来源:互联网 发布:linux vim 显示行数 编辑:程序博客网 时间:2024/06/05 04:08
Elasticsearch作为一个面向文档的存储服务,并没有严格意义上的关联查询能力。但可以通过mapping来指定文档的nested-parent关系,实现关联。
下面以学生(student)和班级(clazz)的关系为例,演示这种"关联查询":
建立一个索引test,有student和clazz两个type,存储班级和学生
首先设置班级和学生的mapping:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
PUT /test
{
"mappings"
: {
"clazz"
: {
"properties"
: {
"students"
: {
"type"
:
"nested"
}
}
},
"student"
:{
"_parent"
: {
"type"
:
"clazz"
}
}
}
}
实测clazz的映射描述可以省略
插入测试数据
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//班级
PUT /test/clazz/c1
{
"name"
:
"class1"
,
"teacher"
:
"Mr wang"
}
//学生,通过parent=c1将其关联至班级class1
PUT /test/student/s1?parent=c1
{
"name"
:
"tom"
,
"age"
:
15
}
//学生,通过parent=c1将其关联至班级class1
PUT /test/student/s2?parent=c1
{
"name"
:
"小明"
,
"age"
:
16
}
然后就可以查询了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//查询班级class1中有哪些学生
GET /test/student/_search
{
"query"
: {
"has_parent"
: {
"type"
:
"clazz"
,
"query"
: {
"match"
:{
"name"
:
"class1"
}
}
}
}
}
//查询小明在哪个班
GET /test/clazz/_search
{
"query"
: {
"has_child"
: {
"type"
:
"student"
,
"query"
: {
"term"
: {
"name"
: {
"value"
:
"小明"
}
}
}
}
}
}
http://www.wowtools.org/blog/articles/2015/09/08/1441692036407.html
0 0
- Elasticsearch做"关联查询"——nested-parent关系
- 如何成为"好导师"、"好学生"——"馒头"铿锵三人行
- 转:关于js中"window.location."、"location."、"parent.location."、"top.location用法
- " " ' '
- Elasticsearch——利用Parent-Child关系解决大数据场景下的实时查询
- 对"一个非常难的查询问题(部门上下级的关系)"之解答的完善
- String s = "a" + "b" + "c" + "d" + "
- 可恨之人,必有可怜之处——"恶"婆婆…
- about"spaceused"
- extern "c"
- 说好"but"
- 打开未关联文件时,禁用 "使用Web服务寻找适当的程序"
- here."
- 程序设计 "
- "基于对象" 和 "面向对象"
- "git rm" 和 "rm" 的区别
- "转义
- 关于".set push"和".set pop"
- 协同过滤推荐算法在MapReduce与Spark上实现对比
- Mahout中数据的存储方式
- 基于Mahout的电影推荐系统
- 贝叶斯理论——一种由表及里的方法
- vs2010+Qt5.5.0开发环境搭建
- Elasticsearch做"关联查询"——nested-parent关系
- 一个封装好的线程类
- 使用Mahout实现协同过滤 spark
- GitHub 优秀的 Android 开源项目
- Hi35xx NVR GDB调试
- 15电气郄慧敏vb作业1在VB中制作计时器
- [置顶] Struts2 的token原理
- 下载网络文件HttpURLConnection.getContentLength()大小为 0
- 约瑟夫环的一种简单解法