ElasticSearch-分詞&安裝ik分詞器&自定義分詞庫(kù)&SpringBoot整合
一個(gè)tokenizer(分詞器)接收一個(gè)字符流,將之分割為獨(dú)立的tokens(詞元,通常是獨(dú)立的單詞),然后輸出tokens流。
例如:whitespace tokenizer遇到空白字符時(shí)分割文本。它會(huì)將文本"Quick brown fox!"分割為[Quick,brown,fox!]
該tokenizer(分詞器)還負(fù)責(zé)記錄各個(gè)terms(詞條)的順序或position位置(用于phrase短語和word proximity詞近鄰查詢),以及term(詞條)所代表的原始word(單詞)的start(起始)和end(結(jié)束)的character offsets(字符串偏移量)(用于高亮顯示搜索的內(nèi)容)。
elasticsearch提供了很多內(nèi)置的分詞器(標(biāo)準(zhǔn)分詞器),可以用來構(gòu)建custom analyzers(自定義分詞器)。
關(guān)于分詞器: https://www.elastic.co/guide/en/elasticsearch/reference/7.6/analysis.html
但是對(duì)于中文,我們需要安裝額外的分詞器
安裝IK分詞器
所有的語言分詞,默認(rèn)使用的都是“Standard Analyzer”,但是這些分詞器針對(duì)于中文的分詞,并不友好。為此需要安裝中文的分詞器。
注意:不能用默認(rèn)elasticsearch-plugin install xxx.zip 進(jìn)行自動(dòng)安裝。IK分詞器的版本跟ES的版本一一對(duì)應(yīng)
https://github.com/medcl/elasticsearch-analysis-ik/releases
在前面安裝的elasticsearch時(shí),我們已經(jīng)將elasticsearch容器的“/usr/share/elasticsearch/plugins”目錄,映射到宿主機(jī)的“ /mydata/elasticsearch/plugins”目錄下,所以比較方便的做法就是下載“/elasticsearch-analysis-ik-7.4.2.zip”文件,然后解壓到該文件夾下即可。安裝完畢后,需要重啟elasticsearch容器。
測(cè)試分詞器
GET _analyze
{
"analyzer": "ik_smart",
"text":"我是中國(guó)人"
}
GET _analyze
{
"analyzer": "ik_max_word",
"text":"我是中國(guó)人"
}
自定義詞庫(kù)
比如我們要把笑一笑算作一個(gè)詞
- 修改/usr/share/elasticsearch/plugins/ik/config中的IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 擴(kuò)展配置</comment>
<!--用戶可以在這里配置自己的擴(kuò)展字典 -->
<entry key="ext_dict"></entry>
<!--用戶可以在這里配置自己的擴(kuò)展停止詞字典-->
<entry key="ext_stopwords"></entry>
<!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展字典 -->
<entry key="remote_ext_dict">http://192.168.56.10/es/fenci.txt</entry>
<!--用戶可以在這里配置遠(yuǎn)程擴(kuò)展停止詞字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
修改完成后,需要重啟elasticsearch容器,否則修改不生效。docker restart elasticsearch
更新完成后,es只會(huì)對(duì)于新增的數(shù)據(jù)用更新分詞。歷史數(shù)據(jù)是不會(huì)重新分詞的。如果想要?dú)v史數(shù)據(jù)重新分詞,需要執(zhí)行:
POST my_index/_update_by_query?conflicts=proceed
遠(yuǎn)程擴(kuò)展詞典
搭配nginx做遠(yuǎn)程擴(kuò)展詞典
# 遠(yuǎn)程擴(kuò)展詞典路徑http://192.168.56.10/es/fenci.txt
mkdir /mydata/nginx/html/es
cd /mydata/nginx/html/es
vim fenci.txt
輸入 笑一笑
測(cè)試效果:
GET _analyze
{
"analyzer": "ik_max_word",
"text":"我是笑一笑"
}
SpringBoot整合
- 導(dǎo)入依賴,這里的版本要和所按照的ELK版本匹配
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.4.2</version>
</dependency>
- 在spring-boot-dependencies中所依賴的ES版本位6.8.5,要改掉
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.4.2</elasticsearch.version>
</properties>
請(qǐng)求測(cè)試項(xiàng),比如es添加了安全訪問規(guī)則,訪問es需要添加一個(gè)安全頭,就可以通過requestOptions設(shè)置
官方建議把requestOptions創(chuàng)建成單實(shí)例
@Configuration
public class GuliESConfig {
public static final RequestOptions COMMON_OPTIONS;
static {
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
COMMON_OPTIONS = builder.build();
}
@Bean
public RestHighLevelClient esRestClient() {
RestClientBuilder builder = null;
// 可以指定多個(gè)es
builder = RestClient.builder(new HttpHost(host, 9200, "http"));
RestHighLevelClient client = new RestHighLevelClient(builder);
return client;
}
}