1、ElasticSearch简介
2、单实例运行ES
不能使用root 用户执行命令
cd ./elasticsearch
sh ./bin/elasticsearch # 当前命令ctrl+c或关闭终端窗口的时候程序就会stop掉
#后台运行
sh ./bin/elasticsearch -d
jps #查看运行进程
3、集群配置
修改各节点配置文件: config/elasticsearch.yml,增加以下配置
# 主节点
cluster.name: master # 集群名称
node.name: master # 节点名称
node.master: true # 是否是主节点
# 从节点1
cluster.name: master # 集群名称
node.name: slave1 # 节点名称
network.host: 0.0.0.0
http.port: 9201 # 端口号
discovery.zen.ping.unicast.hosts: ["127.0.0.1"] # 主节点地址
#从节点2
cluster.name: master # 集群名称
node.name: slave2 # 节点名称
network.host: 0.0.0.0
http.port: 9202 端口号
discovery.zen.ping.unicast.hosts: ["127.0.0.1"] # 主节点地址
4、安装可视化图形插件
ElasticSearch不同于Solr自带图形化界面,想管理elasticsearch集群中所有的主机,看看它们的运行状态,集群数量,还得一台一台机器远程连接进去。这个时候有一个图形管理界面,可以很直观的管理我们所有数据和集群,那就太方便了!
4.1 手动安装head插件
- 安装NodeJs
- GitHub下载elasticsearch-head
- npm安装及启动
cd ./elasticsearch-head
npm install
#前台运行
npm run start
#后台运行
nohup npm run start &
浏览器访问head插件是否成功
http://127.0.0.1:9100/
4.2 Chrome拓展程序 安装ElasticSearch Head
Chrome网上应用店在线安装或手动安装扩展
5、基础概念
- 集群和节点
- 文档: 文档是可以被索引的基本数据单位
- 索引: 含有相同属性的文档集合
- 类型: 索引可以定一个或多个类型,文档必须属于一个类型
- 分片:每个索引都有很多分片,每个分片是一个Lucene索引,提高效率
- 备份:拷贝一份分片就完成了分片的备份
6、存储结构
RESTFul API
API 基本格式 http://ip:端口/<索引>/<类型>/<文档id>
常用HTTP动词: GET、PUT、POST、DELETE
7、创建索引
http://127.0.0.1:9200 发送PUT请求 创建索引
- 索引的配置
settings :
number_of_shards 5 : 代表分片是五个
number_of_replicas 1 : 备份是一个
- 索引的映射定义
mappings:
"grass", 索引的一种类型
"properties" 属性
"age" 名称
"type": 数据类型
示例:PUT http://127.0.0.1:9200/people?include_type_name=true
查看官网示例后发现 7.4 默认不在支持指定索引类型,默认索引类型是_doc(隐含:include_type_name=false),如果你要像之前旧版本一样兼容自定义 type ,需要将 include_type_name=true 携带
{
"settings": {
"number_of_shards": 5,
"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||yyy-MM-dd||epoch_millis"
}
}
}
}
}
8、插入数据
8.1 指定文档ID
# http://127.0.0.q:9200/索引/类型/id
PUT http://127.0.0.1:9200/people/man/1
{
"name":"小王",
"country":"中国",
"age":"20",
"date":"2022-05-17"
}
8.2 自动生成文档ID
# http://127.0.0.q:9200/索引/类型
POST http://127.0.0.1:9200/people/man
{
"name":"小张",
"country":"中国",
"age":"18",
"date":"2022-05-17"
}
9、修改文档数据
9.1 直接修改数据
POST http://127.0.0.1:9200/people/man/1/_update
{
"doc":{
"name":"王麻子"
}
}
9.2 脚本修改文档数据
POST http://127.0.0.1:9200/people/man/1/_update
{
"script":{
"lang":"painless",
# 方式1
"inline":"ctx._source.age+=10",
# 方式2
"inline":"ctx._source.age = params.age",
"params": {
"age": 100
}
# 方式2结束
}
}
10、删除ES数据
10.1 删除索引
DELETE http://127.0.0.1:9200/people
10.2 删除文档
DELETE http://127.0.0.1:9200/people/man/1
11、数据查询
11.1 简单查询
GET http://127.0.0.1:9200/people/man/1
11.2 条件查询
POST http://127.0.0.1:9200/people/_search
# 查询全部数据
{
"query":{
"match_all":{}
}
}
# 指定返回条数、从哪开始
{
"query":{
"match_all":{}
},
"from":1,
"size":1
}
# 关键字查询 (name含有“小”的)
{
"query":{
"match":{
"name":"小"
}
},
# 排序
"sort":[
{
"date":{"order":"desc"}
}
]
}
11.3 聚合查询
POST http://127.0.0.1:9200/people/_search
# 按年龄分组
{
"aggs": {
"group_by_age": { #分组名称(自定义)
"terms": {
"field": "age"
}
}
}
}
#计算字段(返回字段数量、最大值、最小值等)
{
"aggs": {
"grades_age":{
"stats":{
"field":"age"
}
}
}
}
11.4 高级查询
11.4.1 子条件查询(根据指定值查询指定字段)
- Query context
在查询过程中,除了判断文档是否满足查询条件外,ES 还会计算一个
_score
来表示匹配的程度,旨在判断目标文档和查询条件配的程度有多匹配
。
全文本查询
针对文本类型的数据
POST http://127.0.0.1:9200/people/_search
# 模糊查询的关键词 match
# 词语查询 match_phrase
{
"query":{
"match_phrase":{
"title":"张三的书"
}
}
}
# 多字段模糊查询 multi_match
{
"query": {
"multi_match": {
"query":"王",
"fields":["author","title"]
}
}
}
# 字符串查询 query_string
{
"query": {
"query_string": {
"query": "(王 AND 张) OR 18",
"fields": ["name","age"] # 查询的字段
}
}
}
字段级别查询
针对结构化数据,如数字、日期等
# 字段查询 结构化查询
{
"query": {
"term": {
"age": 18
}
}
}
# 范围查询 range
{
"query": {
"range": {
"age": {
"gte": 10,
"lte": 20
}
}
}
- Filter context
在查询过程中,只判断该文档是否满足条件, 只有YES 或者 NO
# 只查询20岁的
{
"query": {
"bool": {
"filter": {
"term": {"age":20}
}
}
}
}
11.4.2 复合条件查询(以一定的逻辑组合子条件查询)
## 固定(指定)分数 score 查询
# constant_score 不支持直接使用match 查询,支持 filter
# boost 指定分数
{
"query": {
"constant_score": {
"filter": {
"match": {
"name": "王麻子"
}
},
"boost":1
}
}
}
## 布尔查询
# should 多个条件,满足一个即显示
{
"query": {
"bool": {
"should": [
{
"match":{
"age":18
}
},
{
"match":{
"name":"小张"
}
}
]
}
}
}
# must 该查询条件有两个子条件,必须同时满足两个条件才会显示
{
"query": {
"bool": {
"must ": [
{
"match":{
"age":18
}
},
{
"match":{
"sex":"女"
}
}
]
}
}
}
## must 和 filter组合查询
{
"query": {
"bool": {
"must": [
{
"match":{
"age":18
}
},
{
"match":{
"sex":"女"
}
}
],
"filter": [
{
"term": {
"name": "王"
}
}
]
}
}
}
## must_not 一定不能满足的条件
{
"query": {
"bool": {
"must_not":{
"match":{
"age":80
}
}
}
}
}