2016-03-02 13 views
1

我希望能夠使用NEST2客戶端設置某種映射,以便不同類型自動放入定義的索引中。這可能嗎?使用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 

但是,一切都在默認的指標,而不是一套指數結束了。每次存儲數據時是否需要提供所需的索引,或者是否可以爲每種類型設置定義的索引?

回答

2

您可以在.Index(..)方法中藉助第二個參數傳遞索引名稱。

就像這樣:

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

UPDATE

MapDefaultTypeIndices將幫助您類型指定默認的索引名。

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

var client = new ElasticClient(settings); 

希望它有幫助。

+0

感謝羅布。我確實看到我可以這樣做,但我希望在創建客戶端時能夠集中代碼。我並不是真的想亂丟索引名字 - 特別是因爲它們根據使用軟件的客戶端而不同。如果我在存儲時必須設置索引,那麼我想這就是我要做的,但如果我決定更改索引模式,那將會是一場噩夢。 –

+0

您可以使用'ConnectionSettings.DefaultIndex(indexName)'設置默認索引名稱。 – Rob

+0

儘管DefaultIndex對所有類型都是默認的。我需要不同的類型去不同的索引。 –

相關問題