Join field type
join数据类型是一个在文档中创建父/子关系的特殊字段。relations部分定义了文档中一组可能关系,每个关系都是一个父名称和一个子名称。
注意
我们不建议使用多级关系来复制关系模型。每级关系都会在查询时增加内存和计算方面的开销。为了获得更好的搜索性能,请改为对数据进行非规范化。
父/子关系可以定义如下:
PUT my-index-000001
{
"mappings": {
"properties": {
"my_id": {
"type": "keyword"
},
"my_join_field": {
"type": "join",
"relations": {
"question": "answer"
}
}
}
}
}
定义了一个简单的关系,question是answer的父亲。
有点像mysql中自己定义父子关系时用的parent_id
使用join索引一个文档,关系的名称和文档可选的父项必须在source中提供。例如,以下示例在question上下文中创建两个父文档:
PUT my-index-000001/_doc/1?refresh
{
"my_id": "1",
"text": "This is a question",
"my_join_field": {
"name": "question"
}
}
PUT my-index-000001/_doc/2?refresh
{
"my_id": "2",
"text": "This is another question",
"my_join_field": {
"name": "question"
}
}
索引父文档时,您可以选择仅指定关系的名称作为快捷方式,而不是将其封装在普通的对象中:
PUT my-index-000001/_doc/1?refresh
{
"my_id": "1",
"text": "This is a question",
"my_join_field": "question"
}
PUT my-index-000001/_doc/2?refresh
{
"my_id": "2",
"text": "This is another question",
"my_join_field": "question"
}
索引子项时,关系的名称以及文档的ID必须添加到_source。
注意
需要索引同一碎片中父文档的谱系,因此必须始终使用其更大的父id来路由子文档。
例如,以下示例显示了如何索引两个child文档:
PUT my-index-000001/_doc/3?routing=1&refresh
{
"my_id": "3",
"text": "This is an answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
PUT my-index-000001/_doc/4?routing=1&refresh
{
"my_id": "4",
"text": "This is another answer",
"my_join_field": {
"name": "answer",
"parent": "1"
}
}
route值是必需的,因为父文档和子文档必须在同一碎片上被索引的
参考
开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 4 天,点击查看活动详情