ES-09-ElasticSearch分词器

news/2023/12/2 10:02:52

说明

  • ElasticSearch分词器
  • 默认分词器(标准分词器)、ik分词器、ik分词器扩展字典自定义词语
  • 关键词:keyword、text、ik_max_word、ik_smart、词条、词典、倒排表
  • 官方文档:https://www.elastic.co/cn/
  • ik分词器文档:https://github.com/medcl/elasticsearch-analysis-ik

核心概念

》数据类型说明

  • keyword:关键词,不能被分词
  • text:普通文本,可以被分词

》分词器概念

  • 词条:索引中最小的存储和查询单元
  • 词典:字典,词条的集合。B+,hashMap
  • 倒排表:词条和文档ID的对照关系表

》默认分词器

  • 默认分词器:standard(标准分词器)
  • 默认分词器对中文不友好,默认所有中文都会被分为单个汉字

》ik分词器

  • 处理中文分词非常友好,会将中文分为词组
  • 提供了细粒度分词(ik_max_word)、粗粒度分词(ik_smart)两种选项

》ik分词器扩展字典

  • 有时候一些专用自定义词语分词器是无法正确分词的,需要我们自定义扩展字典,ik分词器提供了该功能。

操作步骤

》使用默认分词器分词

  • 使用默认分词器尝试分词一句中文

  • 请求示例

    • 请求方式:GET

    • 发送请求:

      curl -X GET http://192.168.3.201:9200/_analyze -H 'Content-Type:application/json' -d '
      {"analyzer": "standard","text": "一句中文。"
      }'
      
      • analyzer:分析器,不填默认就是standard(标准分析器)
    • 响应结果:

      {"tokens": [{"token": "一","start_offset": 0,"end_offset": 1,"type": "<IDEOGRAPHIC>","position": 0},{"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}]
      }
      

》安装ik分词器

  • 下载插件:https://github.com/medcl/elasticsearch-analysis-ik/releases/tag/v7.9.3

    [root@192 ES]# ll
    total 5124
    -rw-r--r--. 1 501 games 4504423 Jan 23 15:40 elasticsearch-analysis-ik-7.9.3.zip
    
    • 注意:下载的版本需要和你本地ES版本一致
  • 解压缩到你的ES根目录下的plugins目录下,并更改所属用户和组为es

    [root@192 plugins]# pwd
    /usr/local/es/7.9.3/plugins[root@192 plugins]# chown es:es elasticsearch-analysis-ik-7.9.3 -R[root@192 plugins]# ll
    total 0
    drwx------. 3 es es 243 Jan 23 15:41 elasticsearch-analysis-ik-7.9.3
    
  • 切换为es用户并重启ES服务(我之前已经停止,这里直接启动)

    [es@192 7.9.3]$ pwd
    /usr/local/es/7.9.3[es@192 7.9.3]$ bin/elasticsearch
    ...
    [2099-01-23T15:46:02,970][INFO ][o.e.p.PluginsService     ] [node-1] loaded plugin [analysis-ik]
    ...
    
    • 通过启动日志可以看到已经成功加载了analysis-ik

》使用ik分词器分词

  • 使用默认分词器尝试分词一句中文

  • 请求示例

    • 请求方式:GET

    • 发送请求:

      curl -X GET http://192.168.3.201:9200/_analyze -H 'Content-Type:application/json' -d '
      {"analyzer": "ik_smart","text": "一句中文。"
      }'
      
    • 响应结果:

      {"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}]
      }
      

》ik分词器扩展字典实现自定义词语分词

  • 自定义词语:粉奶方配儿幼

  • 切换到ik分词器配置文件夹

    [es@192 config]$ pwd
    /usr/local/es/7.9.3/plugins/elasticsearch-analysis-ik-7.9.3/config
    
  • 新建扩展字典文件,并加入自定义词语

    [es@192 config]$ vi custom.dic
    粉奶方配儿幼
    
  • 关联扩展字典

    • 打开配置文件:

      <?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"></entry><!--用户可以在这里配置自己的扩展停止词字典--><entry key="ext_stopwords"></entry><!--用户可以在这里配置远程扩展字典 --><!-- <entry key="remote_ext_dict">words_location</entry> --><!--用户可以在这里配置远程扩展停止词字典--><!-- <entry key="remote_ext_stopwords">words_location</entry> -->
      </properties>
      
    • 修改后的内容:

      <?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>
      
      • 注意修改了这一行:<entry key="ext_dict">custom.dic</entry>
  • 切换为es用户并重启ES服务(我之前已经停止,这里直接启动)

    [es@192 7.9.3]$ bin/elasticsearch
    
  • 请求示例

    • Postman发送GET请求到如下URL:http://192.168.3.201:9200/_analyze

    • 请求方式:GET

    • 发送请求:

      curl -X GET http://192.168.3.201:9200/_analyze -H 'Content-Type:application/json' -d '
      {"analyzer": "ik_smart","text": "粉奶方配儿幼。"
      }'
      
    • 响应结果:

      {"tokens": [{"token": "粉奶方配儿幼","start_offset": 0,"end_offset": 6,"type": "CN_WORD","position": 0}]
      }
      

》ik分词器在索引文档中使用

  • 创建一个索引:

    curl -X PUT http://192.168.3.201:9200/index001 
    
  • 给索引创建mapping:

    curl -X POST http://192.168.3.201:9200/index001/_mapping -H 'Content-Type:application/json' -d'
    {"properties": {"content": {"type": "text","analyzer": "ik_max_word","search_analyzer": "ik_smart"}}
    }'
    
  • 创建文档:

    curl -X POST http://192.168.3.201:9200/index001/_create/1 -H 'Content-Type:application/json' -d'
    {"content": "中国人民万岁。"
    }'
    
  • 高亮查询:

    curl -X POST http://192.168.3.201:9200/index001/_search  -H 'Content-Type:application/json' -d'
    {"query": {"match": {"content": "中国"}},"highlight": {"pre_tags": ["<tag1>","<tag2>"],"post_tags": ["</tag1>","</tag2>"],"fields": {"content": {}}}
    }'
    
  • 响应结果:

    {"took": 5,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": 0.2876821,"hits": [{"_index": "index001","_type": "_doc","_id": "1","_score": 0.2876821,"_source": {"content": "中国人民万岁。"},"highlight": {"content": ["<tag1>中国</tag1>人民万岁。"]}}]}
    }
    

http://www.ppmy.cn/news/131443.html

相关文章

Elasticsearch8.0

Elastic 中国社区官方博客_CSDN博客-Elastic,Elasticsearch,Kibana领域博主Elastic 中国社区官方博客擅长Elastic,Elasticsearch,Kibana,等方面的知识https://elasticstack.blog.csdn.net/ ✅ 启动 elasticsearch # cd /usr/local/elastic/elasticsearch/ # ./bin/elasticsearc…

ES6/ES7/ES8/ES9/ES10

ES10 ES10 功能完全指南 好犯困啊 我来打打字 string.prototype.matchAll() ‘Hello’.match(‘l’) eg:判断字符串中存在几个某元素 yannanna’.match(/n/g).length 扁平化多维数组&#xff08;想不出啥时候会用到&#xff09; let array [1, 2, 3, 4, 5]; array.map(x &g…

ES

文章目录 1. 什么是ElasticSearch&#xff1f;为什么要使用Elasticsearch?——克服模糊查询的缺点、查询速度快2. ES中的倒排索引是什么&#xff1f;——词→文章3. ES是如何实现master选举的&#xff1f;——各节点分别排序投票4. 如何解决ES集群的脑裂问题——增大最少候选节…

es 客户端

ES客户端&#xff1a;Elasticsearch Clients 语言无关性 Java REST ClientJava APIPython APIGo API.Net APIPHP APIJavaScripts APIRuby APIPerl APIElandRustCommunity Contributed Clients Java API 生命周期&#xff08;生卒年&#xff1a;ES 0.9 - ES 7.x&#xff09;…

ES7,ES8,ES10新特性

ES7 ES7在ES6的基础上增加了三项内容 求幂运算符 ** console.log(3 ** 2 ) // 9 Array.prototype.includes()方法 includes()的作用是查找一个值在不在数组中&#xff0c;接受两个参数&#xff1a;搜索值和搜索的开始索引。如果没有传递参数默认的索引是0 // 下面的这两种方…

ES7+ES8

撰文为何 身为一个前端开发者&#xff0c;ECMAScript(以下简称ES)早已广泛应用在我们的工作当中。了解ECMA机构流程的人应该知道&#xff0c;标准委员会会在每年的6月份正式发布一次规范的修订&#xff0c;而这次的发布也将作为当年的正式版本。以后的改动&#xff0c;都会基于…

elasticsearch系列七:ES Java客户端-Elasticsearch Java client

一、ES Client 简介 1. ES是一个服务&#xff0c;采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API &#xff0c;端口 9200 这种连接方式对应于架构图中的RESTful style API这一层&#xff0c;这种客户端的连接方式是RESTful风格的&#xff0c;使用http…

【阅读理解】ES7/ES8/ES9/ES10新特性

今天阅读了一篇咨询&#xff0c;有关于ES7-ES10 &#xff08;ES2016-2019&#xff09;&#xff0c;ES6后新出的特性比较频繁。 首先附上思维导图 下面都是我阅读咨询后理解而编写的&#xff1a; ES7&#xff1a; 1.Array.prototype.includes() 这个方法可以判断一个元素…

ES7.8 安装

环境 CentOS7.4 elasticsearch-7.8.0 jdk8 下载Linux版本的elasticsearch安装包 https://www.elastic.co/cn/downloads/past-releases 安装集群在每个节点上的安装步骤基本上都是一样的&#xff0c;我以一个节点为例 下载完成之后通过ftp上传到linux服务器指定目录下&am…

ES7、ES8、ES9、ES10、ES11新特性

一、ES7新特性 1. Array.prototype.includes includes 方法用来检测数组中是否包含某个元素&#xff0c;返回布尔值 2. 指数操作符 指数运算符 ** &#xff0c;用来实现幂运算&#xff0c;功能与 Math.pow 结果相同 二、ES8新特性 1. async 和 await async 和 await 两种…

ES7、ES8、ES9、ES10新特性

ES7新特性 1.Array.prototype.includes()方法 在ES6中我们有String.prototype.includes()可以查询给定字符串是否包含一个字符,而在 ES7 中,我们在数组中也可以用 Array.prototype.includes 方法来判断一个数组是否包含一个指定的值,根据情况,如果包含则返回true,否则返…

ES7-ES12

includes fromIndex 可选, 从fromIndex 索引处开始查找 valueToFind。如果为负值&#xff08;即从末尾开始往前跳 fromIndex 的绝对值个索引&#xff0c;然后往后搜寻&#xff09;。默认为 0。 arr.includes(valueToFind,[fromIndex]) 想求2的10次方 console.log(Math.pow…

前端ES系列

【 ES6~ES13】 文章目录 【 ES6~ES13】1、ES6常用的特性一.var、let、const之间的区别以及和闭包的关联二.箭头函数和普通函数的区别以及this的指向三.Promise1.简述Promise原理2.Promise的方法then,catch,finally3.async 和 await 四.Proxy 和defineProperty 以及两者区别使用…

ES7和 ES8 一览

ES7 Array.prototype.includes 在es5 或者 es6 中我们要判断数组中是否包含某个元素我们需要用到Array.prototype.indexOf,在es7中加入了 arr.includes(searchElement, fromIndex) 以前我们需要这么写 let arr [react, angular, vue] // Correct if (arr.indexOf(react) !…

ES7与ES8特性

我曾写过一篇关于ES6博客《10个最佳ES6特性》&#xff0c;这次我打算聊聊ES7和ES8特性。 ES7只有2个特性: includes()指数操作符 ES8尚未发布(2017年1月)&#xff0c;下面是它已经完成起草的一些特性&#xff1a; Object.values()Object.entries()padStart()padEnd()Object…

ES7, ES8

文章目录 前言一、ES7&#xff08;一&#xff09; includes() 二、ES8&#xff08;一&#xff09;Object.values()1. 意义&#xff1a;遍历对象对的属性值&#xff0c;需要通过属性名key去获取属性值2. 例如&#xff1a;-->ES8前-->ES8后 &#xff08;二&#xff09;Obje…

Elasticsearch:使用标准 Java HTTP 的集成 - Elastic Stack 8.x

Elasticsearch 功能可以通过多种方式轻松集成到任何 Java 应用程序中&#xff0c;通过 REST API 或通过本机 API。 在 Java 中&#xff0c;很容易使用许多可用库之一调用 REST HTTP 接口&#xff0c;例如 Apache HttpComponents 客户端&#xff08;有关更多信息&#xff0c;请参…

ES6, ES7, ES8, ES9 以及 ES10 新特征

目录 1. ES6 新特征 (2015) 1.1 module 1.1.1 export 1.1.2 import 1.2 Arrow function (箭头函数) 1.2.1 箭头函数结构 1.3 默认参数 1.4 模板字符串 1.5. 解构赋值 1.5.1数组的结构赋值 1.5.2 对象解构赋值 1.6 扩展运算符 1.7Promise(异步) 2 ES7新特征 (2016…

【ES】elasticsearch常见报错(服务端)

elasticsearch常见报错 _search 操作响应错误Cannot search on field [xxxxx] since it is not indexed.unknown type for collapse field ‘xxx’, only keywords and numbers are accepted _search 操作响应错误 Cannot search on field [xxxxx] since it is not indexed. …

ES新语法ES7、ES8、ES9、ES10新特性

ES7新特性 1.Array.prototype.includes()方法 //直观判断数组中是否包含一个元素,&#xff0c;如果包含则返回true&#xff0c;否则返回false。 const arr [1, 3, 5, 2, 8, NaN, -0] arr.includes(1) // true arr.includes(1, 2) // false 该方法的第二个参数表示搜索的起始…
最新文章