什么是ElasticSearch
- 基于Apache Lucene构建的开源搜索引擎
- 采用Java编写,提供简单易用的RESTFul API
- 轻松的横向扩展,可支持BP级的结构化和非结构化的数据处理
可应用场景
- 海量数据分析引擎
- 站内搜索引擎
- 数据仓库
一线公司实际应用场景
- 英国卫报 - 实时分析公众对文章的回应
- 维基百科、GitHub-站内实时搜索
- 百度 - 实时日志监控平台
安装
Windows系统下Elasticsearch安装与Elasticsearch-head插件安装
分布式安装
- 修改D:\Program Files\elasticsearch-5.5.2\config\elasticsearch.yml文件
在文件末尾加入:
cluster.name: fly #集群名
node.name: master #节点名
node.master: true #是否为主节点
network.host: 127.0.0.1 #ip地址
- 将elasticsearch-5.5.2.zip解压为elasticsearch-1,修改elasticsearch-1\config\elasticsearch.yml文件,在文件末尾加入:
http.cors.enabled: true #跨域
http.cors.allow-origin: "*" #跨域
cluster.name: fly #集群名
node.name: slave1 #随从节点名
network.host: 127.0.0.1 #IP地址
http.port: 8200 #端口号
discovery.zen.ping.unicast.hosts: ["127.0.0.1"] #设置寻找主节点
- 将elasticsearch-5.5.2.zip解压为elasticsearch-2,修改elasticsearch-2\config\elasticsearch.yml文件,在文件末尾加入:
http.cors.enabled: true #跨域
http.cors.allow-origin: "*" #跨域
cluster.name: fly #集群名
node.name: slave2 #随从节点名
network.host: 127.0.0.1 #IP地址
http.port: 7200 #端口号
discovery.zen.ping.unicast.hosts: ["127.0.0.1"] #设置寻找主节点
访问localhost:9100如下图:
基础概念
- 集群和节点:一个集群是由多个节点组成的集合
- 索引:含有相同属性的文档集合
- 类型:索引可以包含一个或多个类型,文档必须属于一个类型
- 文档:文档是可以被索引的基本数据单元
- 分片:每个索引都有多个分片,每个分片都是Lucene索引
- 备份:拷贝一个分片就完成了分片的索引
索引创建
- API基本格式:http://
: /<索引>/<类型>/<文档id>
创建索引:访问localhost:9100,点击索引->创建索引,输入索引名book(小写),这样创建的索引为非结构化索引。
使用postman创建索引: - 选择方式为PUT,输入地址:127.0.0.1:9200/people
- 点击Body->row,文件格式选择json,输入内容:
{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"man":{
"properties":{
"name":{
"type":"text"
},
"country":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
如下图:
点击send,返回内容:
{
"acknowledged": true,
"shards_acknowledged": true
}
访问127.0.0.1:9100,将会看到以添加people节点
文档插入
文档插入又分为:
- 指定文档id插入:选择PUT,地址输入:127.0.0.1:9200/people/man/1,内容输入:
{
"name":"石头",
"age":"28",
"countory":"Chian",
"date":"1990-11-21"
}
点击send,返回内容:
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"created": true
}
- 自动产生文档id插入:选择POST,地址输入:127.0.0.1:9200/people/man/,内容输入:
{
"name":"疯狂的石头",
"age":"29",
"countory":"Chian",
"date":"1990-11-22"
}
点击send,返回内容
{
"_index": "people",
"_type": "man",
"_id": "AWEc_pyp21iCO45paMTd",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"created": true
}
刷新localhost:9100,看到people索引的docs的数量为2
修改文档
- 直接修改文档: 选择POST,地址输入127.0.0.1:9200/people/man/1/_update,内容输入:
{
"doc":{
"age":30
}
}
访问localhost:9100,点击数据浏览,刷新,将会看到age已经变为30了
- 通过脚本修改文档:选择POST,地址输入127.0.0.1:9200/people/man/1/_update,内容输入:
{
"script":{
"lang":"painless",
"inline":"ctx._source.age += 10"
}
}
访问localhost:9100,点击数据浏览,刷新,将会看到age已经变为40了
文档查询
- 简单查询:
GET localhost:9200/people/man/1
返回结果
{
"_index": "people",
"_type": "man",
"_id": "1",
"_version": 3,
"found": true,
"_source": {
"name": "石头",
"age": 40,
"countory": "Chian",
"date": "1990-11-21"
}
}
- 条件查询:
POST 127.0.0.1:9200/people/man/_search
body输入内容:
{
"query":{
"match_all":{}
}
}
返回结果
{
"took": 7, #耗时
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "AWEc_pyp21iCO45paMTd",
"_score": 1,
"_source": {
"name": "疯狂的石头",
"age": "29",
"countory": "Chian",
"date": "1990-11-22"
}
},
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": 1,
"_source": {
"name": "石头",
"age": 40,
"countory": "Chian",
"date": "1990-11-21"
}
}
]
}
}
指定文档起始位置与大小查询
POST 127.0.0.1:9200/people/man/_search
body输入内容:
{
"query":{
"match_all":{}
},
"from": 1,
"size": 1
}
从第一个文档查询,查询大小为1,返回结果:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": 1,
"_source": {
"name": "石头",
"age": 40,
"countory": "Chian",
"date": "1990-11-21"
}
}
]
}
}
按条件查询
POST 127.0.0.1:9200/people/man/_search
body输入内容:
{
"query":{
"match":{
"name":"石头"
}
},
"sort":{
"age":{
"order":"desc"
}
}
}
sort:排序
返回内容:
{
"took": 148,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 2,
"max_score": null,
"hits": [
{
"_index": "people",
"_type": "man",
"_id": "1",
"_score": null,
"_source": {
"name": "石头",
"age": 40,
"countory": "Chian",
"date": "1990-11-21"
},
"sort": [
40
]
},
{
"_index": "people",
"_type": "man",
"_id": "AWEc_pyp21iCO45paMTd",
"_score": null,
"_source": {
"name": "疯狂的石头",
"age": "29",
"countory": "Chian",
"date": "1990-11-22"
},
"sort": [
29
]
}
]
}
}
- 聚合查询