官网:https://www.elastic.co/cn/
(1)解压:/opt/software/是解压到那个目录下
tar -zxvf elasticsearch-7.4.0-linux-x86_64.tar.gz -C /opt/software/
(2)修改elasticsearch.yml文件:
cluster.name: my-application
node.name: node-1
network.host: 0.0.0.0
http.port: 9200
cluster.initial_master_nodes: ["node-1"]
(3)启动集群,直接运行命令(会提示不能以root用户启动):
./bin/elasticsearch
(4)创建用户,为用户授权
useradd zhangsan
passwd zhangsanchown -R zhangsan:zhangsan elasticsearch-7.4.0
(5)修改配置文件
切换到root用户:
su root// 最大可创建文件数太小
vim /etc/security/limits.conf
// 文件末尾增加:
zhangsan soft nofile 65535
zhangsan hard nofile 65535//
vim /etc/security/limits.d/20-nproc.conf
zhangsan soft nofile 65536
zhangsan hard nofile 65536
* hard nproc 4096// 虚拟内存太小vim etc/sysctl.confvm.max_map_count = 655360
// 重新加载
sysctl -p
(6)切换为zhangsan用户,启动elasticsearch
su zhangsan
./bin/elasticsearch
(7)启动之后,浏览器访问http://xx.xx.xx.xx:9200
(使用阿里云服务器,在安全组里面加入9200端口,用公网ip访问)
关闭防火墙:
systemctl stop firewalld
官网下载linux与elasticsearch对应的版本:
https://www.elastic.co/cn/downloads/past-releases/kibana-7-4-0
(1)解压:
tar -zxvf kibana-7.4.0-linux-x86_64.tar.gz -C /opt/software/
(2)修改配置:
vim config/kibana.ymlserver.port: 5601
server.host: 0.0.0.0
server.name: kibana-zhangsan
elasticsearch.hosts: ["http://127.0.0.1:9200"]
elasticsearch.requestTimeout: 99999
(3)启动kibana:
cd bin/kibana
./kibana --allow-root
添加索引:
PUT http://ip:端口/索引名称
查询索引:
GET http://ip:端口/索引名称
删除索引:
DELETE http://ip:端口/索引名称
关闭索引:
POST http://ip:端口/索引名称/_close
打开索引:
POST http://ip:端口/索引名称/_open
(1)简单数据类型
(2)复杂数据类型
映射创建出来之后,es不允许直接修改或者删除字段名字和类型的,但是可以添加字段。
(1)添加映射
(2)查询映射
(3)添加字段
# 创建索引:
PUT person# 查询索引
GET person#添加映射 (!!创建索引的时候不增加映射,直接添加文档,会自动把映射加上去)
PUT person/_mapping
{"properties" : {"name": { "type": keyword},"age": {"type": integer}}
}
# 只查询映射
GET person/_mapping# 创建索引并添加映射
PUT person
{"mappings": {"properties" : {"name": { "type": keyword},"age": {"type": integer}}}
}
# 创建之后又要添加一个字段
PUT person/_mapping
{"properties" : {"address": { "type": text}}
}
(1)添加文档
(2)查询文档
(3)修改文档
(4)删除文档
#添加文档,指定id (POST或者PUT都可以)
PUT person/_doc/1
{"name: "zhangsan","age": 18,"address": "北京"
}
# 根据id查询文档
GET person/_doc/1================================================
#添加文档,不指定id(⚠️需要使用POST)
POST person/_doc
{"name: "lisi","age": 18,"address": "北京"
}
GET person/_doc/随机生成的id=============================================
# 修改文档(id存在自动就修改,id不存在就添加)
PUT person/_doc/1
{"name: "lisi","age": 18,"address": "北京"
}=============================================
# 查询所有文档
GET person/_search=============================================
# 根据id删除文档
DELETE person/_doc/id
(1)elasticsearch内置的分词器对中文很不友好,一个字一个词
# 可以用这个看一下,查询的文本会被分成什么词
GET _analyze
{"analyze": "选择分词器名称","text": "需要分词的文本"
}
(2)分词器插件:IK分词
# term查询(会发现查不到数据,因为默认分词器是standard)
GET person/_search
{"query" : {"term": {// 会查询到address分词之后 北京 这个分词的数据"address": {"value": "北京"}}}
}# 创建索引,添加映射,指定使用ik分词器
PUT person
{"mappings: {"properties" : {"name: {"type": "keyword"},"address: {"type": "text",// 指定ik分词器"analyer": "ik_max_word"}}}
}
GET person/_search
{"query" : {"match": {// 先对北京昌平分词,如果分成北京和昌平两个词,会获取到 北京 和 昌平 这个两个词 // 条。对应的数据的并集"address": "北京昌平"}}
}
(1)搭建SpringBoot工程
(2)引入ElasticSearch相关坐标
(3)测试
public void addIndex() {// 1、cient.indices()获取操作索引的对象IndicesClient indices = client.indices();// ...indices.create(createRequest, RequestOptions.DEFAULT);// ...
}
public void addIndex() {// 1、cient.indices()获取操作索引的对象IndicesClient indices = client.indices();// ...indices.get(getRequest, RequestOptions.DEFAULT);// ...
}
public void deleteIndex() {// 1、cient.indices()获取操作索引的对象IndicesClient indices = client.indices();// ...indices.delete(deleteRequest, RequestOptions.DEFAULT);// ...
}
public void existsIndex() {// 1、cient.indices()获取操作索引的对象IndicesClient indices = client.indices();// ...indices.exists(deleteRequest, RequestOptions.DEFAULT);// ...
}
// ...
client.index(request, RequestOptions.DEFAULT);
// ...
添加文档时,id存在则修改,id不存在则添加
// ...
client.get(getRequest, RequestOptions.DEFAULT);
// ...
// ...
client.delete(deleteRequest, RequestOptions.DEFAULT);
// ...
bulk批量操作是将文档的增删改查一系列操作,通过一次请求全部做完,减少网络请求。
# 批量操作——脚本操作
# 1、删除5号记录
# 2、增加8号记录
# 3、修改8号记录,名称为“lisi”POST _bulk
{ "delete": {"_index": "person", "_id": "5" } }
{ "create": {"_index": "person", "_id": "8" } }
{ "name": "张三", "age": 18, "address": "北京" }
{ "update": {"_index": "person", "_id": "8" } }
{ "doc": {"name": "lisi" }}
bulkRequest.add(deleteRequest);
bulkRequest.add(indexRequest);
...
client.bulk(bulkRequest, RequestOptions.DEFAULT);
需求:将数据库的goods表导入到elasticsearch里面
实现步骤:
(1)创建goods索引
(2)查询goods表数据
(3)批量添加到elasticsearch中(添加很多indexRequest)
⚠️批量插入的时候,类型不匹配插入失败的错误非常隐蔽,需要打断点看
虽然写GET person/_search 也可以,但是标准写法是matchAll
# matchAll 查询所有文档
# 默认情况下,es一次展示10条数据
GET person/_search
{"query": {"match_all": {}}
}# form size 控制分页
GET person/_search
{"query": {"match_all": {}},"from": 0,"size": 100
}
== TODO:p29再听一下具体返回值的意思 查询结果详解 max_score:得分 ==
matchAll——JavaAPI
term查询:不会对查询条件分词
GET person/_search
{"query" : {"term": {// 会查询到address分词之后 北京 这个分词的数据"address": {"value": "北京"}}}
}
# term——JavaAPI
会对查询条件进行分词,然后查询,默认取并集
GET person/_search
{"query": {"match": {"brandName": "华为"}}
}GET person/_search
{"query": {"match": {"字段名称" : {"value": "查询条件","operator": "操作(or 或 and)"}}}
}
match查询——JavaAPI
GET person/_search
{"query": {"wildcard": {"title": {// 华 字前面不要加通配符,否则会全表扫描"value": "华?"}}}
}
GET person/_search
{"query": {"regexp": {"title": "\\w+.*"}}
}
GET person/_search
{"query": {"prefix": {"title": {"value": "华为"}}}
}
模糊查询——JavaAPI
range范围查询:查找指定字段在指定范围内包含值。
对查询条件进行排序(任何字段查询都可以排序)
GET goods/_search
{"query": {"range": {"price": {// gte 大于等于"gte": 2000,// lte 小于等于"lte": 3000}}},// 对查询条件进行排序"sort": [{"price": {"order": "desc"}}]
}
GET goods/_search
{"query": {"query_string": {"fields": ["title", "brandName"],"query": "华为 OR 手机"}}
}GET goods/_search
{"query": {"simple_query_string": {"fields": ["title", "brandName"],// 不支持OR AND"query": "华为手机"}}
}
boolQuery:对多个查询条件连接。连接方式:
GET goods/_search
{"query": {"bool": {// brandName必须是华为,并且title里面要包含手机"must":[{"term": {"brandName": {"value": "华为"}}},{"match": {"title": "手机"}}],"filter": [{}],"must_not": [{}],"should": [{}]}}
}
GET good/_search
{"query": {"match": {"title": "华为"}},"aggs": {"max_price": {"max": {"field": "price"}}}
}
GET good/_search
{"query": {"match": {"title": "华为"}},"aggs": {"group_brands": {"terms": {"field": "brandName","size": 100}}}
}
GET goods/_search
{"query": {"match": {"title": "电视",}},"highlight": {"fields": {"title": {"pre_tags": "","post_tags": ""}}}
}
随着业务需求的变更,索引的结构可能发生变化。
ElasticSearch的索引一旦创建,只允许添加字段,不允许改变字段。因为改变字段需要重建倒排索引,影响内部缓存结构,性能太低。