SpringBoot2>06 - ElasticSearch(一)

扯淡:

提到搜索,我们肯定听说过Lucene、solr。眼下比Solr更火热的就是ElasticSearch(下文全部简称ES)。ES是一个用java开发基于Lucene的分布式搜索服务器,基于Restful Web 接口,是当前流行的企业级搜索引擎。主要用于大数据、分布式系统中。

springboot、springcloud、docker学习目录:【传送门】 

ES 结构:

类比MySQL数据库:

ES MySQL
index(索引) databases(数据库)
type(类型) table(表)
document(文档) row(表记录)

ES安装:

1、下载:https://www.elastic.co/cn/downloads/past-releases 

选择对应版本下载。解压即可,无需安装。

2、启动:

扫描二维码关注公众号,回复: 5041661 查看本文章

2.1、bin目录 执行 elasticsearch 命令。

2.2、浏览器:http://127.0.0.1:9200/

显示如下:

{
  "name" : "70KI8pA",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "2UHtqlESQn2aW_U69NGStQ",
  "version" : {
    "number" : "5.6.8",
    "build_hash" : "688ecce",
    "build_date" : "2018-02-16T16:46:30.010Z",
    "build_snapshot" : false,
    "lucene_version" : "6.6.1"
  },
  "tagline" : "You Know, for Search"
}

ES Restful API:

基本rest api 操作:

1、新建索引(index)
put请求 :http://127.0.0.1:9200/index_name/
示例:http://127.0.0.1:9200/userindex/

2、新建文档(document)
post请求 : http://127.0.0.1:9200/index_name/doc_name
示例:http://127.0.0.1:9200/userindex/user
body:
{
    "name":"ron",
    "age":"25",
    "description":"how`s it going"
}

3、查询文档
get请求 : 
# 查询所有
http://127.0.0.1:9200/userindex/user/_search
# 根据id查询
http://127.0.0.1:9200/userindex/user/1
# 根据其他列查询
http://127.0.0.1:9200/userindex/user/_search?q=name:ron
# 模糊查询(*:代表任意字符)
http://127.0.0.1:9200/userindex/user/_search?q=name:*r*

4、修改文档
put请求 : http://127.0.0.1:9200/userindex/user/_id
示例:http://127.0.0.1:9200/userindex/user/AOPFrI4pFdDAbId5S_D7
body:
{
    "name":"ron",
    "age":"18",
    "description":"not bad"
}
# 注意:若指定的id不存在则会创建新的文档

5、删除文档
delete请求 : http://127.0.0.1:9200/userindex/user/1

可借助postman操作上述api,但还是相对较复杂。

Head 插件:

使用rest api 直接操作ES显得较复杂,Head为操作ES的一种图像化界面插件,需要node.js 环境。

1、下载:https://github.com/mobz/elasticsearch-head 

复制到ES安装目录的之外的任意目录。

2、安装(head为前端项目):

2.1、安装 node js、cnpm

2.2、切换到淘宝源

npm install ‐g cnpm ‐‐registry=https://registry.npm.taobao.org

2.2、grunt安装为全局命令

npm install ‐g grunt‐cli

2.3、安装依赖(package.json中)

cnpm install

3、启动:

进入elasticsearch-head-master目录

执行:grunt server

浏览器:http://127.0.0.1:9100/

4、操作错误:

ES默认不允许跨域,修改ES config下elasticsearch.yml。

# 末尾新增  
http.cors.enabled: true
http.cors.allow-origin: "*"

5、操作:

上述rest api 的操作均可在此界面中操作。

IK分词器:

ES是用来做搜索的,它会根据关键词来匹配需要搜索的内容。对于中文,默认一个字就是一个词,例如:“魔镜魔镜,谁是这个世界最帅的人”这句话会被分成14个词。IK分词器是一款国人开发的相对简单的中文分词器。

1、下载:https://github.com/medcl/elasticsearch-analysis-ik/releases

2、解压后重命名为ik,复制到ES安装目录的plugins中。

3、重启ES。

4、分词算法ik_smart、ik_max_word

4.1、最少切分 ik_smart

例如:

#  请求:http://127.0.0.1:9200/_analyze?analyzer=ik_smart&pretty=true&text=程序员
{
  "tokens" : [
    {
      "token" : "程序员",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    }
  ]
}

4.2、最细粒度 ik_max_word

#  请求:http://127.0.0.1:9200/_analyze?analyzer=ik_max_word&pretty=true&text=程序员
{
  "tokens" : [
    {
      "token" : "程序员",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "程序",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "员",
      "start_offset" : 2,
      "end_offset" : 3,
      "type" : "CN_CHAR",
      "position" : 2
    }
  ]
}

5、自定义词库:

“帅的一比”,本身不是一个词,可通过自定义词库使其是一个词。

5.1、进入到ES安装目录下/plugins/ik/config目录。新建自定义的字典。

可以看到已经存在默认的字典。

custom.dic 为自己新建的字典,名称随便取,可在其中添加自定义的词。

内容:

帅的一比

5.2、修改配置IKAnalyzer.cfg.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>

5.3、重启ES、重新分词。

# 请求:http://127.0.0.1:9200/_analyze?analyzer=ik_smart&pretty=那个弹钢琴的,帅的一比
{
  "tokens" : [
    {
      "token" : "那个",
      "start_offset" : 0,
      "end_offset" : 2,
      "type" : "CN_WORD",
      "position" : 0
    },
    {
      "token" : "弹钢琴",
      "start_offset" : 2,
      "end_offset" : 5,
      "type" : "CN_WORD",
      "position" : 1
    },
    {
      "token" : "的",
      "start_offset" : 5,
      "end_offset" : 6,
      "type" : "CN_CHAR",
      "position" : 2
    },
    {
      "token" : "帅的一比",
      "start_offset" : 7,
      "end_offset" : 11,
      "type" : "CN_WORD",
      "position" : 3
    }
  ]
}

至此,ES的基本使用已经完成。

总结:

1、ES的下载安装。
2、ES Rest API操作ES 增、删、改、查。
3、Head 插件,图形化界面。
4、IK中文分词器,ik_smart最少切分、ik_max_word最细粒度,两种分词算法。
5、IK自定义词库。

个人学习分享
更多 springboot、springcloud、docker 文章,关注微信公众号吧:

猜你喜欢

转载自blog.csdn.net/cp026la/article/details/86551091