2015-10-19 71 views
0

「我正在嘗試使用ElasticSearch和它的NEST .NET客戶端在自動完成搜索的索引中創建我正在關注http://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch的教程並已運行。成問題,創建索引和它的設置時Sepcifically我想創建具有以下設置的指數(取直出本教程的TL的; DR,首先):如何使用嵌套將數組值設置屬性添加到ES索引

PUT /test_index 
{ 
    "settings": { 
    "analysis": { 
     "filter": { 
     "edge_ngram_filter": { 
      "type": "edge_ngram", 
      "min_gram": 2, 
      "max_gram": 20 
     } 
    }, 
    "analyzer": { 
     "edge_ngram_analyzer": { 
      "type": "custom", 
      "tokenizer": "standard", 
      "filter": [ 
       "lowercase", 
       "edge_ngram_filter" 
      ] 
     } 
    } 
    } 
} 

我的問題是我不知道如何發送"filter": [ "lowercase", "edge_ngram_filter" ]部分使用NEST。我目前的嘗試看起來像這樣:

esclient.CreateIndex("test_index", s => s 
    .Settings(settings => settings 
     .Add("analysis.filter.edge_ngram_filter.type", "edge_ngram") 
     .Add("analysis.filter.edge_ngram_filter.min_gram", "2") 
     .Add("analysis.filter.edge_ngram_filter.max_gram", "20") 
     .Add("analysis.analyzer.edge_ngram_analyzer.type", "custom") 
     .Add("analysis.analyzer.edge_ngram_analyzer.tokenizer", "standard") 
     // Interesting part on the line below! 
     .Add("analysis.analyzer.edge_ngram_analyzer.filter", new string[] { "lowercase", "edge_ngram_filter" }) 
    ) 
); 

但是這失敗了一個JsonWriterException:「不支持的類型:System.String []。使用JsonSerializer類獲取對象的JSON表示。路徑「settings.index」。「我試圖簡單地手動提供JSON陣列(.Add("analysis.analyzer.edge_ngram_analyzer.filter", "[ \"lowercase\", "\edge_ngram_analyzer\" ]"))的JSON代替轉義爲一個字符串。

是否NEST API提供這樣的方法嗎?

回答

1

UpdateSettings(..)有更愉快的語法來設置分析設置

這是你如何處理您的案件:。

client.UpdateSettings(s => s 
    .Index(indexName) 
    .Analysis(a => a 
     .TokenFilters(f => f.Add("edge_ngram_filter", new EdgeNGramTokenFilter 
     { 
      MinGram = 2, 
      MaxGram = 20 
     })) 
     .Analyzers(analyzer => analyzer.Add("edge_ngram_analyzer", new CustomAnalyzer 
     { 
      Tokenizer = "standard", 
      Filter = new List<string> {"lowercase", "edge_ngram_filter"} 
     })))); 

記得關閉索引之前,你會更新索引設置

希望它有幫助。

更新:

您也可以在索引創建過程中實現此目的。無需更新設置。

client.CreateIndex(indexName, i => i 
    .Analysis(a => a 
     .TokenFilters(f => f.Add("edge_ngram_filter", new EdgeNGramTokenFilter 
     { 
      MinGram = 2, 
      MaxGram = 20 
     })) 
     .Analyzers(analyzer => analyzer.Add("edge_ngram_analyzer", new CustomAnalyzer 
     { 
      Tokenizer = "standard", 
      Filter = new List<string> {"lowercase", "edge_ngram_filter"} 
     })))); 
相關問題