在ElasticSearch 6中创建父子关系时遇到问题

8bi*_*ero 4 elasticsearch elasticsearch-5

我目前正在研究Uudemy的ElasticSearch课程,但视频正在谈论ElasticSearch 5,我目前正在使用ElasticSearch 6.我无法将父/子关系转换为新格式.

在视频设置FranchiseFilms(即Star WarsThe Jedi Returns分别.

导师做以下事情:

curl -H "Content-Type: application/json" -XPUT "127.0.0.1:9200/series" -d '
{
    "mappings": {
        "franchise": {},
        "film": {
            "_parent": {
                "type": "franchise"
            }
        }
    }
}'
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试添加映射时,我收到以下错误:

?  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
}
Run Code Online (Sandbox Code Playgroud)

我已经根据以下资源尝试了很多东西但是找不到解决方案: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"] }
Run Code Online (Sandbox Code Playgroud)

并通过执行以下操作批量导入数据:

?  curl -H "Content-Type: application/json" -XPUT "127.0.0.1:9200/_bulk?pretty" --data-binary @series.json
Run Code Online (Sandbox Code Playgroud)

Pra*_*kla 11

使用Elasticsearch 6.0,存在一些阻止父/子关系的根本性变化.

  1. 一个索引不能包含多个类型.在这里阅读更多.
  2. 父/子关系已被删除,因此该 _parent字段也被删除.您必须使用连接字段而不是父/子.

父/子关系要求有两种不同的类型,并且两种类型都在同一索引中定义.既然您不能在一个索引中拥有多个类型,则无法以与5.x和先前版本中支持的方式相同的方式支持父/子关系.

您可以参考联接字段文档,了解如何对父/子关系执行类似操作.但是现在,您必须在同一类型的单个Elasticsearch索引中定义两种类型的文档.请参阅示例,该示例解释了如何使用此处的连接字段来模拟"1对多"关系(1个问题,与该问题相关的多个答案).

编辑:

使用Elasticsearch 6.x更新的示例join field如下所示.

删除现有索引(如果存在).

curl -XDELETE "http://localhost:9200/series"
Run Code Online (Sandbox Code Playgroud)

使用连接字段创建新索引,建立特许经营电影之间的连接关系:

curl -XPUT "http://localhost:9200/series" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "doc": {
      "properties": {
        "join_field": { 
          "type": "join",
          "relations": {
            "franchise": "film" 
          }
        }
      }
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

您的示例中更新的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"} }
'
Run Code Online (Sandbox Code Playgroud)

上面的批量请求创建了一个与该特许经营相关的特许经营和多部电影.

要查询具有特许经营权ID = 1的所有电影,请使用以下父ID查询.

curl -XGET "http://localhost:9200/series/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "parent_id": { 
      "type": "film",
      "id": "1"
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

  • 嗨,我已将示例转换为在6.0上运行.在链接中找到的series.json已更新(仅次要更新),对于6.0,我们必须在series.json的所有语句中使用"routing":"1"而不是"parent":"1"等 (2认同)