1 Elasticsearch简介
1.1 什么是ElasticSearch
Elasticsearch是一个实时的分布式搜索和分析引擎。它可以帮助你用前所未有的速 度去处理大规模数据。ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分 布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发 的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用 于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便
1.2 ElasticSearch特点
(1)可以作为一个大型分布式集群(数百台服务器)技术,处理PB级数据,服务大公 司;也可以运行在单机上
(2)将全文检索、数据分析以及分布式技术,合并在了一起,才形成了独一无二的ES;
(3)开箱即用的,部署简单
(4)全文检索,同义词处理,相关度排名,复杂数据分析,海量数据的近实时处理
1.3 ElasticSearch体系结构
索引(index) -》数据库(databases)
类型(type) -》表(table)
文档(document) -》 行(row)
2 安装ES
去官网下载ElasticSearch 5.6.8版本( zip 格式)
https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-8
无需安装,解压安装包后即可使用,进入ElasticSearch安装目录下的bin目录,执行命令
elasticsearch
打开浏览器,在地址栏输入http://127.0.0.1:9200/ 即可看到输出结果(默认端口号9200);返回json格式数据
2.2 Postman调用RestAPI
2.2.1 新建索引
创建一个叫articleindex的索引 ,就以put方式提交
http://127.0.0.1:9200/tensquare_elasticsearch
2.2.2 新建文档
以post方式提交 http://127.0.0.1:9200/tensquare_elasticsearch/article
body:
{
"title":"spring教程",
"content":"spring框架教程"
}
result:
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AXQ92f-y6VlK-8rsjN9Q",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
_id是由系统自动生成的。
2.2.3 查询全部文档
查询某索引某类型的全部数据,以get方式请求(_search):http://127.0.0.1:9200/tensquare_elasticsearch/article/_search
result:
{
"took": 30,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AXQ92f-y6VlK-8rsjN9Q",
"_score": 1.0,
"_source": {
"title": "spring教程",
"content": "spring框架教程"
}
}
]
}
}
2.2.4 修改文档
以put形式(需要携带id,根据id来有就修改,没有就新增)提交以下地址:http://127.0.0.1:9200/tensquare_elasticsearch/article/AXQ92f-y6VlK-8rsjN9Q
body:
{
"title":"spring教程",
"content":"spring修改操作"
}
result:
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AXQ92f-y6VlK-8rsjN9Q",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
2.2.5 按ID查询文档
get带id访问:http://127.0.0.1:9200/tensquare_elasticsearch/article/1
result:
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"title": "spring教程",
"content": "spring修改操作"
}
}
2.2.6 基本匹配查询
根据某列进行查询 get方式提交下列地址:
http://127.0.0.1:9200/tensquare_elasticsearch/article/_search?q=title:spring
_search?q=title:spring(title=‘spring’)
2.2.7 模糊查询
加*
http://127.0.0.1:9200/tensquare_elasticsearch/article/_search?q=title:*s*
2.2.8 删除文档
根据ID删除文档,删除ID为1的文档 DELETE方式提交
http://127.0.0.1:9200/tensquare_elasticsearch/article/1
result:
{
"found": true,
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "1",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
3 ik分词器使用
浏览器输入:
http://localhost:9200/_analyze?analyzer=chinese&pretty=true&text=%E6%88%91%E6%98%AF%E7%A8%8B%E5%BA%8F%E5%91%98
默认的中文分词是将每个字看成一个词,这显然是不符合要求的,所以我们需要安装[中文分词器]来解决这个问题。
下载地址:
https://github.com/medcl/elasticsearch-analysis-ik/releases
下载5.6.8版本
(1)先将其解压,将解压后的elasticsearch文件夹重命名文件夹为ik
(2)将ik文件夹拷贝到elasticsearch/plugins 目录下。
(3)重新启动,即可加载IK分词器
win重启进入es/bin/cmd:elasticsearch
4.3 IK分词器测试
IK提供了两个分词算法ik_smart 和 ik_max_word
其中 ik_smart 为最少切分,ik_max_word为最细粒度划分
(1)ik_smart最小切分:在浏览器地址栏输入地址
http://localhost:9200/_analyze?analyzer=ik_smart&pretty=true&text=%E6%88%91%E6%98%AF%E7%A8%8B%E5%BA%8F%E5%91%98
(2)ik_max_word最细切分:在浏览器地址栏输入地址
http://localhost:9200/_analyze?analyzer=ik_max_word&pretty=true&text=%E6%88%91%E6%98%AF%E7%A8%8B%E5%BA%8F%E5%91%98
返回结果:
{
"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
}
]
}
4.4 自定义词库
步骤:
(1)进入elasticsearch/plugins/ik/config目录
(2)新建一个my.dic文件,编辑内容:
我是中国人
修改IKAnalyzer.cfg.xml(在ik/config目录下)
重新启动elasticsearch,通过浏览器测试自定义分词效果
http://localhost:9200/_analyze?analyzer=ik_smart&pretty=true&text=%E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4%BA%BA