Skip to content

Commit fdeb3d1

Browse files
committed
Make sure reindex preserves _parent, _routing and _timestamp
Fixes elastic#140
1 parent 5867040 commit fdeb3d1

File tree

3 files changed

+84
-1
lines changed

3 files changed

+84
-1
lines changed

Changelog.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Changelog
77
------------------
88

99
* Add ``indices.flush_synced`` API
10+
* ``helpers.reindex`` now supports reindexing parent/child documents
1011

1112
1.5.0 (2015-05-18)
1213
------------------

elasticsearch/helpers/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,18 @@ def reindex(client, source_index, target_index, query=None, target_client=None,
292292
"""
293293
target_client = client if target_client is None else target_client
294294

295-
docs = scan(client, query=query, index=source_index, scroll=scroll, **scan_kwargs)
295+
docs = scan(client,
296+
query=query,
297+
index=source_index,
298+
scroll=scroll,
299+
fields=('_source', '_parent', '_routing', '_timestamp'),
300+
**scan_kwargs
301+
)
296302
def _change_doc_index(hits, index):
297303
for h in hits:
298304
h['_index'] = index
305+
if 'fields' in h:
306+
h.update(h.pop('fields'))
299307
yield h
300308

301309
kwargs = {

test_elasticsearch/test_server/test_helpers.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from datetime import datetime
2+
13
from elasticsearch import helpers, TransportError
24

35
from . import ElasticsearchTestCase
@@ -244,3 +246,75 @@ def test_all_documents_get_moved(self):
244246
self.assertEquals(50, self.client.count(index='prod_index', doc_type='answers')['count'])
245247

246248
self.assertEquals({"answer": 42, "correct": True}, self.client.get(index="prod_index", doc_type="answers", id=42)['_source'])
249+
250+
class TestParentChildReindex(ElasticsearchTestCase):
251+
def setUp(self):
252+
super(TestParentChildReindex, self).setUp()
253+
body={
254+
'settings': {"number_of_shards": 1, "number_of_replicas": 0},
255+
'mappings': {
256+
'question': {
257+
'_timestamp': {'enabled': True, 'store': True},
258+
},
259+
'answer': {
260+
'_parent': {'type': 'question'},
261+
}
262+
}
263+
}
264+
self.client.indices.create(index='test-index', body=body)
265+
self.client.indices.create(index='real-index', body=body)
266+
267+
self.client.index(
268+
index='test-index',
269+
doc_type='question',
270+
id=42,
271+
body={},
272+
timestamp=datetime(2015, 1, 1)
273+
)
274+
self.client.index(
275+
index='test-index',
276+
doc_type='answer',
277+
id=47,
278+
body={'some': 'data'},
279+
parent=42
280+
)
281+
self.client.indices.refresh(index='test-index')
282+
283+
def test_children_are_reindexed_correctly(self):
284+
helpers.reindex(self.client, 'test-index', 'real-index')
285+
286+
self.assertEquals(
287+
{
288+
'_id': '42',
289+
'_index': 'real-index',
290+
'_source': {},
291+
'_type': 'question',
292+
'_version': 1,
293+
'fields': {'_timestamp': 1420070400000},
294+
'found': True
295+
},
296+
self.client.get(
297+
index='real-index',
298+
doc_type='question',
299+
id=42,
300+
fields=['_source', '_timestamp']
301+
)
302+
)
303+
self.assertEquals(
304+
{
305+
'_id': '47',
306+
'_index': 'test-index',
307+
'_source': {'some': 'data'},
308+
'_type': 'answer',
309+
'_version': 1,
310+
'fields': {'_parent': '42'},
311+
'found': True
312+
},
313+
self.client.get(
314+
index='test-index',
315+
doc_type='answer',
316+
id=47,
317+
parent=42,
318+
fields=['_source', '_parent']
319+
)
320+
)

0 commit comments

Comments
 (0)