# Copy \_id field to another field for prefix query

**URL:** https://discuss.elastic.co/t/copy--id-field-to-another-field-for-prefix-query/68198
**Category:** Elasticsearch
**Created:** [December 6, 2016, 4:47pm UTC](https://discuss.elastic.co/t/copy--id-field-to-another-field-for-prefix-query/68198 "2016-12-06T16:47:41Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![tcollinsworth](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tcollinsworth](https://discuss.elastic.co/u/tcollinsworth)
#### Post date: [December 6, 2016, 4:47pm UTC](https://discuss.elastic.co/t/copy--id-field-to-another-field-for-prefix-query/68198/1 "2016-12-06T16:47:41Z")

</div>

I have an index that stores versioned schema files where the filename is entity~date~hash.schema.json and forms the unique id that is stored in the \_id field.

I want to use a copy\_to to copy the \_id field to an altId field so I don't have to rewrite other application code that creates and indexes the data. I can't figure out how to create an ES mapping that does this. Is it possible?

On 2.4 I used a prefix query with a groovy script to get the latest schema, see query A below. Finally moved up to ES 5.0 so that I could take advantage of deleteByQuery, waitfor, and refresh to have reliable tests without waiting for commit/refresh cycle. So had to rewrite queries for scripts and change field to stored\_fields - no biggie, see Query B. However, the real problem is that the prefix queries on \_id are no longer supported in 5.0.

Query A worked with 2.4:

> ```
> {
> index: 'uts_msg_body_json_schema',
> type: 'definition',
> body: {
> from: 0, size: 1,
> query: {
> prefix: { _id: "REPLACE_ME" }
> },
> sort: {
> _script: {
> script: "Double.parseDouble(doc[\"_uid\"].value.split(\"~\")[1].split(\"[.schema]\")[0].replace('v',''))",
> lang: "groovy",
> type: "number",
> order: "desc"
> }
> }
> }
> }
> 
> ```

Query B not working with 5.0 due to prefix query on \_id no longer supported,  
Need to figure out how to copy \_id to altid field:

> ```
> {
> index: 'uts_msg_body_json_schema',
> type: 'definition',
> body: {
> from: 0, size: 1,
> query: {
> prefix: { _id: "REPLACE_ME" }
> },
> sort: {
> _script: {
> type: "number",
> script: {
> lang: 'groovy',
> inline: "Double.parseDouble(doc[\"_uid\"].value.split(\"~\")[1].split(\"[.schema]\")[0].replace('v',''))"              
> },
> order: "desc"
> }
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

### Author: ![tcollinsworth](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@tcollinsworth](https://discuss.elastic.co/u/tcollinsworth)
#### Post date: [December 7, 2016, 4:52pm UTC](https://discuss.elastic.co/t/copy--id-field-to-another-field-for-prefix-query/68198/2 "2016-12-07T16:52:24Z")

</div>

Bit the bullet and added an altId field and updated the indexing to populate it. Couldn't get the copy\_to to work because it complained that \_id was already defined.

---

<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 4, 2017, 4:52pm UTC](https://discuss.elastic.co/t/copy--id-field-to-another-field-for-prefix-query/68198/3 "2017-01-04T16:52:42Z")

</div>

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