2017-06-21 55 views
0

我有一個關於彈性搜索分析器的問題。 創建自定義分析是這樣的:使用自定義彈性搜索分析器

Analyzers(o => o.Custom("custom", 
         m => m.CharFilters("icu_normalizer").Filters("lowercase", "asciifolding").Tokenizer("icu_tokenizer") 

並試圖導致以下令牌(好)的分析:

/_analyze?analyzer=custom&text=SödertorG 

{ 
    "tokens": [ 
     { 
      "token": "sodertorg", 
      "start_offset": 0, 
      "end_offset": 9, 
      "type": "<ALPHANUM>", 
      "position": 0 
     } 
    ] 
} 

但是,當我試圖尋找這個道理,就像這樣:

_search?q=sodertorg&analyzer=custom 

我沒有得到任何結果(壞)。

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
     "total": 5, 
     "successful": 5, 
     "failed": 0 
    }, 
    "hits": { 
     "total": 0, 
     "max_score": null, 
     "hits": [] 
    } 
} 

我錯過了什麼嗎? 謝謝。

+0

如果您使用您存儲數據的確切字段,即'_search?q = field:sodertorg&analyzer = custom',會發生什麼? – Val

+0

沒有幫助。只要我搜索'södertorg'(或_search?q = name1:södertorg),標準分析器就會找到結果,但我的自定義分析器不起作用,儘管它適用於其他搜索(例如「jarn」作爲電子郵件字段) – Senj

+0

您的自定義分析儀存儲在哪裏? –

回答

0

當您正在執行query_string查詢時,您將定位到_all字段。查詢字符串中指定的analyzer僅適用於分析查詢的方式;內容索引時,_all字段仍將使用standard分析器。

要解決問題,您需要確保您的自定義analyzer應用於映射中的_all字段。

var createIndexResponse = client.CreateIndex("my-index", c => c 
    .Settings(s => s 
     .Analysis(a => a 
      .Analyzers(o => o 
       .Custom("custom", m => m 
        .CharFilters("icu_normalizer") 
        .Filters("lowercase", "asciifolding") 
        .Tokenizer("icu_tokenizer") 
       ) 
      ) 
     ) 
    ) 
    .Mappings(m => m 
     .Map<Project>(mm => mm 
      // apply to _all field 
      .AllField(s => s 
       .Analyzer("custom") 
      ) 
      .AutoMap() 
     ) 
    ) 
); 

隨着映射以這種方式應用的分析,你應該得到預期的結果;您不再需要在查詢中指定分析器,因爲映射中的分析器將用於索引時間和查詢時間分析。