# Solr's copyField equivalent in ElasticSearch

**URL:** <https://discuss.elastic.co/t/solrs-copyfield-equivalent-in-elasticsearch/302682>\
**Category:** Elasticsearch\
**Created:** [April 19, 2022, 7:40am UTC](https://discuss.elastic.co/t/solrs-copyfield-equivalent-in-elasticsearch/302682 "2022-04-19T07:40:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nn14](https://avatars.discourse-cdn.com/v4/letter/n/9e8a1a/32.png) [@nn14](https://discuss.elastic.co/u/nn14)\
**Post date:** [April 19, 2022, 7:40am UTC](https://discuss.elastic.co/t/solrs-copyfield-equivalent-in-elasticsearch/302682/1 "2022-04-19T07:40:05Z")

</div>

Hello team,  
We are migrating from Solr to ES. Please let me know if there is an equivalent of `copyField` in ES.

Basically for the below Solr code, this was done to enable date range searches on `lastUpdateDate` I need an equivalent in ES. Or let me know if we can enable date range searches in a different way

`<copyField source="updateDate" dest="lastUpdateDate"/>`

```auto
<field name="updateDate" type="date" stored="true" indexed="true" />
<field name="lastUpdateDate" stored="true" indexed="true" type="date_range" />

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 19, 2022, 9:51am UTC](https://discuss.elastic.co/t/solrs-copyfield-equivalent-in-elasticsearch/302682/2 "2022-04-19T09:51:31Z")

</div>

Hi @nn14

The " **copy\_to**" can help you?

> **[copy\_to | Elasticsearch Guide \[8.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html)**

---

<div class="post-metadata">

**Author:** ![nn14](https://avatars.discourse-cdn.com/v4/letter/n/9e8a1a/32.png) [@nn14](https://discuss.elastic.co/u/nn14)\
**Post date:** [April 19, 2022, 1:12pm UTC](https://discuss.elastic.co/t/solrs-copyfield-equivalent-in-elasticsearch/302682/3 "2022-04-19T13:12:40Z")

</div>

Hi @RabBit_BR.

I tried and this is what I have

```auto
				"lastUpdateDate": {
					"type": "date"
				},
				"updateDate": {
					"type": "date",
					"copy_to": "lastUpdateDate"
				}

```

I believe this `copy_to` field will not appear in \_source field as per the documentation. If I need to show **all the fields** in the response including the **copy field** , how should I query in the index?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 19, 2022, 1:39pm UTC](https://discuss.elastic.co/t/solrs-copyfield-equivalent-in-elasticsearch/302682/4 "2022-04-19T13:39:17Z")

</div>

Look this:

> **[store | Elasticsearch Guide \[8.1\] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-store.html)**

Set store:true for lastUpdateDate field.  
In query use "stored\_fields": ["lastUpdateDate"].

---

<div class="post-metadata">

**Author:** ![nn14](https://avatars.discourse-cdn.com/v4/letter/n/9e8a1a/32.png) [@nn14](https://discuss.elastic.co/u/nn14)\
**Post date:** [April 19, 2022, 4:31pm UTC](https://discuss.elastic.co/t/solrs-copyfield-equivalent-in-elasticsearch/302682/5 "2022-04-19T16:31:53Z")

</div>

Hi @RabBit_BR , thanks I can now see the copied field using the query you shared. I am using the below Java code to retrieve all the fields from the index. This returns all fields from \_source. How can I include copied fields in the same response. Is this something which you can provide some pointers?  
`queryString=*`

```auto
        SearchRequest searchRequest = new SearchRequest(indexName);
        SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();

        sourceBuilder.query(new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.queryStringQuery(queryString))
                .withFields()
                .build()
                .getQuery());

        searchRequest.source(sourceBuilder);
SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 19, 2022, 4:59pm UTC](https://discuss.elastic.co/t/solrs-copyfield-equivalent-in-elasticsearch/302682/6 "2022-04-19T16:59:52Z")

</div>

Try this:

```auto
    sourceBuilder.storedFields(List.of("lastUpdateDate"));

```

---

<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:** [May 17, 2022, 5:00pm UTC](https://discuss.elastic.co/t/solrs-copyfield-equivalent-in-elasticsearch/302682/7 "2022-05-17T17:00:14Z")

</div>

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