使用NEST2将类型分配给特定索引

时间:2016-03-02 14:47:17

标签: nest elasticsearch-net

我希望能够使用NEST2客户端设置某种映射,以便将不同类型自动放入已定义的索引中。这可能吗?

我试图像这样映射类型:

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

然后将它们编入索引:

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

但是一切都以默认索引而不是设置索引结束。每次存储数据时是否需要提供所需的索引,或者是否可以为每种类型设置定义的索引?

1 个答案:

答案 0 :(得分:2)

您可以在.Index(..)方法的第二个参数的帮助下传递索引名称。

就像这样:

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

更新

MapDefaultTypeIndices将帮助您指定类型的默认索引名称。

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

var client = new ElasticClient(settings);

希望它有所帮助。