Nest for Generic Object index

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.

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

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

{
  "bar": "bar"
}
{
  "qux": "qux"
}

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