Created
November 30, 2017 23:29
-
-
Save geekpete/ff8a1f99df14b1a023260ffc1166965f to your computer and use it in GitHub Desktop.
reindexing only child docs from a parent-join index to a new index in Elasticsearch 6.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# based on the v6 docs for parent-join | |
# https://www.elastic.co/guide/en/elasticsearch/reference/current/parent-join.html | |
DELETE /my_index | |
DELETE /my_index-new | |
PUT my_index | |
{ | |
"mappings": { | |
"doc": { | |
"properties": { | |
"my_join_field": { | |
"type": "join", | |
"relations": { | |
"question": "answer" | |
} | |
} | |
} | |
} | |
} | |
} | |
PUT my_index-new | |
{ | |
"mappings": { | |
"doc": { | |
"properties": { | |
"my_join_field": { | |
"type": "join", | |
"relations": { | |
"question": "answer" | |
} | |
} | |
} | |
} | |
} | |
} | |
PUT my_index/doc/1?refresh | |
{ | |
"text": "This is a question", | |
"my_join_field": "question" | |
} | |
PUT my_index/doc/2?refresh | |
{ | |
"text": "This is another question", | |
"my_join_field": "question" | |
} | |
PUT my_index/doc/3?routing=1&refresh | |
{ | |
"text": "This is an answer", | |
"my_join_field": { | |
"name": "answer", | |
"parent": "1" | |
} | |
} | |
PUT my_index/doc/4?routing=1&refresh | |
{ | |
"text": "This is another answer", | |
"my_join_field": { | |
"name": "answer", | |
"parent": "1" | |
} | |
} | |
GET /my_index/_search | |
# query to find all child docs | |
GET /my_index/_search | |
{ | |
"query": { | |
"has_parent": { | |
"parent_type": "question", | |
"query": { | |
"match_all": {} | |
} | |
} | |
} | |
} | |
# use that query to reindex | |
POST _reindex | |
{ | |
"source": { | |
"index": "my_index", | |
"query": { | |
"has_parent": { | |
"parent_type": "question", | |
"query": { | |
"match_all": {} | |
} | |
} | |
} | |
}, | |
"dest": { | |
"index": "my_index-new" | |
} | |
} | |
# only 2 docs which are the child docs in the new index | |
GET my_index-new/_search |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment