1.1 什么是ElasticSearch
Elasticsearch是一个实时的分布式搜索和分析引擎。它可以帮助你用前所未有的速度去处理大规模数据。ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
1.2 ElasticSearch特点
(1)可以作为一个大型分布式集群(数百台服务器)技术,处理PB级数据,服务大公
司;也可以运行在单机上
(2)将全文检索、数据分析以及分布式技术,合并在了一起,才形成了独一无二的ES;
(3)开箱即用的,部署简单
(4)全文检索,同义词处理,相关度排名,复杂数据分析,海量数据的近实时处理
1.3 ElasticSearch体系结构
2 走进ElasticSearch
2.1 ElasticSearch部署与启动
下载ElasticSearch 5.6.8版本
https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-8
无需安装,解压安装包后即可使用
在命令提示符下,进入ElasticSearch安装目录下的bin目录,执行命令
elasticsearch
即可启动。
我们打开浏览器,在地址栏输入http://127.0.0.1:9200/ 即可看到输出结果
{
"name" : "uV2glMR",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "RdV7UTQZT1‐Jnka9dDPsFg",
"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"
}
2.2 Postman调用RestAPI
2.2.1 新建索引
例如我们要创建一个叫articleindex的索引 ,就以put方式提交
http://127.0.0.1:9200/articleindex/
2.2.2 新建文档
新建文档:
以post方式提交 http://127.0.0.1:9200/articleindex/article
body:
{
"title":"SpringBoot2.0",
"content":"发布啦"
}
返回结果如下:
{
"_index": "articleindex",
"_type": "article",
"_id": "AWPKsdh0FdLZnId5S_F9",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
_id是由系统自动生成的。
2.2.3 查询全部文档
查询某索引某类型的全部数据,以get方式请求
http://127.0.0.1:9200/articleindex/article/_search 返回结果如下:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "articleindex",
"_type": "article",
"_id": "AWPKrI4pFdLZnId5S_F7",
"_score": 1,
"_source": {
"title": "SpringBoot2.0",
"content": "发布啦"
}
},
{
"_index": "articleindex",
"_type": "article",
"_id": "AWPKsdh0FdLZnId5S_F9",
"_score": 1,
"_source": {
"title": "elasticsearch入门",
"content": "零基础入门"
}
}
]
}
}
2.2.4 修改文档
以put形式提交以下地址:
http://192.168.223.128:9200/articleindex/article/AWPKrI4pFdLZnId5S_F7
body:
{
"title":"SpringBoot2.0正式版",
"content":"发布了吗"
}
返回结果:
{
"_index": "articleindex",
"_type": "article",
"_id": "AWPKsdh0FdLZnId5S_F9",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
如果我们在地址中的ID不存在,则会创建新文档
以put形式提交以下地址:
http://192.168.223.128:9200/articleindex/article/1
body:
{
"title":"十次方课程好给力",
"content":"知识点很多"
}
返回信息:
{
"_index": "articleindex",
"_type": "article",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
再次查询,看是否有新增的这条文档
2.2.5 按ID查询文档
GET方式请求
http://192.168.223.128:9200/articleindex/article/1
2.2.6 基本匹配查询
根据某列进行查询 get方式提交下列地址:
http://192.168.223.128:9200/articleindex/article/_search?q=title:课程
好给力
以上为按标题查询,返回结果如下:
{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 2.0649285,
"hits": [
{
"_index": "articleindex",
"_type": "article",
"_id": "1",
"_score": 2.0649285,
"_source": {
"title": "课程好给力",
"content": "知识点很多"
}
}
]
}
}
2.2.7 模糊查询
我们可以用*代表任意字符:
http://192.168.223.128:9200/articleindex/article/_search?q=title:*s*
2.2.8 删除文档
根据ID删除文档,删除ID为1的文档 DELETE方式提交
http://192.168.223.128:9200/articleindex/article/1
返回结果如下:
{
"found": true,
"_index": "articleindex",
"_type": "article",
"_id": "1",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
再次查看全部是否还存在此记录