使用Elasticsearch 6.0,存在一些阻止父/子关系的根本性变化.
>一个索引不能包含多个类型.阅读更多here.
>父/子关系已被删除,因此
_parent字段也被删除.您必须使用连接字段
父母/子女.
父/子关系要求有两种不同的类型,并且两种类型都在同一索引中定义.既然您不能在一个索引中拥有多个类型,则无法以与5.x和先前版本中支持的方式相同的方式支持父/子关系.
您可以参考联接字段文档,了解如何对父/子关系执行类似操作.但是现在,您必须在同一类型的单个Elasticsearch索引中定义两种类型的文档.请参阅示例,该示例解释了如何使用连接字段here建模“1对多”关系(1个问题,与该问题相关的多个答案).
编辑:
使用join字段为Elasticsearch 6.x更新的示例如下所示.
删除现有索引(如果存在).
curl -XDELETE "http://localhost:9200/series"
使用连接字段创建新索引,建立特许经营权和加盟关系之间的联系关系.电影:
curl -XPUT "http://localhost:9200/series" -H 'Content-Type: application/json' -d'
{
"mappings": {
"doc": {
"properties": {
"join_field": {
"type": "join",
"relations": {
"franchise": "film"
}
}
}
}
}
}'
您的示例中更新的series.json将作为内联批量请求发送:
curl -XPOST "http://localhost:9200/_bulk" -H 'Content-Type: application/json' -d'
{ "create" : { "_index" : "series", "_type" : "doc", "_id" : "1"} }
{ "id": "1", "title" : "Star Wars", "join_field": "franchise" }
{ "create" : { "_index" : "series", "_type" : "doc", "_id" : "260", "routing" : "1" } }
{ "id": "260", "title" : "Star Wars: Episode IV - A New Hope", "year":"1977" , "genre":["Action", "Adventure", "Sci-Fi"], "join_field": {"name": "film", "parent": "1"} }
{ "create" : { "_index" : "series", "_type" : "doc", "_id" : "1196", "routing" : "1" } }
{ "id": "1196", "title" : "Star Wars: Episode V - The Empire Strikes Back", "year":"1980" , "genre":["Action", "Adventure", "Sci-Fi"], "join_field": {"name": "film", "parent": "1"} }
{ "create" : { "_index" : "series", "_type" : "doc", "_id" : "1210", "routing" : "1" } }
{ "id": "1210", "title" : "Star Wars: Episode VI - Return of the Jedi", "year":"1983" , "genre":["Action", "Adventure", "Sci-Fi"], "join_field": {"name": "film", "parent": "1"} }
{ "create" : { "_index" : "series", "_type" : "doc", "_id" : "2628", "routing" : "1" } }
{ "id": "2628", "title" : "Star Wars: Episode I - The Phantom Menace", "year":"1999" , "genre":["Action", "Adventure", "Sci-Fi"], "join_field": {"name": "film", "parent": "1"} }
{ "create" : { "_index" : "series", "_type" : "doc", "_id" : "5378", "routing" : "1" } }
{ "id": "5378", "title" : "Star Wars: Episode II - Attack of the Clones", "year":"2002" , "genre":["Action", "Adventure", "Sci-Fi", "IMAX"], "join_field": {"name": "film", "parent": "1"} }
{ "create" : { "_index" : "series", "_type" : "doc", "_id" : "33493", "routing" : "1" } }
{ "id": "33493", "title" : "Star Wars: Episode III - Revenge of the Sith", "year":"2005" , "genre":["Action", "Adventure", "Sci-Fi"], "join_field": {"name": "film", "parent": "1"} }
{ "create" : { "_index" : "series", "_type" : "doc", "_id" : "122886", "routing" : "1" } }
{ "id": "122886", "title" : "Star Wars: Episode VII - The Force Awakens", "year":"2015" , "genre":["Action", "Adventure", "Fantasy", "Sci-Fi", "IMAX"], "join_field": {"name": "film", "parent": "1"} }
'
上面的批量请求创建了一个与该特许经营相关的特许经营和多部电影.
要查询具有特许经营权ID = 1的所有电影,请使用以下父ID查询.
curl -XGET "http://localhost:9200/series/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"parent_id": {
"type": "film",
"id": "1"
}
}
}'