# Nest for Generic Object index

**URL:** <https://discuss.elastic.co/t/nest-for-generic-object-index/226890>\
**Category:** Elasticsearch\
**Created:** [April 7, 2020, 12:22pm UTC](https://discuss.elastic.co/t/nest-for-generic-object-index/226890 "2020-04-07T12:22:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![4ea9049c983a41c5ecf0](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/4ea9049c983a41c5ecf0/32/48265_2.png) [@4ea9049c983a41c5ecf0](https://discuss.elastic.co/u/4ea9049c983a41c5ecf0)\
**Post date:** [April 7, 2020, 12:22pm UTC](https://discuss.elastic.co/t/nest-for-generic-object-index/226890/1 "2020-04-07T12:22:23Z")

</div>

Hi All,

I want to index the .NET class object. There are multiple classes I have so want to keep the index generic, is there any way to do so in Nest ?  
I have tried dynamic but it didn't work for me.

---

<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:** [April 8, 2020, 12:25am UTC](https://discuss.elastic.co/t/nest-for-generic-object-index/226890/2 "2020-04-08T00:25:33Z")

</div>

> [@4ea9049c983a41c5ecf0](#):
>
> I want to index the .NET class object. There are multiple classes I have so want to keep the index generic,

You might want to consider indexing different C# POCO types into different indices; different types are likely to have different properties, so there's a potential to end up with sparse fields if indexing multiple different JSON structures into the same index.

If you _truly_ want to index multiple different types into one index, you can do so. You'd need some type into which all source documents can be deserialized into, such as `Json.NET`'s `JObject` type, which can be configured as the serializer to use for source documents by using the [`Nest.JsonNetSerializer` nuget package](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/custom-serialization.html#_jsonnetserializer)

```auto
private static void Main()
{
	var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
    var settings = new ConnectionSettings(pool, JsonNetSerializer.Default);
	var client = new ElasticClient(settings);

	var indexResponse = client.Index(new Foo { Bar = "bar" }, i => i
		.Index("my_index")
		.Id(1)
		.Refresh(Refresh.WaitFor)
	);

	indexResponse = client.Index(new Baz { Qux = "qux" }, i => i
		.Index("my_index")
		.Id(2)
		.Refresh(Refresh.WaitFor)
	);
	
	var searchResponse = client.Search<JObject>(s => s
		.Index("my_index")
		.Query(q => q
			.MatchAll()
		)
	);
	
	foreach(var document in searchResponse.Documents)
	{
		Console.WriteLine(document.ToString());
	}
}

public class Foo
{
    public string Bar { get; set; }
}

public class Baz
{
	public string Qux { get; set; }
}

```

which prints

```auto
{
  "bar": "bar"
}
{
  "qux": "qux"
}

```

---

<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 6, 2020, 12:25am UTC](https://discuss.elastic.co/t/nest-for-generic-object-index/226890/3 "2020-05-06T00:25:42Z")

</div>

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