一、下载安装ElasticSearch
访问官网的下载地址:https://www.elastic.co/downloads/elasticsearch,windows版的下载ZIP格式的。
如果不想下载最新版的,可以点击“past releases”选择过去的版本。
双击bin下的elasticsearch.bat可以启动elasticSearch ,使用es存放的数据放在data目录下,
二、入门demo
build.gradle
// buildscript 代码块中脚本优先执行
buildscript {
// ext 用于定义动态属
ext {
springBootVersion = '1.5.2.RELEASE'
}
// 自定义 Elasticsearch 的版本
//ext['elasticsearch.version'] = '5.2.2'
// 使用了 Maven 的中央仓库(你也可以指定其他仓库)
repositories {
//mavenCentral()
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
}
// 依赖关系
dependencies {
// classpath 声明说明了在执行其余的脚本时,ClassLoader 可以使用这些依赖项
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
// 使用插件
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
// 打包的类型为 jar,并指定了生成的打包的文件名称和版本
jar {
baseName = 'elasticsearch-in-action'
version = '1.0.0'
}
// 指定编译 .java 文件的 JDK 版本
sourceCompatibility = 1.8
// 默认使用了 Maven 的中央仓库。这里改用自定义的镜像库
repositories {
//mavenCentral()
maven {
url 'http://maven.aliyun.com/nexus/content/groups/public/'
}
}
// 依赖关系
dependencies {
// 该依赖对于编译发行是必须的
compile('org.springframework.boot:spring-boot-starter-web')
// 添加 Spring Data Elasticsearch 的依赖
compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')
// 添加 JNA 的依赖
compile('net.java.dev.jna:jna:4.3.0')
// 该依赖对于编译测试是必须的,默认包含编译产品依赖和编译时依
testCompile('org.springframework.boot:spring-boot-starter-test')
}
application.properties
#Elasticsearch 服务地址
spring.data.elasticsearch.cluster-nodes=localhost:9300
# 设置连接超时时间
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
blog
@Document(indexName = "blog", type = "blog")
//indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
//type类型 可以理解为表名
public class Blog implements Serializable {
private static final long serialVersionUID = 1L;
@Id // 主键
private String id; // 用户的唯一标识
private String title;
private String content;
protected Blog() { // JPA 的规范要求无参构造函数;设为 protected 防止直接使用
}
public Blog(String name, String content) {
this.title = name;
this.content = content;
}
public Blog(String id, String name, String content) {
this.id = id;
this.title = name;
this.content = content;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
BlogRepository
/**
* Blog 存储库.
*/
public interface BlogRepository extends ElasticsearchRepository<Blog, String> {
/**
* 根据用户名分页查询用户列表
* @param name
* @param pageable
* @return
*/
Page<Blog> findByTitleLikeOrContentLike(String title, String content, Pageable pageable);
}
Controller
/**
* Blog 控制器.
*/
@RestController
public class BlogController {
@Autowired
private BlogRepository blogRepository;
//http://localhost:8080/list?title=机&content=机
@GetMapping("list")
public List<Blog> list(@RequestParam(value="title",required=false,defaultValue="") String title,
@RequestParam(value="content",required=false,defaultValue="") String content,
@RequestParam(value="pageIndex",required=false,defaultValue="0") int pageIndex,
@RequestParam(value="pageSize",required=false,defaultValue="10") int pageSize) {
Pageable pageable = new PageRequest(pageIndex, pageSize);
Page<Blog> page = blogRepository.findByTitleLikeOrContentLike(title, content, pageable);
return page.getContent();
}
//http://localhost:8080/save
@GetMapping("save")
public String save(){
Blog blog = new Blog("1","手机","这是一个手机");
blogRepository.save(blog);
blog = new Blog("2","洗衣机","这是一个洗衣机");
blogRepository.save(blog);
blog = new Blog("3","洗碗机","这是一个洗碗机");
blogRepository.save(blog);
return "success";
}
}
三、运行测试
首先启动es,再启动项目,浏览器访问http://localhost:8080/save 先写入数据 再访问 http://localhost:8080/list?title=手机&content=手机
本文提供一个入门小demo,详细参考 https://blog.csdn.net/chen_2890/article/details/83895646