首页 > 其他分享 >搜索引擎ElasticSearch18_IK 分词器和ElasticSearch集成使用3

搜索引擎ElasticSearch18_IK 分词器和ElasticSearch集成使用3

时间:2024-05-23 13:07:52浏览次数:11  
标签:type token start IK ElasticSearch 分词器 offset true

一、上述查询存在问题分析

 在进行字符串查询时,我们发现去搜索"搜索服务器"和"钢索"都可以搜索到数据;

 而在进行词条查询时,我们搜索"搜索"却没有搜索到数据;

 究其原因是ElasticSearch的标准分词器导致的,当我们创建索引时,字段使用的是标准分词器:

 
{
    "query": {
        "term": {
            "title": "搜索"
        }
    }
}
{
    "mappings": {
        "article": {
            "properties": {
                "id": {
                    "type": "long",
                    "store": true,
                    "index":"not_analyzed"
                },
                "title": {
                    "type": "text",
                    "store": true,
                    "index":"analyzed",
                    "analyzer":"standard"   //标准分词器
                },
                "content": {
                    "type": "text",
                    "store": true,
                    "index":"analyzed",
                    "analyzer":"standard"   //标准分词器
                }
            }
        }
    }
}

 例如对 "我是程序员" 进行分词

 标准分词器分词效果测试:

http://127.0.0.1:9200/_analyze?analyzer=standard&pretty=true&text=我是程序员

 分词结果: 

                    "index":"not_analyzed"
                },
                "title": {
                    "type": "text",
                    "store": true,
                    "index":"analyzed",
                    "analyzer":"standard"   //标准分词器
                },
                "content": {
                    "type": "text",
                    "store": true,
                    "index":"analyzed",
                    "analyzer":"standard"   //标准分词器
                }
            }
        }
    }
}
http://127.0.0.1:9200/_analyze?analyzer=standard&pretty=true&text=我是程序员
{
  "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
    },
    {
      "token" : "员",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "<IDEOGRAPHIC>",
      "position" : 4
    }
  ]
}

 而我们需要的分词效果是:我、是、程序、程序员

 这样的话就需要对中文支持良好的分析器的支持,支持中文分词的分词器有很多,word分词器、庖丁解牛、盘古 分词、Ansj分词等,但我们常用的还是下面要介绍的IK分词器。

二、IK分词器简介

 IKAnalyzer是一个开源的,基于java语言开发的轻量级的中文分词工具包。从2006年12月推出1.0版开始, IKAnalyzer已经推出 了3个大版本。最初,它是以开源项目Lucene为应用主体的,结合词典分词和文法分析算法的 中文分词组件。新版本的IKAnalyzer3.0则发展为 面向Java的公用分词组件,独立于Lucene项目,同时提供了对 Lucene的默认优化实现。

 IK分词器3.0的特性如下:

  1)采用了特有的“正向迭代最细粒度切分算法“,具有60万字/秒的高速处理能力。

  2)采用了多子处理器分析模 式,支持:英文字母(IP地址、Email、URL)、数字(日期,常用中文数量词,罗马数字,科学计数法),中文 词汇(姓名、地名处理)等分词处理。

  3)对中英联合支持不是很好,在这方面的处理比较麻烦.需再做一次查询,同 时是支持个人词条的优化的词典存储,更小的内存占用。

  4)支持用户词典扩展定义。  

  5)针对Lucene全文检索优 化的查询分析器IKQueryParser;采用歧义分析算法优化查询关键字的搜索排列组合,能极大的提高Lucene检索的 命中率。 

三、ElasticSearch集成IK分词器

 1、IK分词器的安装

  1)下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases 

   课程资料也提供了IK分词器的压缩包

  2)解压,将解压后的elasticsearch文件夹拷贝到elasticsearch-5.6.8\plugins下,并重命名文件夹为analysis-ik

   

  3)重新启动ElasticSearch,即可加载IK分词器 

   

 2、IK分词器测试

  IK提供了两个分词算法ik_smart 和 ik_max_word

  其中 ik_smart 为最少切分,ik_max_word为最细粒度划分

  我们分别来试一下

  1)最小切分:在浏览器地址栏输入地址

http://127.0.0.1:9200/_analyze?analyzer=ik_smart&pretty=true&text=我是程序员

  输出的结果为:

 
http://127.0.0.1:9200/_analyze?analyzer=ik_smart&pretty=true&text=我是程序员
{
  "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" : 5,
      "type" : "CN_WORD",
      "position" : 2
    }
  ]
}

  2)最细切分:在浏览器地址栏输入地址

http://127.0.0.1:9200/_analyze?analyzer=ik_max_word&pretty=true&text=我是程序员

  输出的结果为: 

{
  "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" : 5,
      "type" : "CN_WORD",
      "position" : 2
    },
    {
      "token" : "程序",
      "start_offset" : 2,
      "end_offset" : 4,
      "type" : "CN_WORD",
      "position" : 3
    },
    {
      "token" : "员",
      "start_offset" : 4,
      "end_offset" : 5,
      "type" : "CN_CHAR",
      "position" : 4
    }
  ]
}

四、修改索引映射mapping

 1、重建索引

  删除原有blog1索引

DELETE        localhost:9200/blog1

  创建blog1索引,此时分词器使用ik_max_word

