Elasticsearch 是一个实时的分布式搜索分析引擎
启动本地:
运行:elasticsearch.bat
路径:D:\work_software\elasticsearch\bin
D:\work_software\elasticsearch-slave\slave1\bin
D:\work_software\elasticsearch-slave\slave2\bin
插件:npm run start (命令行进入,时间可能较久)
路径:D:\work_software\elasticsearch\elasticsearch-head-master
1.准备环境:
1.下载 https://www.elastic.co/downloads/elasticsearch
2.运行 bin/elasticsearch
3.访问 http://localhost:9200/
4.安装插件elasticsearch-head-master:
参考文章:https://blog.youkuaiyun.com/u012270682/article/details/72934270
安装 node.js
1.下载 https://nodejs.org/en/
2.安装
3.查看node.js版本 node -v
4.查看npm 版本 npm -v
5.安装grunt 运行: npm install -g grunt-cli
6.确保 nodejs 环境变量ok ,参考文章 https://www.cnblogs.com/gaosheng-221/p/6799744.html
安装插件: elasticsearch-head-master
位置:D:\work_software\elasticsearch
1.下载 https://github.com/mobz/elasticsearch-head
2.运行 D:\work_software\elasticsearch\elasticsearch-head-master>npm install
3.启动 D:\work_software\elasticsearch\elasticsearch-head-master>npm run start
2.集群搭建:
复制多份 elasticsearch,选一份做master ,其余为slave
注意端口不能重复
master修改配置文件 config/elasticsearch.yml(冒号后必须有空格,yml文件)
http.cors.enabled: true
http.cors.allow-origin: "*"
cluster.name: Leroy
node.name: master
node.master: true
network.host: 127.0.0.1
http.port: 9200
slave1修改配置文件 config/elasticsearch.yml(冒号后必须有空格,yml文件)
http.cors.enabled: true
http.cors.allow-origin: "*"
#名字要与 master 的一致
cluster.name: Leroy
node.name: salve1
network.host: 127.0.0.1
http.port: 9201
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
slave2修改配置文件 config/elasticsearch.yml(冒号后必须有空格,yml文件)
http.cors.enabled: true
http.cors.allow-origin: "*"
#名字要与 master 的一致
cluster.name: Leroy
node.name: salve1
network.host: 127.0.0.1
http.port: 9202
discovery.zen.ping.unicast.hosts: ["127.0.0.1"]
3.基础概念
集群:多个节点组成,每个节点的集群名字必须一致
索引(datebase):含有相同索引的文档集合,英文字母小写,不含-,
类型(table):索引可以定义一个或多个类型,文档必须属于一个类型
文档(row):可以被索引的基本数据单位
分片:每一个索引都有多个分片,每个分片是一个lucene索引(默认5个分片)
备份:拷贝一个分片就完成一个分片的备份(默认一个备份)
4.restFul api 风格
1.创建索引:put----127.0.0.1:9200/people
{
"settings":{
"number_of_shards":1,
"number_of_replicas":3
},
"mappings":{
"man":{
"properties":{
"name":{
"type":"text"
},
"country":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"birth":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis "
}
}
}
}
}
2.插入数据,指定id(路径最后的就是id)
post----127.0.0.1:9200/people/man/1
{
"name":"leroy",
"country":"China",
"age":26,
"birth":"1992-01-28"
}
3.插入数据,自动生成id(路径不传id即可)
post----127.0.0.1:9200/people/man
{
"name":"leroy2222",
"country":"China",
"age":20,
"birth":"1992-01-24"
}
4.修改数据
post---127.0.0.1:9200/people/man/1/_update
{
"doc":{
"name":"leroy6666"
}
}
5.脚本修改
post---127.0.0.1:9200/people/man/1/_update
{
"script":{
"lang":"painless",
"inline":"ctx._source.age+=params.age;ctx._source.name=params.name",
"params":{
"name":"张三",
"age":18
}
}
}
6.删除数据
delete----127.0.0.1:9200/people/man/1
7.删除索引
delete----127.0.0.1:9200/people
8.简单查询, 条件查询,聚合查询
简单查询 GET---127.0.0.1:9200/book/novel/1--->id值
条件查询 POST---127.0.0.1:9200/book/novel/_search
参数:body---row---json(application/json)
{
"query":{
"match_all":{}
},
"from":1,
"size":20
}
参数:关键词查询
{
"query":{
"match":{
"title":"ElasticSearch"
}
},
"sort":[
{"publish_date":{"order":"desc"}}
]
}