官网地址:https://www.elastic.co/products/elasticsearch
官方中文文档地址:https://www.elastic.co/guide/cn/elasticsearch/guide/current/index.html
ElasticSearch是一个分布式搜索服务,提供的是一组Restful API,底层基于Lucene,采用多shard(分片)的方式保证数据安全,并且提供自动resharding的功能。是目前全文搜索引擎的首选,可以快速的存储、搜索和分析海量数据,Springboot通过整合Spring Data ElasticSearch为我们提供了非常方便的检索功能支持。
Bash
[root@localhost elasticsearch-6.6.0]# mkdir data # 创建数据文件夹(6.0自带logs文件夹)Bash
vi elasticsearch.yml # 修改配置文件Bash
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"] # 主机名Bash
注意:node.name可以随便取,但是一个集群中不能重复,注意path.data前不能有空格,冒号后必须有一个空格
问题一:ERROR: bootstrap checks failed
su root vi /etc/security/limits.confBash
* soft nofile 65536 * hard nofile 131072 * soft nproc 2048 * hard nproc 4096Bash
问题二:max number of threads [1024] for user [lish] likely too low, increase to at least [2048]
vi /etc/security/limits.d/90-nproc.conf * soft nproc 2048Bash
问题三:max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
vi /etc/sysctl.conf vm.max_map_count=655360 sysctl -pBash
elasticsearch禁止使用root用户启动,需要新建一个testuser用户
[testuser@hadoop102 elasticsearch-6.6.0]$ ./bin/elasticsearchBash
**访问地址:**http://192.168.1.8:9200/
插件地址:https://github.com/zt1115798334/elasticsearch-head-master
Nodejs安装
tar -zxvf node-v10.15.1-linux-x64.tar.gz -C /opt/module/Bash
vi /etc/profile export NODE_HOME=/opt/module/node-v10.15.1-linux-x64 export PATH=$PATH:$NODE_HOME/bin source /etc/profileBash
elasticsearch-head-master安装
[root@hadoop102 sortware]# unzip elasticsearch-head-master.zip -d /opt/module/Bash
[root@hadoop102 elasticsearch-head-master]# npm install grunt --saveBash
npm install -g cnpm --registry=https://registry.npm.taobao.orgBash
npm install -g grunt-cliBash
vim Gruntfile.jsBash
options: { hostname:'0.0.0.0', port: 9100, base: '.', keepalive: true }JSON
# 检查head根目录下是否存在base文件夹 没有:将 _site下的base文件夹及其内容复制到head根目录下 mkdir base cp base/* ../base/Bash
[root@hadoop102 module]# chown -R luokangyuan:luokangyuan elasticsearch-head-master/Bash
[luokangyuan@hadoop102 elasticsearch-head-master]$ grunt server -dBash
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.orgBash
http://192.168.1.8:9100/vi elasticsearch.yml http.cors.enabled: true http.cors.allow-origin: "*"
Bash
Bash
Bash
Bash
Bash
Bash
Bash
集群就是包含了多个节点,每一个节点属于哪一个集群是通过一个集群名称配置。
集群中的一个节点,节点也存在名称,默认是随机分配一个名称,默认节点会加入到一个elasticsearch集群中。
索引包含的是一大推相似结构的文档数据,例如我们的商品索引,订单索引等,类比于我们的数据库。
每一个索引里面可以有一个或者多个type,type是index中的一个逻辑数据分类,比如我的博客系统,一个索引,可以定义用户数据type,可以定义文章数据type,也可以定义评论数据type,类比数据库的表。
文档是ElasticSearch中最小的数据单元,一条Document可以是一条文章数据,一条用户数据,一条评论数据,通常使用JSON数据结构来表示,每个index下的type中,存储多个document,类别数据库中的行。
Field是ElasticSearch中的最小单位,一个document里面粗在多个Field字段,每个Field就是一个数据字段,类比数据库中的列。
数据如何存储在索引上,需要一个约束配置,例如数据类型,是否存储,查询的时候是否分词等等,类比数据库汇总的约束。
Springboot默认使用Spring Data Elasticsearch模块进行操作,同时也存在另外一个操作ElasticSearch的模块,那就是jest。
Jest的GitHub地址:https://github.com/searchbox-io/Jest
Jest文档地址:https://github.com/searchbox-io/Jest/tree/master/jest
第一步:增加POM 文件
<dependency> <groupId>io.searchbox</groupId> <artifactId>jest</artifactId> <version>5.3.4</version> </dependency>XML
第二步:增加ElasticSearch配置项
spring: elasticsearch: jest: uris: http://192.168.1.9:9200/YAML
第三步:使用JestClient进行交互
public class Users { // 标示主键字段 @JestId private Integer id; private Integer code; private String name; private String sex; private String age; private String notes; }Java
@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(); } }Java
最后测试
http://192.168.1.9:9200/wzry/yxjs/2.properties
官方文档地址:https://docs.spring.io/spring-data/elasticsearch/docs/3.1.5.RELEASE/reference/html/
第一步:增加POM文件
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>XML
第二步:增加配置项
spring: data: elasticsearch: cluster-name: elasticsearch cluster-nodes: 192.168.1.9:9300 repositories: enabled: trueYAML
第三步:进行数据交互
@Document(indexName = "study", type = "book") public class Book { private Integer id; private String name; private String notes; }Java
public interface BookRepository extends ElasticsearchRepository<Book, Integer> { }Java
@Autowired BookRepository bookRepository; @Test public void testSpringDataEs(){ Book book = new Book(); book.setId(11); book.setName("一个陌生女人的来信"); book.setNotes("还不错"); bookRepository.index(book); }Java
注意:如果启动报错,可能是spring data elasticsearch和elasticsearch存在版本对应关系
版本对应参考官方文档:https://github.com/spring-projects/spring-data-elasticsearch/blob/master/README.md
spring data elasticsearchelasticsearch3.2.x6.5.03.1.x6.2.23.0.x5.5.02.1.x2.4.02.0.x2.2.01.3.x1.5.2版本不适配解决方法
查看spring data elasticsearch的版本号安装对应版本的elasticsearch即可当然也可以根据安装的elasticsearch版本改变Springboot版本解决办法示例:
# 安装对应版本的elasticsearch [root@localhost /]# docker pull registry.docker-cn.com/library/elasticsearch:2.4Bash
# 启动对应版本的elasticsearch [root@localhost /]# docker run -e ES_JAVA_OPTS="-Xms256m -Xmx256m" -d -p 9201:9200 -p 9301:9300 --name ES14 01e5bee1e059Bash
我个人本次测试环境:
Springboot:1.5.19elasticsearch:2.41.上传安装文件
/opt/sortware/jdk-8u201-linux-x64.tar.gzBash
2.解压
tar -zxvf jdk-8u201-linux-x64.tar.gzBash
3.重命名
mv jdk1.8.0_201 jdk1.8Bash
4.打开系统配置文件
vi /etc/profileBash
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/binBash
6.重启配置文件
source /etc/profileBash
7.查看版本
java -versionBash
删除所有容器
docker rm `docker ps -a -q`Bash
查看存在的镜像
docker imagesBash
查看所有启动的容器
docker ps -aBash
停止容器
docker stop搜索仓库
docker search elasticsearch拉取仓库
docker pull registry.docker-cn.com/library/elasticsearch
赏
