Making doc _version searchable

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:
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)

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 :slight_smile:

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:

const setVersionSearchable = async () => {
        const timeout = '30s';
	const responseQueue = [await{
		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

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