# How to index docs using Scan and Scroll

**URL:** <https://discuss.elastic.co/t/how-to-index-docs-using-scan-and-scroll/34515>\
**Category:** Elasticsearch\
**Created:** [November 13, 2015, 1:35pm UTC](https://discuss.elastic.co/t/how-to-index-docs-using-scan-and-scroll/34515 "2015-11-13T13:35:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![r.ganeshbabu](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r.ganeshbabu](https://discuss.elastic.co/u/r.ganeshbabu)\
**Post date:** [November 13, 2015, 1:35pm UTC](https://discuss.elastic.co/t/how-to-index-docs-using-scan-and-scroll/34515/1 "2015-11-13T13:35:11Z")

</div>

Hi All,

I have gone through scan & scroll documentation in elasticsearch where we can able to copy docs from one index to another index and I tried to do that by the following command.

POST /es\_item/\_search?scroll=10m&search\_type=scan  
{  
"query": { "match\_all": {}}  
}

I got the following response in sense

{  
"\_scroll\_id": "c2Nhbjs1OzE0Okp2WmZocUZLU2N2t0ck0tNV9UUEE7MTA6ODJqSEl4X3BTRENISVAwSE8xRzQtdzsxNjpKdlpmaHFGS1NjT09rdHJNLTVfVFBBOzExOjgyakhJeF9wU0RDSElQMEhPMUc0LXc7MTU6SnZaZmhxRktTY09Pa3RyTS01X1RQQTsxO3RvdGFsX2hpdHM6MTIxNjU2Ow==",  
"took": 3,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 121656,  
"max\_score": 0,  
"hits": []  
}  
}

When I use GET command

GET \_search/scroll?scroll=1m&scroll\_id=c2Nhbjs1Ozk6SnZaZmhxRktTY09Pa3RyTS01X1RQQTs2OjgyakhJeF9wU0RDSElQMEhPMUc0LXc7ODpKdlpmaGS1NjT09rdHJNLTVfVFBBOzc6ODJqSEl4X3BTRENISVAwSE8xRzQtdzsxMDpKdlpmaHFGS1NjT09rdHJNLTVfVFBBOzE7dG90YWxfaGl0czoxMjE2NTY7

I can able to see the list of items

sample output:-

{  
"\_scroll\_id": "c2Nhbjs1OzE3Okp2WmZocUZLU2NPT2t0ck0tNV9UUEE7MTI6ODJqSEl4X3BTRENISVAwSE8xRzQtdzsxOTpKdlpmaHFGS1NjT09rdHJNLTVfVFBBOzEzOjgyakhJeF9wU0RDSElQMEhPMUc0LXc7MTg6SnZaZmhxRktTY09Pa3RyTS01X1RQQTsxO3RvdGFsX2hpdHM6MTIxNjU2Ow==",  
"took": 4,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 121656,  
"max\_score": 0,  
"hits": [  
{  
"\_index": "es\_item",  
"\_type": "item",  
"\_id": "16636281",  
"\_score": 0,  
"\_source": {  
"ITEM\_ID": 16636281,  
"ITEM\_CODE": "16636281",  
"ITEM\_DSCR": null,  
"ITEM\_SPECIFICITY\_REF\_ID": 184,  
"ITEM\_TYPE": "STANDARD ITEM",  
}  
}]  
}}

Can you give some detailed info or sample code of how to index docs by using the scroll id?

It would be very helpful.

Thanks ,  
Ganeshbabu R

---

<div class="post-metadata">

**Author:** ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)\
**Post date:** [November 13, 2015, 9:56pm UTC](https://discuss.elastic.co/t/how-to-index-docs-using-scan-and-scroll/34515/2 "2015-11-13T21:56:01Z")

</div>

Sounds like you're trying to reindex some documents. You can't directly index/reindex docs by a search result, e.g. something contained in a scroll.

What you can do is to perform the query/scan and, as you scroll through, load the \_source of each hit to be reindexed, best case through the bulk API. So pseudocode for each scroll becomes

```
bulk_index = []
foreach hit in hits
  //modify hit if necessary, e.g. change the index it's going into
  bulk_index[] = hit
  if (bulk_index.length > some_max)
    elasticsearch.index_bulk(bulk_index)
    bulk_index = []
  endif
endfor
elasticsearch.index_bulk(bulk_index)

```

There are tools out there to do this for you, including [the reindex helper in elasticsearch-py](http://elasticsearch-py.readthedocs.org/en/master/helpers.html#reindex)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 11:38pm UTC](https://discuss.elastic.co/t/how-to-index-docs-using-scan-and-scroll/34515/3 "2017-07-05T23:38:31Z")

</div>


