# Making doc \_version searchable

**URL:** https://discuss.elastic.co/t/making-doc-version-searchable/293198
**Category:** Elasticsearch
**Tags:** painless
**Created:** [December 30, 2021, 10:44am UTC](https://discuss.elastic.co/t/making-doc-version-searchable/293198 "2021-12-30T10:44:05Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Metalmacher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/metalmacher/32/104108_2.png) [@Metalmacher](https://discuss.elastic.co/u/Metalmacher)
#### Post date: [December 30, 2021, 10:44am UTC](https://discuss.elastic.co/t/making-doc-version-searchable/293198/1 "2021-12-30T10:44:06Z")

</div>

Hello. I would like to query my documents in my index based on their \_version.  
I have tried a couple of solutions but none of them worked, hopefully you might be able to assist me with this.  
My attempts are:

1. Executing an \_update\_by\_query request on all documents and assigning \_version to a new, indexable field:

```auto
POST testing_index/_update_by_query
{
  "query": {
    "match_all": {}
  }, 
  "script": {
    "lang": "painless",
    "source": "ctx._source['version'] = ctx._version;"
  }
}

```

However the value in 'version' shows -1 for all documents.  
2. Creating an ingest pipeline with a script processor (also tried a "set" processor)

```auto
PUT _ingest/pipeline/version_searchable
{
    "description" : "Moves _version to indexable field version",
    "processors" : [
      {
        "script" : {
          "source" : "ctx.version = ctx._version;"
        }
      }
    ]
}
POST testing_index/_update_by_query?pipeline=version_searchable

```

And in this attempt the version shows -3 oddly enough.

Am I doing something wrong here? Or is there maybe a better way of accomplishing this?  
Appreciate the assistance 🙂

---

<div class="post-metadata">

### Author: ![Metalmacher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/metalmacher/32/104108_2.png) [@Metalmacher](https://discuss.elastic.co/u/Metalmacher)
#### Post date: [January 3, 2022, 10:34am UTC](https://discuss.elastic.co/t/making-doc-version-searchable/293198/2 "2022-01-03T10:34:37Z")

</div>

Wanted to update, I've discovered one solution so far which is to use the \_update endpoint. Version is specified there correctly, so what I'm doing right now is scrolling through the entirety of my index and bulking \_update calls with a painless script to add version as an indexable field. I've had to run this flow from the client, using a nodejs server. This here's the code for anyone who's interested:

```auto
const setVersionSearchable = async () => {
        const timeout = '30s';
	const responseQueue = [await client.search({
		index: myIndex,
		scroll: timeout,
		size: 10000,
		_source: ['_id'],
		body: {
			query: {
				match_all: {}
			}
		}
	})];
	while (responseQueue.length) {
		const { body } = responseQueue.shift();
		if (body.hits.hits.length) {
			const bulkUpdates = body.hits.hits.flatMap((doc) => [
				{ update: { _id: doc._id, _index: myIndex } },
				{
					script: {
						lang: 'painless',
						source: 'ctx._source[\'version\'] = ctx._version;'
					}
				}
			]);
			await client.bulk({
				refresh: true,
				body: bulkUpdates
			});
			responseQueue.push(await client.scroll({
				scrollId: body._scroll_id,
				scroll: timeout
			}));
		}
	}
};

```

---

<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: [January 31, 2022, 10:35am UTC](https://discuss.elastic.co/t/making-doc-version-searchable/293198/3 "2022-01-31T10:35:03Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
