2015-06-04 15 views
0

我定義爲以下類型中流利API單元測試的示例的方案映射(FluentMappingFullExampleTests)像這樣:某個類型的編程PUT映射未被使用?

_client.Map<SomeType>(m => m 
      .Type("mytype") 
      ... 

我然後通過調用添加SOMETYPE的實例,以索引等

_client.Index<SomeType>(instance) 

但是,當我去尋找一個實例時,我沒有找到'mytype'的任何實例;相反,有一個'sometype'的實例,並且爲'sometype'創建了一個新的類型映射。我希望在執行插入操作時可以遵守PUT映射。

我沒有使用PUT映射他們應該使用的方式嗎?不幸的是,單元測試沒有顯示出往返,所以我不確定是否還有其他我應該做的事情。

編輯:它值得一提的是,我試圖實現100%編程映射,在這裏;類型上沒有NEXT屬性。

回答

0

我能處理你的使用情況在我的例子:

//request url: http://localhost:9200/indexName 
var indicesOperationResponse = client.CreateIndex(indexName); 

//request url: http://localhost:9200/indexName/document2/_mapping 
var response = client.Map<Document>(m => m 
    .Type("document2") 
    .Properties(p => p.String(s => s.Name(n => n.Name).Index(FieldIndexOption.NotAnalyzed)))); 

//request url: http://localhost:9200/indexName/document2/1 
client.Index(new Document { Id = 1, Name = "test"}); 

client.Refresh(); 

//request url: http://localhost:9200/indexName/document2/_search 
var searchResponse = client.Search<Document>(s => s.Query(q => q.MatchAll())); 

關鍵的事情是與ElasticType屬性來標記Document類:

[ElasticType(Name = "document2")] 
public class Document 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

希望這有助於你。

UPDATE

您的評論是有道理的。而不是使用ElasticType,您可以更改ElasticClient的類型名稱推斷。

var uri = new Uri("http://localhost:9200"); 
var settings = new ConnectionSettings(uri) 
    .SetDefaultIndex(indexName) 
    .MapDefaultTypeNames(d => d.Add(typeof(Document), "document2")) 

var client = new ElasticClient(settings); 

因此,我們可以從Document

public class Document 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

刪除屬性,我們不需要也映射到指定類型別名:

var response = client.Map<Document>(m => m 
    .Properties(p => p.String(s => s.Name(n => n.Name).Index(FieldIndexOption.NotAnalyzed)))); 
+0

是的,但現在包含數據的彙編類型必須針對NEST進行編譯。使用編程映射的動機是讓我們可以根本不使用ElasticSearch的「知識」來使用POCO對象,並讓PUT映射處理所有的連接。 從類型中刪除屬性會發生什麼? –

+0

@AndyHopper的權利。檢查我的更新。 – Rob

+0

啊哈!我希望在映射中提供類型別名可以實現這一點。我會給它一個鏡頭。 快速問題:此方法是否會將多個數據類型混疊到相同的名稱? –