本文的Spring Boot版本为1.5.9,Elasticsearch版本为2.4.4,话不多说,直接上代码。

一、启动Elasticsearch

  在官网上下载Elasticsearch后,打开bin目录下的elasticsearch.bat,出现下面的图,就证明成功启动了。

二、新建项目,添加依赖

  在创建spring boot项目中,可以在nosql中选择添加Elasticsearch的依赖,完整的依赖如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>cmo.jf.cloud</groupId>
<artifactId>resourceCente</artifactId>
<version>1.0.0</version>
<packaging>war</packaging> <name>Elasticsearch</name>
<description>搜索引擎</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

  需要注意的是:

  1.spring boot2.X版本必须使用Elasticsearch 5.X版本

  2.Elasticsearch 2.X的版本必须使用spring boot1.5版本

  3.目前spring-boot-starter-data-elasticsearch还不支持Elasticsearch 6.X版本

  如果出现版本不兼容,这会出现下面的错误:

NoNodeAvailableException[None of the configured nodes are available: [{#transport#-1}{729dgKKVSF-ti27v2_w68g}{127.0.0.1}{127.0.0.7:9300}]]

三、Elasticsearch的配置文件

  和操作数据库一样,我们需要配置好配置文件,才能访问Elasticsearch,配置文件如下:

##端口号
server.port=8888
##es地址
spring.data.elasticsearch.cluster-nodes =127.0.0.1:9300

四、Elasticsearch的实体类

  在Elasticsearch中,我们需要创建一个实体类作为索引,简单的理解就是在Elasticsearch中创建数据库和表。

   利用@Document注解可以创建数据库和表。其中Document中的参数意义为:

   indexName:索引名称 可以理解为数据库名 必须为小写不然会报org.elasticsearch.indices.InvalidIndexNameException异常

type:类型 可以理解为表名

   代码如下:  

import java.io.Serializable;

import org.springframework.data.elasticsearch.annotations.Document;

//indexName索引名称 可以理解为数据库名 必须为小写 不然会报org.elasticsearch.indices.InvalidIndexNameException异常
//type类型 可以理解为表名
@Document(indexName = "class",type = "user")
public class User implements Serializable {
//定义成员属性
private Integer id;
private String name;
private Integer age;
private String sex;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public User(Integer id, String name, Integer age, String sex) {
super();
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public User() {
super();
} }

五、创建一个数据访问层

  同样,和其他的数据库访问操作一样,我们需要创建一个dao层来操作Elasticsearch,这个dao层类似于mongodb的操作,只需要继承ElasticsearchRepository接口就能实现。

import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import cmo.jf.cloud.bean.User; @Configuration
public interface UserMapper extends ElasticsearchRepository<User,Long>{ }

六、创建controller层测试

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import cmo.jf.cloud.bean.User;
import cmo.jf.cloud.dao.UserMapper; @RestController
public class UserController {
@Autowired
UserMapper userMapper; // 访问接口地址:localhost:8888/save
//存储数据
@GetMapping("save")
public String save(){
User user = new User(1,"张三",15,"男");
userMapper.save(user);
return "success";
} //访问接口地址:localhost:8888/delete?id=1
//根据ID删除数据
@GetMapping("delete")
public String delete(long id){
userMapper.delete(id);
return "success";
} //访问接口地址:localhost:8888/getOne?id=1
//根据ID查询数据
@GetMapping("getOne")
public User getOne(long id){
User user = userMapper.findOne(id);
return user;
}
}

在添加几条数据后我们访问head插件---ES管理界面可以看到我们添加的数据库在页面上

在数据浏览界面中可以看到我们添加的数据

在基本查询中可以根据表名和条件,查询我们刚才插入的数据

七、项目中出现的错误

  直接上错误代码:

Description:

Cannot determine embedded database driver class for database type NONE

Action:

If you want an embedded database please put a supported one on the classpath.
If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

          这个错误是由于打开了spring boot的自动配置数据,但是没有配置数据源,所以报错,解决的方案是直接关闭数据源自动配置,

只需要在启动类上加入:@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})即可

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ElasticsearchApplication { public static void main(String[] args) {
SpringApplication.run(ElasticsearchApplication.class, args);
}
}

八、小结

  这就是spring boot1.5整合ElasticSearch 2.X的操作,本文作为初级学习起来还是有帮助,但是ElasticSearch 中的一些高级操作如:同步数据库,分词器,高亮等都将在后面的文章中进行书写。ElasticSearch 6.X、ElasticSearch 5.X的API方式也和ElasticSearch 2.X不同,我也会放在后面进行讲解。

Elasticsearch学习(1) Spring boot整合Elasticsearch的更多相关文章

  1. Elasticsearch学习(3) spring boot整合Elasticsearch的原生方式

    前面我们已经介绍了spring boot整合Elasticsearch的jpa方式,这种方式虽然简便,但是依旧无法解决我们较为复杂的业务,所以原生的实现方式学习能够解决这些问题,而原生的学习方式也是E ...

  2. Elasticsearch学习(4) spring boot整合Elasticsearch的聚合操作

    之前已将spring boot原生方式介绍了,接下将结介绍的是Elasticsearch聚合操作.聚合操作一般来说是解决一下复杂的业务,比如mysql中的求和和分组,由于博主踩的坑比较多,所以博客可能 ...

  3. Spring Boot整合Elasticsearch

    Spring Boot整合Elasticsearch   Elasticsearch是一个全文搜索引擎,专门用于处理大型数据集.根据描述,自然而然使用它来存储和搜索应用程序日志.与Logstash和K ...

  4. 【spring boot】【elasticsearch】spring boot整合elasticsearch,启动报错Caused by: java.lang.IllegalStateException: availableProcessors is already set to [8], rejecting [8

    spring boot整合elasticsearch, 启动报错: Caused by: java.lang.IllegalStateException: availableProcessors ], ...

  5. Spring Boot 项目学习 (四) Spring Boot整合Swagger2自动生成API文档

    0 引言 在做服务端开发的时候,难免会涉及到API 接口文档的编写,可以经历过手写API 文档的过程,就会发现,一个自动生成API文档可以提高多少的效率. 以下列举几个手写API 文档的痛点: 文档需 ...

  6. Spring Boot 整合 Elasticsearch,实现 function score query 权重分查询

    摘要: 原创出处 www.bysocket.com 「泥瓦匠BYSocket 」欢迎转载,保留摘要,谢谢! 『 预见未来最好的方式就是亲手创造未来 – <史蒂夫·乔布斯传> 』 运行环境: ...

  7. spring boot 整合 elasticsearch 5.x

    spring boot与elasticsearch集成有两种方式.一种是直接使用elasticsearch.一种是使用data中间件. 本文只指针使用maven集成elasticsearch 5.x, ...

  8. Spring Boot 整合 elasticsearch

    一.简介 我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的 首选.他可以快速的存储.搜索和分析海量数据.Spring Boot通过整合Spring Data E ...

  9. Spring Boot整合ElasticSearch和Mysql 附案例源码

    导读 前二天,写了一篇ElasticSearch7.8.1从入门到精通的(点我直达),但是还没有整合到SpringBoot中,下面演示将ElasticSearch和mysql整合到Spring Boo ...

随机推荐

  1. ios常用空间UIScrollViewIndicator的一些属性

    UIScrollView属性: 1  alwaysBounceHorizontal         BOOL值,当水平滚条到达终点,总是(视图)弹跳 2  alwaysBounceVertical   ...

  2. MongoDB系列[2]:MongoDB导入导出以及数据库备份

    PS: 以下所有操作都是基于MongoDB自带的工具进行的,所以操作时一定要手动切换到Mongodb的bin目录下面,并且使用管理员权限运行命令 导出工具 mongoexport 概念: mongoD ...

  3. 微信小程序开发注意点和坑集

    开发(Tips)   避开频繁setData * 小程序端对于频繁的逻辑层和显示层的交互很不友好,特别是安卓机,与浏览器上js直接操作DOM不同,小程序通过逻辑更新显示层并不完全实时,开发者应避免出现 ...

  4. 201671010140. 2016-2017-2 《Java程序设计》java学习第十一周

     java学习第十一周     本周,进行了java集合方面的知识,在博客园的帮助下,我的课前预习更有条理性,重点明确,本周的课堂反应明显更好了,首先,梳理一下本周知识点.  Collection   ...

  5. Perl 获取时间函数

    Perl 时间日期 Perl中处理时间的函数有如下几种:    1.time() 函数:返回从1970年1月1日起累计的秒数    2.localtime() 函数:获取本地时区时间(多用这个)    ...

  6. Python 爬虫之 Scrapy 分布式原理以及部署

    Scrapy分布式原理 关于Scrapy工作流程 Scrapy单机架构 上图的架构其实就是一种单机架构,只在本机维护一个爬取队列,Scheduler进行调度,而要实现多态服务器共同爬取数据关键就是共享 ...

  7. [C++] STACK_Principle

    STACK_Principle

  8. IPMI命令

    yum -y install ipmitool/etc/init.d/ipmi start ipmitool -I open lan set 1 ipaddr 172.16.8.33ipmitool ...

  9. python3--json序列化

    # Auther: Aaron Fan # 把数据存入到一个文件中 # json格式的数据几乎可以通用语任何编程语言,但是仅仅只是简单的格式转换# 比如:字典.列表.元组.字符串这些,像函数.类就不可 ...

  10. HTML的DOM树结构

    在面试连续跪了两轮后,我觉得两个月的前端白学了.主要的原因是学而不思,知识是零散的,并没有组织起来.于是,我决定从今天起,复习并总结一下前端的知识点. 一般的网页浏览者看到的是网页的整体外观,前端开发 ...