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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
You can set up default index name with ConnectionSettings.DefaultIndex(indexName).
The DefaultIndex will default for ALL types though. I need different types to go to different indexes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.