# (Elasticsearch NEST C#) How to apply CopyTo to any field in an index

**URL:** <https://discuss.elastic.co/t/elasticsearch-nest-c-how-to-apply-copyto-to-any-field-in-an-index/248224>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [September 10, 2020, 7:09pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-c-how-to-apply-copyto-to-any-field-in-an-index/248224 "2020-09-10T19:09:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![WestCoastWondering](https://avatars.discourse-cdn.com/v4/letter/w/c67d28/32.png) [@WestCoastWondering](https://discuss.elastic.co/u/WestCoastWondering)\
**Post date:** [September 10, 2020, 7:09pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-c-how-to-apply-copyto-to-any-field-in-an-index/248224/1 "2020-09-10T19:09:14Z")

</div>

Hi,

My team is using Elasticsearch NEST to implement search within a .Net application.

The documents in this index include static text fields, such as title and description as well as dynamic fields such as field1234.

I want to use the CopyTo functionality to copy all of the field contents to a particular field (let's call it allContent) during indexing, so that I can search efficiently later.

I read this relevant discussion:

> [@Copy\_to in Nest Attributes](https://discuss.elastic.co/t/copy-to-in-nest-attributes/95724/2):
>
> You can't specify copy\_to fields with attribute mapping because multiple fields can be specified, which are represented with the Fields type, and [attribute parameters are restricted to constant values](https://msdn.microsoft.com/en-us/library/aa288454(v=vs.71).aspx), so it's not possible to assign an instance of Fields within an [Attribute] declaration. You can define copy\_to fields with fluent mapping though. Since the original \_source field is not modified, you don't need to add another property to your C# POCO for a copy\_to field. Here's an example client…

In that discussion's example by @forloop , the field to copy is explicitly named in the .properties .text portion. Is it possible to broaden this to all fields?

---

<div class="post-metadata">

**Author:** ![WestCoastWondering](https://avatars.discourse-cdn.com/v4/letter/w/c67d28/32.png) [@WestCoastWondering](https://discuss.elastic.co/u/WestCoastWondering)\
**Post date:** [September 10, 2020, 10:38pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-c-how-to-apply-copyto-to-any-field-in-an-index/248224/2 "2020-09-10T22:38:54Z")

</div>

This was first (feeble) attempt at using dynamic templates for this purpose:

```auto
EsClient.CreateIndex(name, c => c
                    .InitializeUsing(indexConfig)
                    .Mappings(m => m.Map<AppBuilder>(mp => mp.AutoMap()
                        .DynamicTemplates(dts => dts
                            .DynamicTemplate("all_strings", dt => dt
                                .MatchMappingType("string")
                                .Mapping(dtm => dtm
                                    .Text(dtmt => dtmt
                                        .Index(true)
                                        .CopyTo(ct => ct
                                            .Field("_ternarySearch"))
                                    )
                                )
                            )
                         )
                        .Properties(ps => ps
                            .Completion(cn => cn
                                .Name(p => p.Suggest)))))

```

It did not seem to work (generating content into a field named \_ternarySearch for the documents)

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [September 11, 2020, 2:17am UTC](https://discuss.elastic.co/t/elasticsearch-nest-c-how-to-apply-copyto-to-any-field-in-an-index/248224/3 "2020-09-11T02:17:19Z")

</div>

Dynamic templates is the approach to take to be able to configure [`copy_to`](https://www.elastic.co/guide/en/elasticsearch/reference/current/copy-to.html) to another field or fields for _dynamic_ fields i.e. fields whose names you don't know ahead of time and can provide explicit mapping for.

Your example looks good and produces the following request (I'm assuming you're using NEST 6.x based on the `CreateIndex()` method call)

```auto
{
  "mappings": {
    "appbuilder": {
      "dynamic_templates": [
        {
          "all_strings": {
            "mapping": {
              "type": "text",
              "copy_to": [
                "_ternarySearch"
              ],
              "index": true
            },
            "match_mapping_type": "string"
          }
        }
      ],
      "properties": {
        "suggest": {
          "type": "completion"
        }
      }
    }
  }
}

```

This will map new `string` fields within the `_source` document as `text` field mapping, index the results of running the field value through analysis against the field name in the inverted index (`"index": true`, which is the default), and also copy the field value to `"_ternarySearch"` field, index the results of running the field value through analysis and index them in the inverted index against the `"_ternarySearch"` field.

**Note that** the original `_source` document sent to Elasticsearch is not changed by this process, and is persisted verbatim as it was sent in, meaning that the `_source` returned in the hits for documents matching a query **will not contain** a `"_ternarySearch"` field, because it didn't exist on `_source`.

---

<div class="post-metadata">

**Author:** ![WestCoastWondering](https://avatars.discourse-cdn.com/v4/letter/w/c67d28/32.png) [@WestCoastWondering](https://discuss.elastic.co/u/WestCoastWondering)\
**Post date:** [September 15, 2020, 6:56pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-c-how-to-apply-copyto-to-any-field-in-an-index/248224/4 "2020-09-15T18:56:08Z")

</div>

Thank you for the detailed description. It did throw me for a loop when I didn't see the results in \_source, but after I re-read your explanation, it makes sense.

I'm getting the results I expect now for dynamic fields.

Much appreciated!

---

<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:** [October 13, 2020, 6:56pm UTC](https://discuss.elastic.co/t/elasticsearch-nest-c-how-to-apply-copyto-to-any-field-in-an-index/248224/5 "2020-10-13T18:56:12Z")

</div>

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