ES使用過程中的一些問題

問題1

描述:資產編碼“01FIRSTsec012017051510110”,加上FieldIndex.not_analyzed註解後,資產搜索(“FIRSTsec01”)搜不到字母與數字的模糊組合,使用analyzer = ""註解後可以搜索到

代碼段:

//搜索不到

@Field(type = FieldType.String, index = FieldIndex.not_analyzed,store = true

private String transAssetCode;

  

//搜索到

@Field(type = FieldType.String, analyzer = "",store = true

private String transAssetCode;

原因解釋:

採用ES默認分析器的過濾結果爲:

curl -XGET 'localhost:9200/_analyze?analyzer=standard&pretty=true' -d 'FIRSTsec01' 

 

{

 

  "tokens" : [ {

 

    "token" "firstsec01",

 

    "start_offset" 0,

 

    "end_offset" 10,

 

    "type" "<ALPHANUM>",

 

    "position" 0

 

  } ]

 

}

採用默認分析器時,‘FIRSTsec01’ 在存儲進ES時被分析器抓換爲小寫的 ‘firstsec01’,代碼中我們搜索時,一律把英文字符轉換爲小寫搜索,因此搜索‘FIRSTsec01’轉換爲小寫的‘firstsec01’,可以搜出結果

而採用FieldIndex.not_analyzed註解時,默認不做分析,此時ES中存儲的是大寫的字母,因此搜索時在代碼中轉換爲小寫後搜索不到結果。


問題2:

描述:更改字段的分析器類型後,啓動報錯

原因解釋:

1.更改分析器後,ES啓動在PutMapping時,會檢查分析器類型是否一致,不一致會報錯

所以當更改分析器類型時,只能reindex所有文檔,要想做到無縫切換,可以使用別名

tips:增加字段不受影響,不需要reIndex!

參見:https://www.elastic.co/blog/changing-mapping-with-zero-downtime

ES配置相關

1.配置ES刷新間隔

curl -XPUT 'http://59.110.9.92:9200/cloud_asset/_settings' -d '{

    "index" : {

        "refresh_interval" "-1" 

    }

}'

-1代表關閉自動刷新,這個刷新是指刷新doc進入segment,doc刷新後是可搜索狀態,但並不是寫入磁盤,見https://www.elastic.co/guide/en/elasticsearch/guide/current/near-real-time.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章