1

I would like to be able to set up some sort of mapping using the NEST2 client so that different types are automatically put in a defined index. Is this possible?

I've tried to map types like this:

client.Map<A>(m => m.Index("index1"));
client.Map<B>(m => m.Index("index2"));

And then index them like this:

client.Index(new SomethingThatGoesToTheDefaultIndex());
client.Index(new A());//Should end up in index1
client.Index(new B());//Should end up in index2

But everything ends up in the default index and not the set index. Do I need to give the required index every time I store data, or is it possible to set up a defined index per type?

Russell Troywest
  • 8,635
  • 3
  • 35
  • 40

1 Answers1

2

You can pass index name with help of second parameter in .Index(..) method.

Just like this:

client.Index(new A(), descriptor => descriptor.Index("index1"));
client.Index(new B(), descriptor => descriptor.Index("index2"));

UPDATE

MapDefaultTypeIndices will help you to specify default index name for type.

var settings = new ConnectionSettings() 
    .MapDefaultTypeIndices(dictionary =>
    {
        dictionary.Add(typeof (A), "index1");
        dictionary.Add(typeof (B), "index2");
    });

var client = new ElasticClient(settings);

Hope it helps.

Rob
  • 9,664
  • 3
  • 41
  • 43
  • Thanks Rob. I did see I could do this but I was hoping I could centralise the code when I created the client. I don't really want to be littering index names all over the place - especially since they are different depending on the client using the software. If I have to set the index when storing then I guess that's what I'll do, but it'll be a nightmare to change if I decide to change the index pattern. – Russell Troywest Mar 02 '16 at 15:21
  • You can set up default index name with `ConnectionSettings.DefaultIndex(indexName)`. – Rob Mar 02 '16 at 15:24
  • The DefaultIndex will default for ALL types though. I need different types to go to different indexes. – Russell Troywest Mar 02 '16 at 15:26