# Copy\_to in Nest Attributes

**URL:** <https://discuss.elastic.co/t/copy-to-in-nest-attributes/95724>\
**Category:** Elasticsearch\
**Created:** [August 3, 2017, 1:35pm UTC](https://discuss.elastic.co/t/copy-to-in-nest-attributes/95724 "2017-08-03T13:35:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mjgasior](https://avatars.discourse-cdn.com/v4/letter/m/a587f6/32.png) [@mjgasior](https://discuss.elastic.co/u/mjgasior)\
**Post date:** [August 3, 2017, 1:35pm UTC](https://discuss.elastic.co/t/copy-to-in-nest-attributes/95724/1 "2017-08-03T13:35:37Z")

</div>

Is it possible to use copy\_to in Nest attributes? I know, there is an option to use the [Fluent Mapping](https://stackoverflow.com/questions/33851566/combining-attribute-fluent-mapping-for-copy-to-with-nest) (but the CopyTo doesn't work with string) instead of Attribute Mapping. And if I do make a "copy\_to", do I have to add another field to my Mapping class as a placeholder? These are my mappings:

```
[ElasticsearchType(Name = "project")]
public class Project
{
	public Guid Id { get; set; }
	public bool IsOpen { get; set; }
	public bool IsDeployed { get; set; }
            
    [Text(Analyzer = "substring_analyzer")]
	public string ExternalCode { get; set; }
	public Owner Owner { get; set; }
}

public class Owner
{
	public string FirstName { get; set; }
	public string LastName { get; set; }
}

```

And initialization:

```
client.CreateIndex(INDEX_NAME, descriptor => descriptor
	.Mappings(ms => ms
		.Map<Project>(m => m.AutoMap())
	)
	.Settings(s => s
		.Analysis(a => a
			.Analyzers(analyzer => analyzer
				.Custom("substring_analyzer", analyzerDescriptor => analyzerDescriptor
					.Tokenizer("keyword")
					.Filters("lowercase", "substring")
				)
			)
			.TokenFilters(tf => tf
				.NGram("substring", filterDescriptor => filterDescriptor
					.MinGram(1)
					.MaxGram(9)
				)
			)
		)
	)
);

```

I'd like to use the copy\_to option on Owner.FirstName, Owner.LastName and the ExternalCode fields, and than add the NGram tokenizer to the whole stuff.

---

<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:** [August 9, 2017, 1:25pm UTC](https://discuss.elastic.co/t/copy-to-in-nest-attributes/95724/2 "2017-08-09T13:25:10Z")

</div>

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.CreateIndex("my-index", descriptor => descriptor
	.Mappings(ms => ms
		.Map<Project>(m => m
			.AutoMap()
			.Properties(p => p
				.Text(t => t
					.Name(n => n.ExternalCode)
					.Fields(f => f
						.Keyword(k => k
							.Name("keyword")
							.IgnoreAbove(256)
						)
					)
					.CopyTo(c => c
						.Field("another_field")
					)
				)
				.Object<Owner>(o => o
					.Name(n => n.Owner)
					.AutoMap()
					.Properties(pp => pp
						.Text(t => t
							.Name(n => n.FirstName)
							.Fields(f => f
								.Keyword(k => k
									.Name("keyword")
									.IgnoreAbove(256)
								)
							)
							.CopyTo(c => c
								.Field("another_field")
							)
						)
						.Text(t => t
							.Name(n => n.LastName)
							.Fields(f => f
								.Keyword(k => k
									.Name("keyword")
									.IgnoreAbove(256)
								)
							)
							.CopyTo(c => c
								.Field("another_field")
							)
						)
					)
				)
			)
		)
	)
	.Settings(s => s
		.Analysis(a => a
			.Analyzers(analyzer => analyzer
				.Custom("substring_analyzer", analyzerDescriptor => analyzerDescriptor
					.Tokenizer("keyword")
					.Filters("lowercase", "substring")
				)
			)
			.TokenFilters(tf => tf
				.NGram("substring", filterDescriptor => filterDescriptor
					.MinGram(1)
					.MaxGram(9)
				)
			)
		)
	)
);

```

Copy `ExternalCode`, `Owner.FirstName` and `Owner,LastName` to the `"another_field"` field. Now, if we index the following

```
client.IndexMany(new[] 
{
	new Project
	{
		Id = Guid.NewGuid(),
		ExternalCode = "foo",
	},
	new Project
	{
		Id = Guid.NewGuid(),
		Owner = new Owner
		{
			FirstName = "bar",
			LastName = "baz"		
		}
	}
}, "my-index");

client.Refresh("my-index");

```

We can search on the `"another_field"` field

```
client.Search<Project>(s => s
	.Index("my-index")
	.Query(q => q
		.Match(m => m
			.Field("another_field")
			.Query("foo bar baz")
		)
	)
);

```

which yields

```
{
  "took" : 64,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 2,
    "max_score" : 0.51623213,
    "hits" : [
      {
        "_index" : "my-index",
        "_type" : "project",
        "_id" : "26b9cbf6-24b8-45d1-ae63-4b3061c2ecc3",
        "_score" : 0.51623213,
        "_source" : {
          "id" : "26b9cbf6-24b8-45d1-ae63-4b3061c2ecc3",
          "isOpen" : false,
          "isDeployed" : false,
          "owner" : {
            "firstName" : "bar",
            "lastName" : "baz"
          }
        }
      },
      {
        "_index" : "my-index",
        "_type" : "project",
        "_id" : "44aae698-10bf-4771-afc4-ac822c630e0d",
        "_score" : 0.2876821,
        "_source" : {
          "id" : "44aae698-10bf-4771-afc4-ac822c630e0d",
          "isOpen" : false,
          "isDeployed" : false,
          "externalCode" : "foo"
        }
      }
    ]
  }
}
```

---

<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:** [September 6, 2017, 1:25pm UTC](https://discuss.elastic.co/t/copy-to-in-nest-attributes/95724/3 "2017-09-06T13:25:16Z")

</div>

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