PUT        localhost:9200/blog1
{
    "mappings": {
        "article": {
            "properties": {
                "id": {
                    "type": "long",
                    "store": true,
                    "index":"not_analyzed"
                },
                "title": {
                    "type": "text",
                    "store": true,
                    "index":"analyzed",
                    "analyzer":"ik_max_word"
                },
                "content": {
                    "type": "text",
                    "store": true,
                    "index":"analyzed",
                    "analyzer":"ik_max_word"
                }
            }
        }
    }
}

  创建文档

POST    localhost:9200/blog1/article/1
{
    "id":1,
    "title":"ElasticSearch是一个基于Lucene的搜索服务器",
    "content":"它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。"
}

 2、再次测试queryString查询

  请求url:

POST    localhost:9200/blog1/article/_search

  请求体:

{
    "query": {
        "query_string": {
            "default_field": "title",
            "query": "搜索服务器"
        }
    }
}

  postman截图:

   

  将请求体搜索字符串修改为"钢索",再次查询:

{
    "query": {
        "query_string": {
            "default_field": "title",
            "query": "钢索"
        }
    }
}

  postman截图:

   

 3、再次测试term测试

  请求url:

POST    localhost:9200/blog1/article/_search

  请求体:

{
    "query": {
        "term": {
            "title": "搜索"
        }
    }
}

  postman截图:

   

 

标签:type,token,start,IK,ElasticSearch,分词器,offset,true
From: https://www.cnblogs.com/ajing2018/p/18208184

相关文章

  • 搜索引擎ElasticSearch18_ElasticSearch的客户端操作2
    实际开发中,主要有三种方式可以作为elasticsearch服务的客户端:第一种,elasticsearch-head插件第二种,使用elasticsearch提供的Restful接口直接访问第三种,使用elasticsearch提供的API进行访问一、安装Postman工具Postman中文版是postman这款强大网页调试工具的windows客户端,提......
  • 搜索引擎ElasticSearch18_ElasticSearch简介1
    一、ElasticSearch简介1、什么是ElasticSearchElaticsearch,简称为es,es是一个开源的高扩展的分布式全文检索引擎,它可以近乎实时的存储、检索数据;本身扩展性很好,可以扩展到上百台服务器,处理PB级别的数据。es也使用Java开发并使用Lucene作为其核心来实现所有索引和搜索的功能,但......
  • Latex wiki
    HomeJumptobottomTakashiTamuraeditedthispageMar11,2022·4revisionsLaTeXWorkshopisanextensionforVisualStudioCode,aimingtoprovideall-in-onefeaturesandutilitiesforLaTeXtypesettingwithVisualS......
  • Elasticsearch
    Elasticsearch全文搜索引擎-PHP使用教程。 1、声明依赖关系:        比方说,你的项目中需要一个php版的elasticsearch框架。为了将它添加到你的项目中(下载),你所需要做的就是创建一个composer.json文件,其中描述了项目的依赖关系。注意文件要放在你执行composer命令......
  • minikube 搭建 k8s 单机环境
    准备linux环境uname-acat/etc/os-release查看linux环境查看linux发行版本,可以在/etc目录下找到以release结尾的文件,这个一般就是记录发行版本的文件准备docker环境需要有一个镜像打包的工具安装dockersudoyuminstalldocker如果提示没有找到软件,那么需要配置一下软......
  • 一个使用Python加密连接Elasticsearch的简单封装
    依赖:elasticsearch==7.17.9eshelpercore.py:#!/usr/bin/python3#coding=utf-8importdatetimeimportosimportsslfromelasticsearchimportElasticsearchdefget_env()->str:#这里指定查询的环境索引return"uat"defget_output_file_pat......
  • elasticsearch存储经纬度且按照范围进行查询
    elasticsearch存储经纬度且按照范围进行查询背景:我在客户那边有很多舆情事件数据,数据里面包含的是有经纬度的,项目需求是用户在系统中输入一个地址,系统就可以查询到该地址100米500米1000米范围内的事件信息,当然了还可以输入事件的关键信息做模糊查询,所以我选择了使用es来存储......
  • ES(Elasticsearch)入门-深入索引操作
    1.创建索引使用PUT请求。结构PUT/${index_name}//索引名称{"settings":{...索引相关的配置项目,如何:分配个数副分片个数等},"mappings":{...数据的结构}}-----------------------------------实例---------------------------......
  • 自动化部署elasticsearch三节点集群
    什么是Elasticsearch?Elasticsearch是一个开源的分布式搜索和分析引擎,构建在ApacheLucene的基础上。它提供了一个分布式多租户的全文搜索引擎,具有实时分析功能。Elasticsearch最初是用于构建全文搜索引擎,但它的功能已经扩展到包括日志分析、应用程序性能监控、地理信息系统等......
  • 即构 UIKits 重磅发布!高效开发与自定义UI兼备,打造互动场景新标杆
    即构UIKits上线,新一代场景化实时互动SDK!即构科技发布了首款面向中小团队的整合型实时互动产品UIKits,基于场景化最佳实践,整合RTC、IM、直播、美颜等多款产品,打造了音视频通话UIKit(CallKit)、互动直播UIKit(LiveStreamingKit)、语聊房UIKit(LiveAudioRoomKit)等多个场景互动SDK......