撸一门技术,必先登其官网,扒其皮,喝其血html
官网地址:www.elastic.co/products/el…java
官方中文文档地址:www.elastic.co/guide/cn/el…node
ElasticSearch
是一个分布式搜索服务,提供的是一组Restful API
,底层基于Lucene
,采用多shard
(分片)的方式保证数据安全,而且提供自动resharding的功能。是目前全文搜索引擎的首选,能够快速的存储、搜索和分析海量数据,Springboot
经过整合Spring Data ElasticSearch
为咱们提供了很是方便的检索功能支持。linux
tar -zxvf elasticsearch-6.6.0.tar.gz -C /opt/module/ # 解压安装包
复制代码
[root@localhost elasticsearch-6.6.0]# mkdir data # 建立数据文件夹(6.0自带logs文件夹)
复制代码
vi elasticsearch.yml # 修改配置文件
复制代码
cluster.name: my-application # 集群名称(多集群时候只需节点名称一直便可)
node.name: node-102 # 节点名称
path.data: /opt/module/elasticsearch-6.6.0/data # 数据路径
path.logs: /opt/module/elasticsearch-6.6.0/logs # 日志路径
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 192.168.1.8 # 网络地址
http.port: 9200 # 端口
discovery.zen.ping.unicast.hosts: ["hadoop102"] # 主机名
复制代码
注意:node.name能够随便取,可是一个集群中不能重复,注意path.data前不能有空格,冒号后必须有一个空格git
问题一:ERROR: bootstrap checks failed
github
su root
vi /etc/security/limits.conf
复制代码
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
复制代码
问题二:max number of threads [1024] for user [lish] likely too low, increase to at least [2048]
spring
vi /etc/security/limits.d/90-nproc.conf
* soft nproc 2048
复制代码
问题三:max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
sql
vi /etc/sysctl.conf
vm.max_map_count=655360
sysctl -p
复制代码
elasticsearch禁止使用root用户启动,须要新建一个testuser用户docker
[testuser@hadoop102 elasticsearch-6.6.0]$ ./bin/elasticsearch
复制代码
**访问地址:**http://192.168.1.8:9200/数据库
tar -zxvf node-v10.15.1-linux-x64.tar.gz -C /opt/module/
复制代码
vi /etc/profile
export NODE_HOME=/opt/module/node-v10.15.1-linux-x64
export PATH=$PATH:$NODE_HOME/bin
source /etc/profile
复制代码
[root@hadoop102 sortware]# unzip elasticsearch-head-master.zip -d /opt/module/
复制代码
[root@hadoop102 elasticsearch-head-master]# npm install grunt --save
复制代码
npm install -g cnpm --registry=https://registry.npm.taobao.org
复制代码
npm install -g grunt-cli
复制代码
vim Gruntfile.js
复制代码
options: {
hostname:'0.0.0.0',
port: 9100,
base: '.',
keepalive: true
}
复制代码
# 检查head根目录下是否存在base文件夹 没有:将 _site下的base文件夹及其内容复制到head根目录下
mkdir base
cp base/* ../base/
复制代码
[root@hadoop102 module]# chown -R luokangyuan:luokangyuan elasticsearch-head-master/
复制代码
[luokangyuan@hadoop102 elasticsearch-head-master]$ grunt server -d
复制代码
npm install grunt-contrib-clean -registry=https://registry.npm.taobao.org
npm install grunt-contrib-concat -registry=https://registry.npm.taobao.org
npm install grunt-contrib-watch -registry=https://registry.npm.taobao.org
npm install grunt-contrib-connect -registry=https://registry.npm.taobao.org
npm install grunt-contrib-copy -registry=https://registry.npm.taobao.org
npm install grunt-contrib-jasmine -registry=https://registry.npm.taobao.org
复制代码
http://192.168.1.8:9100/
复制代码
vi elasticsearch.yml
http.cors.enabled: true
http.cors.allow-origin: "*"
复制代码
[root@localhost /]# systemctl start docker
复制代码
[root@localhost /]# docker search elasticsearch
复制代码
[root@localhost /]# docker pull registry.docker-cn.com/library/elasticsearch
复制代码
[root@localhost /]# docker images
复制代码
docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9200:9200 -p 9300:9300 --name ES01 5acf0e8da90b
复制代码
[root@localhost /]# docker ps
复制代码
http://192.168.1.14:9200/
复制代码
集群就是包含了多个节点,每个节点属于哪个集群是经过一个集群名称配置。
集群中的一个节点,节点也存在名称,默认是随机分配一个名称,默认节点会加入到一个elasticsearch
集群中。
索引包含的是一大推类似结构的文档数据,例如咱们的商品索引,订单索引等,类比于咱们的数据库。
每个索引里面能够有一个或者多个type
,type
是index
中的一个逻辑数据分类,好比个人博客系统,一个索引,能够定义用户数据type
,能够定义文章数据type
,也能够定义评论数据type
,类比数据库的表。
文档是ElasticSearch
中最小的数据单元,一条Document
能够是一条文章数据,一条用户数据,一条评论数据,一般使用JSON
数据结构来表示,每一个index
下的type
中,存储多个document
,类别数据库中的行。
Field
是ElasticSearch
中的最小单位,一个document
里面粗在多个Field
字段,每一个Field
就是一个数据字段,类比数据库中的列。
数据如何存储在索引上,须要一个约束配置,例如数据类型,是否存储,查询的时候是否分词等等,类比数据库汇总的约束。
关系型数据库Mysql | 非关系型数据库ElasticSearch |
---|---|
数据库Database | 索引Index |
表Table | 类型Type |
数据行Row | 文档Document |
数据列Column | 字段Field |
约束Schema | 映射Mapping |
Springboot
默认使用Spring Data Elasticsearch
模块进行操做,同时也存在另一个操做ElasticSearch
的模块,那就是jest
。
Jest的GitHub地址:github.com/searchbox-i…
Jest文档地址:github.com/searchbox-i…
第一步:增长POM 文件
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.4</version>
</dependency>
复制代码
第二步:增长ElasticSearch配置项
spring:
elasticsearch:
jest:
uris: http://192.168.1.9:9200/
复制代码
第三步:使用JestClient进行交互
public class Users {
// 标示主键字段
@JestId
private Integer id;
private Integer code;
private String name;
private String sex;
private String age;
private String notes;
}
复制代码
@Autowired
JestClient jestClient;
@Test
public void contextLoads() {
// 给es中保存一份文档
Users users = new Users();
users.setId(2);
users.setCode(123456);
users.setAge("87");
users.setName("鲁班七号");
users.setSex("男");
users.setNotes("王者峡谷人见人想揍的小鲁班");
// 构建一个王者荣耀的索引和英雄角色类型
Index build = new Index.Builder(users).index("wzry").type("yxjs").build();
try {
jestClient.execute(build);
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testSeach(){
// 测试搜索es中知足条件的数据
String json = "{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"notes\" : \"峡谷小人\"\n" +
" }\n" +
" }\n" +
"}";
Search build = new Search.Builder(json).addIndex("wzry").addType("yxjs").build();
try {
SearchResult execute = jestClient.execute(build);
System.out.println(execute.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
复制代码
最后测试
http://192.168.1.9:9200/wzry/yxjs/2
复制代码
官方文档地址:docs.spring.io/spring-data…
第一步:增长POM文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
复制代码
第二步:增长配置项
spring:
data:
elasticsearch:
cluster-name: elasticsearch
cluster-nodes: 192.168.1.9:9300
repositories:
enabled: true
复制代码
第三步:进行数据交互
@Document(indexName = "study", type = "book")
public class Book {
private Integer id;
private String name;
private String notes;
}
复制代码
public interface BookRepository extends ElasticsearchRepository<Book, Integer> {
}
复制代码
@Autowired
BookRepository bookRepository;
@Test
public void testSpringDataEs(){
Book book = new Book();
book.setId(11);
book.setName("一个陌生女人的来信");
book.setNotes("还不错");
bookRepository.index(book);
}
复制代码
注意:若是启动报错,多是spring data elasticsearch和elasticsearch存在版本对应关系
版本对应参考官方文档:github.com/spring-proj…
spring data elasticsearch | elasticsearch |
---|---|
3.2.x | 6.5.0 |
3.1.x | 6.2.2 |
3.0.x | 5.5.0 |
2.1.x | 2.4.0 |
2.0.x | 2.2.0 |
1.3.x | 1.5.2 |
版本不适配解决方法
spring data elasticsearch
的版本号elasticsearch
便可elasticsearch
版本改变Springboot
版本解决办法示例:
# 安装对应版本的elasticsearch
[root@localhost /]# docker pull registry.docker-cn.com/library/elasticsearch:2.4
复制代码
# 启动对应版本的elasticsearch
[root@localhost /]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9200 -p 9301:9300 --name ES14 01e5bee1e059
复制代码
我我的本次测试环境:
1.5.19
2.4
mkdir
建立文件夹pwd
查看当前所在路径scp -r CentOS-7-x86_64-Minimal-1810.iso root@192.168.1.8:/opt/sortware
在当前文件上传文件到指定服务器文件夹cat /etc/centos-release
查看系统版本rm -rf jdk-8u201-linux-i586.tar.gz
不提示的递归删除文件或者文件夹tar -zxvf jdk-8u201-linux-x64.tar.gz
解压hostnamectl
查看主机名whereis sudoers
查找文件位置ls -l /etc/sudoers
查看文件权限chmod -v u+w /etc/sudoers
加入可写权限firewall-cmd --state
查看防火墙状态systemctl stop firewalld.service
关闭防火墙1.上传安装文件
/opt/sortware/jdk-8u201-linux-x64.tar.gz
复制代码
2.解压
tar -zxvf jdk-8u201-linux-x64.tar.gz
复制代码
3.重命名
mv jdk1.8.0_201 jdk1.8
复制代码
4.打开系统配置文件
vi /etc/profile
复制代码
5.添加环境变量
## Java
export JAVA_HOME=/opt/sortware/jdk1.8
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
复制代码
6.重启配置文件
source /etc/profile
复制代码
7.查看版本
java -version
复制代码
删除全部容器
docker rm `docker ps -a -q`
复制代码
查看存在的镜像
docker images
复制代码
查看全部启动的容器
docker ps -a
复制代码
中止容器
docker stop
复制代码
搜索仓库
docker search elasticsearch
复制代码
拉取仓库
docker pull registry.docker-cn.com/library/elasticsearch
复制代码