當Elasticsearch在你的文檔中檢測到一個新的字符串域,它會自動設置其為一個全文字符串域,使用<mark>標準分析器</mark>對它進行分析。你不希望總是這樣。可能你想使用一個不同的分析器,適用于你的數據使用的語言。有時候你想要一個字符串域就是一個字符串域—不使用分析,直接索引你傳入的精確值,例如用戶 ID 或者一個內部的狀態域或標簽。要做到這一點,我們必須手動指定這些域的映射。
[TOC]
# 1. IK 分詞器
ES的默認分詞器無法識別中文單詞這樣的詞匯,而是簡單的將每個字拆為一個詞。
```json
GET /_analyze
{
"text": "測試單詞"
}
響應結果如下:
{
"tokens" : [
{
"token" : "測", # token實際存儲到索引中的詞條
"start_offset" : 0, # start_offset和end_offset指明字符在原始字符串中的位置
"end_offset" : 1,
"type" : "<IDEOGRAPHIC>",
"position" : 0 # position指明詞條在原始文本中出現的位置
},
{
"token" : "試",
"start_offset" : 1,
"end_offset" : 2,
"type" : "<IDEOGRAPHIC>",
"position" : 1
},
{
"token" : "單",
"start_offset" : 2,
"end_offset" : 3,
"type" : "<IDEOGRAPHIC>",
"position" : 2
},
{
"token" : "詞",
"start_offset" : 3,
"end_offset" : 4,
"type" : "<IDEOGRAPHIC>",
"position" : 3
}
]
}
```
這樣的結果顯然不符合我們的使用要求,所以我們需要下載 ES 對應版本的中文分詞器。
步驟如下:
**1. 下載IK分詞器**
https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.8.0

**2. 解壓到`%ES_HOME%/plugins/`目錄下**

**3. 重啟ES**
**4. 指定IK分詞器**
* `ik_max_word`:會將文本做最細粒度的拆分。
* `ik_smart`:會將文本做最粗粒度的拆分。
```json
GET /_analyze
{
"text": "測試單詞",
"analyzer":"ik_max_word"
}
響應結果如下:
{
"tokens" : [
{
"token" : "測試",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "單詞",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 1
}
]
}
```
<br/>
ES 中也可以進行擴展詞匯,下面的查詢僅僅可以得到每個字的分詞結果,我們需要做的就是使分詞器識別到弗雷爾卓德也是一個詞語。
```json
GET /_analyze
{
"text": "弗雷爾卓德",
"analyzer":"ik_max_word"
}
響應結果如下:
{
"tokens" : [
{
"token" : "弗",
"start_offset" : 0,
"end_offset" : 1,
"type" : "CN_CHAR",
"position" : 0
},
{
"token" : "雷",
"start_offset" : 1,
"end_offset" : 2,
"type" : "CN_CHAR",
"position" : 1
},
{
"token" : "爾",
"start_offset" : 2,
"end_offset" : 3,
"type" : "CN_CHAR",
"position" : 2
},
{
"token" : "卓",
"start_offset" : 3,
"end_offset" : 4,
"type" : "CN_CHAR",
"position" : 3
},
{
"token" : "德",
"start_offset" : 4,
"end_offset" : 5,
"type" : "CN_CHAR",
"position" : 4
}
]
```
使分詞器識別到弗雷爾卓德也是一個詞語,需要做如下工作。
**1. 創建`%ES_HOME%/plugins/ik分詞器目錄/config/**.dic`文件**
創建`custom.dic`(文件名自定義)文件并將需要作為中文詞語的字符串寫入文件中。

```
弗雷爾卓德
測試單詞
```
**2. 在文件`%ES_HOME%/plugins/ik分詞器目錄/config/IKAnalyzer.cfg.xml`中配置`custom.dic`文件**
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 擴展配置</comment>
<!--用戶可以在這里配置自己的擴展字典 -->
<entry key="ext_dict">custom.dic</entry>
<!--用戶可以在這里配置自己的擴展停止詞字典-->
<entry key="ext_stopwords"></entry>
<!--用戶可以在這里配置遠程擴展字典 -->
<!-- <entry key="remote_ext_dict">words_location</entry> -->
<!--用戶可以在這里配置遠程擴展停止詞字典-->
<!-- <entry key="remote_ext_stopwords">words_location</entry> -->
</properties>
```
**3. 重啟ES**
**4. 測試**
```json
GET /_analyze
{
"text": "弗雷爾卓德",
"analyzer":"ik_max_word"
}
響應結果如下:
{
"tokens" : [
{
"token" : "弗雷爾卓德",
"start_offset" : 0,
"end_offset" : 5,
"type" : "CN_WORD",
"position" : 0
}
]
}
GET /_analyze
{
"text": "測試單詞",
"analyzer":"ik_max_word"
}
響應結果如下:
{
"tokens" : [
{
"token" : "測試單詞",
"start_offset" : 0,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 0
},
{
"token" : "測試",
"start_offset" : 0,
"end_offset" : 2,
"type" : "CN_WORD",
"position" : 1
},
{
"token" : "單詞",
"start_offset" : 2,
"end_offset" : 4,
"type" : "CN_WORD",
"position" : 2
}
]
}
```
- Elasticsearch是什么
- 全文搜索引擎
- Elasticsearch與Solr
- 數據結構
- 安裝Elasticsearch
- Linux單機安裝
- Windows單機安裝
- 安裝Kibana
- Linux安裝
- Windows安裝
- es基本語句
- 索引操作
- 文檔操作
- 映射操作
- 高級查詢
- es-JavaAPI
- maven依賴
- 索引操作
- 文檔操作
- 高級查詢
- es集群搭建
- Linux集群搭建
- Windows集群搭建
- 核心概念
- 索引(Index)
- 類型(Type)
- 文檔(Document)
- 字段(Field)
- 映射(Mapping)
- 分片(Shards)
- 副本(Replicas)
- 分配(Allocation)
- 系統架構
- 分布式集群
- 單節點集群
- 故障轉移
- 水平擴容
- 應對故障
- 路由計算
- 分片控制
- 寫流程
- 讀流程
- 更新流程
- 多文檔操作流程
- 分片原理
- 倒排索引
- 文檔搜索
- 動態更新索引
- 近實時搜索
- 持久化變更
- 段合并
- 文檔分析
- 內置分析器
- 分析器使用場景
- 測試分析器
- 指定分析器
- 自定義分析器
- 文檔處理
- 文檔沖突
- 樂觀并發控制
- 外部系統版本控制
- es優化
- 硬件選擇
- 分片策略
- 合理設置分片數
- 推遲分片分配
- 路由選擇
- 寫入速度優化
- 批量數據提交
- 優化存儲設備
- 合理使用合并
- 減少Refresh的次數
- 加大Flush設置
- 減少副本的數量
- 內存設置
- 重要配置
- es常見問題
- 為什么要使用Elasticsearch
- master選舉流程
- 集群腦裂問題
- 索引文檔流程
- 更新和刪除文檔流程
- 搜索流程
- ES部署在Linux時的優化方法
- GC方面ES需要注意的點
- ES對大數據量的聚合實現
- 并發時保證讀寫一致性
- 字典樹
- ES的倒排索引
- Spring Data Elasticsearch
- 環境搭建
- 索引操作
- 文檔操作