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

 

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