2017-09-12 90 views
0

我已成功爲我的索引創建了一個描述符。已經創建了索引。生成的響應是有效的響應。 我不確定以後如何使用描述符。如何使用CreateMappingDescriptor爲使用NEST客戶端進行彈性搜索創建索引?

var defaultIndex = "mynestclient"; 
var node = new Uri("http://localhost:9200"); 
var settings = new ConnectionSettings(node 
      ).DefaultIndex(defaultIndex); 
     var client = new ElasticClient(settings); 
     var clientcreated= client.CreateIndex(defaultIndex); 
     Console.WriteLine("Index Created"); 

var descriptor = new CreateIndexDescriptor(defaultIndex) 
        .Settings(s => s 
        .Analysis(a => a 
         .TokenFilters(t => t 
          .NGram("my_filter", ng => ng.MaxGram(32).MinGram(1))) 
         .Analyzers(an => an 
          .Custom("my_analyzer", ca => ca 
           .Tokenizer("standard") 
           .Filters("my_filter", "lowercase") 
          ) 
         ) 
        ) 
       ) 
       .Mappings(ms => ms 
        .Map<Investor>(m => m 
         .Properties(props => props 
          .Text(t => t 
           .Name(p => p.Name) 
           .Analyzer("my_analyzer") 
          ) 
          .Text(t => t 
           .Name(p => p.SomeOtherField) 
           .Analyzer("my_analyzer")) 
          .Boolean(b => b 
           .Name(p => p.IsActive)))); 

我已經通過了文檔。使用描述符不多。 另外,如果任何人都可以指向一個圍繞NEST的文檔,指定PutMappingDescriptors和CreateIndexDescriptors之間的區別,或者類似地,這會有所幫助。 任何輸入不勝感激。

回答

2

描述符是用流暢的方式向NEST請求Elasticsearch端點的類型。然後將描述符傳遞給客戶端上的相應方法。例如,

var descriptor = new CreateIndexDescriptor(defaultIndex) 
        .Settings(s => s 
        .Analysis(a => a 
         .TokenFilters(t => t 
          .NGram("my_filter", ng => ng.MaxGram(32).MinGram(1))) 
         .Analyzers(an => an 
          .Custom("my_analyzer", ca => ca 
           .Tokenizer("standard") 
           .Filters("my_filter", "lowercase") 
          ) 
         ) 
        ) 
       ) 
       .Mappings(ms => ms 
        .Map<Investor>(m => m 
         .Properties(props => props 
          .Text(t => t 
           .Name(p => p.Name) 
           .Analyzer("my_analyzer") 
          ) 
          .Text(t => t 
           .Name(p => p.SomeOtherField) 
           .Analyzer("my_analyzer")) 
          .Boolean(b => b 
           .Name(p => p.IsActive)))); 

var createIndexResponse = client.CreateIndex(defaultIndex, c => descriptor); 

CreateIndex()方法的第二個參數是一個Func<CreateIndexDescriptor, ICreateIndexRequest>所以c參數傳遞給委託一個CreateIndexDescriptor,所以你可以簡單地流利地從這個代替

var createIndexResponse = client.CreateIndex(defaultIndex, c => c 
     .Settings(s => s 
     .Analysis(a => a 
      .TokenFilters(t => t 
       .NGram("my_filter", ng => ng.MaxGram(32).MinGram(1))) 
      .Analyzers(an => an 
       .Custom("my_analyzer", ca => ca 
        .Tokenizer("standard") 
        .Filters("my_filter", "lowercase") 
       ) 
      ) 
     ) 
    ) 
    .Mappings(ms => ms 
     .Map<Investor>(m => m 
      .Properties(props => props 
       .Text(t => t 
        .Name(p => p.Name) 
        .Analyzer("my_analyzer") 
       ) 
       .Text(t => t 
        .Name(p => p.SomeOtherField) 
        .Analyzer("my_analyzer")) 
       .Boolean(b => b 
        .Name(p => p.IsActive)))) 
    ) 
); 
建立的參數

帶有描述符的API設計不需要自己實例化新的描述符實例,方法需要爲您創建描述符的Func<TRequestDescriptor, TRequestInterface>,然後您只需調用需要的方法來設置屬性th在你想要的請求中,類似於你所做的.Settings().Mappings()等。這被稱爲Fluent Lambda語法。

如果Func委託和lambda表達式不是你的東西,那麼NEST也提供所有API的請求類型(也稱爲對象初始化器語法)。請求類型在每種情況下也執行相應的TRequestInterface,並且NEST客戶端上的每種方法都被重載以獲得TRequestInterface。上述對象初始化等效的例子就是

var createIndexRequest = new CreateIndexRequest(defaultIndex) 
{ 
    Settings = new IndexSettings 
    { 
     Analysis = new Analysis 
     { 
      TokenFilters = new TokenFilters 
      { 
       { "my_filter", new NGramTokenFilter 
        { 
         MaxGram = 32, 
         MinGram = 1 
        } 
       } 
      }, 
      Analyzers = new Analyzers 
      { 
       { "my_analyzer", new CustomAnalyzer 
        { 
         Tokenizer = "standard", 
         Filter = new[] {"my_filter", "lowercase" } 
        } 
       } 
      } 
     } 
    }, 
    Mappings = new Mappings 
    { 
     { typeof(Investor), new TypeMapping 
      { 
       Properties = new Properties<Investor> 
       { 
        { i => i.Name, new TextProperty 
         { 
          Analyzer = "my_analyzer" 
         } 
        }, 
        { i => i.SomeOtherField, new TextProperty 
         { 
          Analyzer = "my_analyzer" 
         } 
        }, 
        { i => i.IsActive, new BooleanProperty() } 
       } 
      } 
     } 
    } 
}; 

var createIndexResponse = client.CreateIndex(createIndexRequest); 

使用流利的lambda語法,對象初始化語法,或兩者​​兼而有之,這取決於你:)

而且,如果任何人都可以點我一個圍繞NEST的文檔,它指定了PutMappingDescriptors和CreateIndexDescriptors之間的區別,或者類似的,這會有所幫助。

PutMappingDescriptor把一個映射到現有的索引而CreateIndexDescriptor創建索引在這裏可以任選地在同一時間創建的映射。該主題在文檔中的introduction to the mapping section中進行了討論。

相關問題