一、简介
Elasticsearch 是一个分布式可扩展的实时搜索和分析引擎,一个建立在全文搜索引擎 Apache Lucene(TM) 基础上的搜索引擎.当然 Elasticsearch 并不仅仅是 Lucene 那么简单,它不仅包括了全文搜索功能,还可以进行以下工作:
- 分布式实时文件存储,并将每一个字段都编入索引,使其可以被搜索。
- 实时分析的分布式搜索引擎。
- 可以扩展到上百台服务器,处理PB级别的结构化或非结构化数据。
二、安装
下载安装包:https://www.elastic.co/downloads/elasticsearch
1、windos 下安装
下载elasticsearch-6.5.0.zip并解压,进入bin目录下启动:
下载elasticsearch-head解压直接启动index.html即可,连接head需要修改elasticsearch.yml文件:
#allow origin
http.cors.enabled: true
http.cors.allow-origin: "*"
elasticsearch-head下载:https://download.csdn.net/download/typ1805/10918855
2、centos7 下安装
在官网上下载对应版本的ES安装包,或者我们可以服务器上 使用Linux的下载命令
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.0.tar.gz
创建文件夹,解压安装包:
[root@localhost usr]# mkdir elasticsearch
[root@localhost elasticsearch]# tar -vxf elasticsearch-5.5.0.tar.gz
修改配置文件elasticsearch.yml:
network.host: 0.0.0.0 ##监听主机
discovery.zen.minimum_master_nodes: 1 ##我这里只有一台,所以修改为1
启动(加 -d表示后台启动):
[root@localhost elasticsearch-5.5.0]# ./bin/elasticsearch
错误一:
主要原因是已经有提示了:Caused by: java.lang.RuntimeException: can not run elasticsearch as root,说是不能在root用户下运行,接下来我们换个用户来运行。
切换用户启动:
[root@localhost config]# useradd es
[root@localhost config]# passwd es
[root@localhost config]# su es
[es@localhost elasticsearch-5.5.0]$ ./bin/elasticsearch
错误二:
改变elasticsearch文件夹所有者到当前用户:
[es@localhost elasticsearch-5.5.0]$ sudo chown -R es:es elasticsearch
es is not in the sudoers file.This incident will be reported.
解决方法:
1.切换到root用户下
su root
2.添加sudo文件的写权限
chmod u+w /etc/sudoers
3.编辑sudoers文件
vi /etc/sudoers
找到这行 root ALL=(ALL) ALL,在他下面添加xxx ALL=(ALL) ALL (这里的xxx是你的用户名)
可以sudoers添加下面四行中任意一条
es ALL=(ALL) ALL ## 允许用户youuser执行sudo命令(需要输入密码).
%es ALL=(ALL) ALL ## 允许用户组youuser里面的用户执行sudo命令(需要输入密码).
es ALL=(ALL) NOPASSWD: ALL ## 允许用户youuser执行sudo命令,并且在执行的时候不输入密码.
%es ALL=(ALL) NOPASSWD: ALL ## 允许用户组youuser里面的用户执行sudo命令,并且在执行的时候不输入密码.
4.撤销sudoers文件写权限
chmod u-w /etc/sudoers
再次执行:
[es@localhost elasticsearch-5.5.0]$ sudo chown -R es:es elasticsearch
>>> /etc/sudoers: syntax error near line 93 <<<
sudo: parse error in /etc/sudoers near line 93
sudo: no valid sudoers sources found, quitting
sudo: unable to initialize policy plugin
原因是直接用vim修改文件时报错,应该用visudo命令来编辑sudoers文件。
解决办法,执行:
pkexec visudo
进入文件编辑,对于刚才修改错误的地方,直接修改,保存退出后,恢复正常.
错误三:
1、max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536]
切换到root用户,编辑limits.conf 添加类似如下内容
vi /etc/security/limits.conf
添加如下内容:
* soft nofile 65536
* hard nofile 131072
* soft nproc 2048
* hard nproc 4096
2、max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
切换到root用户修改配置sysctl.conf
vi /etc/sysctl.conf
添加下面配置:
vm.max_map_count=655360
执行:
sysctl -p
启动成功:
浏览器访问:http://192.168.0.148:9200/
三、集成
整合版本:
- SpringBoot-2.0.5.RELEASE
- Elasticsearch-5.5.0
- Spring-boot-starter-data-elasticsearch-2.0.5.RELEASE
- Spring--data-elasticsearch-3.0.10.RELEASE
1、添加ES相关的依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
配置文件:
server:
port: 8081
# ES
spring:
data:
elasticsearch:
cluster-nodes: 192.168.0.148:9200 #127.0.0.1:9300
cluster-name: typ
创建实体:
package com.example.demo.es.entity;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
/**
* 路径:com.example.demo.es.entity
* 类名:
* 功能:实体类
* 备注:indexName索引名称:可以理解为数据库名。必须为小写,不然会报org.elasticsearch.indices.InvalidIndexNameException异常
type类型:可以理解为表名
* 创建人:typ
* 创建时间:2019/1/8 9:41
* 修改人:
* 修改备注:
* 修改时间:
*/
@Data
@Document(indexName = "sys", type = "user")
public class User implements Serializable {
private Long id;
private String name;
private String description;
}
创建UserRepository 继承ElasticsearchRepository
package com.example.demo.es.repository;
import com.example.demo.es.entity.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
/**
* 路径:com.example.demo.es.repository
* 类名:
* 功能:《用一句描述一下》
* 备注:
* 创建人:typ
* 创建时间:2019/1/8 10:11
* 修改人:
* 修改备注:
* 修改时间:
*/
@Component
public interface UserRepository extends ElasticsearchRepository<User,Long> {
}
创建controller
package com.example.demo.es.controller;
import com.alibaba.fastjson.JSON;
import com.example.demo.es.entity.User;
import com.example.demo.es.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Optional;
/**
* 路径:com.example.demo.es.controller
* 类名:
* 功能:《用一句描述一下》
* 备注:
* 创建人:typ
* 创建时间:2019/1/8 10:13
* 修改人:
* 修改备注:
* 修改时间:
*/
@Slf4j
@RestController
public class UserController {
@Autowired
UserRepository userRepository;
/**
* 方法名:
* 功能:保存
* 描述:
* 创建人:typ
* 创建时间:2019/1/15 11:39
* 修改人:
* 修改描述:
* 修改时间:
*/
@PostMapping("/save")
public String save(@RequestBody User test) {
log.info("save 入参:{}", JSON.toJSON(test));
try {
userRepository.save(test);
return "保存成功!";
} catch (Exception e) {
e.printStackTrace();
}
return "保存失败!";
}
/**
* 方法名:
* 功能:根据ID查询
* 描述:
* 创建人:typ
* 创建时间:2019/1/15 11:39
* 修改人:
* 修改描述:
* 修改时间:
*/
@GetMapping("/findById")
public Object findById(Long id) {
log.info("findById 入参:id:{}", id);
Optional optional = userRepository.findById(id);
log.info("findById 出参:{}", JSON.toJSON(optional));
return optional;
}
/**
* 方法名:
* 功能:获取所有数据
* 描述:
* 创建人:typ
* 创建时间:2019/1/15 11:39
* 修改人:
* 修改描述:
* 修改时间:
*/
@GetMapping("/find")
public Object find() {
log.info("find 入参:{}");
Object object = userRepository.findAll();
log.info("find 出参:{}",JSON.toJSON(object));
return object;
}
/**
* 方法名:
* 功能:分页查询
* 描述:
* 创建人:typ
* 创建时间:2019/1/15 11:39
* 修改人:
* 修改描述:
* 修改时间:
*/
@GetMapping("/pageFind")
public Object pageFind(Integer pageNum, Integer pageSize){
log.info("pageFind 入参:pageNum:{},pageSize:{}",pageNum,pageSize);
Page<User> pageFind = userRepository.findAll(new PageRequest(pageNum,pageSize));
log.info("pageFind 出参:{}",JSON.toJSON(pageFind));
return pageFind;
}
}
启动工程,添加数据:
查看数据: