我目前正在Uudemy上学习ElasticSearch课程,但视频正在讨论ElasticSearch 5,我目前正在使用ElasticSearch 6.我无法将父/子关系转换为新格式。
视频正在设置Franchise
Films
(分别为Star Wars
和The Jedi Returns
。
导师做了以下事情:
curl -H "Content-Type: application/json" -XPUT "127.0.0.1:9200/series" -d '
{
"mappings": {
"franchise": {},
"film": {
"_parent": {
"type": "franchise"
}
}
}
}'
但是当我尝试添加映射时,我会收到以下错误:
➜ Downloads curl -H "Content-Type: application/json" -XPUT "127.0.0.1:9200/series?pretty" -d '
{
"mappings": {
"franchise": {},
"film": {
"_parent": {
"type": "franchise"
}
}
}
}'
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "Rejecting mapping update to [series] as the final mapping would have more than 1 type: [franchise, film]"
}
],
"type" : "illegal_argument_exception",
"reason" : "Rejecting mapping update to [series] as the final mapping would have more than 1 type: [franchise, film]"
},
"status" : 400
}
我已经根据以下资源尝试了很多东西但是却找不到解决方案: https://www.elastic.co/blog/index-type-parent-child-join-now-future-in-elasticsearch https://www.elastic.co/guide/en/elasticsearch/reference/master/parent-join.html https://github.com/elastic/elasticsearch/issues/20257
TLDR:有人可以帮助我将ElastiSearch 5中的亲子关系翻译成在ElasticSearch 6中正确的方式吗?
下一步(验证):作为验证映射是否正常的验证,导师然后执行以下操作:
他获得了以下JSON数据:
wget http://media.sundog-soft.com/es/series.json
从JSON文件中提取:
{ "create" : { "_index" : "series", "_type" : "franchise", "_id" : "1"} }
{ "id": "1", "title" : "Star Wars" }
{ "create" : { "_index" : "series", "_type" : "film", "_id" : "260", "parent" : "1" } }
{ "id": "260", "title" : "Star Wars: Episode IV - A New Hope", "year":"1977" , "genre":["Action", "Adventure", "Sci-Fi"] }
通过执行以下操作批量导入数据:
➜ curl -H "Content-Type: application/json" -XPUT "127.0.0.1:9200/_bulk?pretty" --data-binary @series.json
答案 0 :(得分:8)
使用Elasticsearch 6.0时,存在一些阻止父/子关系的根本性变化。
_parent
字段也会被删除。您必须使用连接字段
父母/子女。父/子关系要求有两种不同的类型,并且两种类型都在同一索引中定义。现在您不能在一个索引中拥有多个类型,因此无法支持父/子关系,就像它们在5.x和先前版本中支持它们一样。
您可以参考联接字段文档,了解如何对父/子关系执行类似操作。但是现在,您必须在同一类型的单个Elasticsearch索引中定义两种类型的文档。请参阅示例,该示例说明如何使用联接字段here对“1对多”关系进行建模(1个问题,与该问题相关的多个答案)。
编辑:
使用join field
为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"
}
}
}'
答案 1 :(得分:1)
完成@Pranav Shukla所说的如果你想查询特定电影属于它的特许经营权(例如“The Force Awakens”),你可以使用以下查询:
curl -XGET localhost:9200/series/_search?pretty -H 'Content-Type: application/json' -d '
{
"query": {
"has_child" : {
"type": "film",
"query": {
"match": {
"title": "The Force Awakens"
}
}
}
}
}'