赞
踩
今天给大家简单的介绍一下SpringBoot如何集成Elasticsearch,并简单的介绍一下基于SpringBoot模式下怎么进行简单的增删改查操作,这边增删改查操作有点类似于JPA的模式。(什么是JPA模式,大家可以自行搜索答案)
废话不多说,现在马上开始我们今天的内容。如何新建Springboot项目我这边就不废话了,不会的同学可以看我以前写的教程。
文章地址:IDEA上面如何创建SpringBoot项目-CSDN博客
1.首先是引入相关的依赖,下面是我的pom文件。
- <?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>com.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>jar</packaging>
-
- <name>ElasticSearch</name>
- <description>ElasticSearch project for Spring Boot</description>
-
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.0.2.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>
- <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
- <dependency>
- <groupId>com.google.code.gson</groupId>
- <artifactId>gson</artifactId>
- <version>2.8.0</version>
- </dependency>
-
- <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-elasticsearch -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
- <version>2.0.2.RELEASE</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </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>
pom.xml文件中最重要的其实就是引入ES(Elasticsearch的简称后面我都这么叫),也就是spring-boot-starter-data-elasticsearch 依赖。
2.接下来就是对应的配置文件了,具体配置文件如下所示:
- # elasticsearch集群名称,默认的是elasticsearch
- spring.data.elasticsearch.cluster-name=my-application
-
- #节点的地址 注意api模式下端口号是9300,千万不要写成9200
- spring.data.elasticsearch.cluster-nodes=192.168.11.24:9300
-
- #是否开启本地存储
- spring.data.elasticsearch.repositories.enable=true
3.索引对应的实体类如下所示:
- package com.elasticsearch.entity;
- import org.springframework.data.annotation.Id;
- import org.springframework.data.elasticsearch.annotations.Document;
- import org.springframework.data.elasticsearch.annotations.Field;
-
- /**
- * @author linzhiqiang
- * @date 2018/5/16
- */
- @Document(indexName = "company",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")
- public class Employee {
- @Id
- private String id;
- @Field
- private String firstName;
- @Field
- private String lastName;
- @Field
- private Integer age = 0;
- @Field
- private String about;
-
- public String getId() {
- return id;
- }
-
- public void setId(String id) {
- this.id = id;
- }
-
- public String getFirstName() {
- return firstName;
- }
-
- public void setFirstName(String firstName) {
- this.firstName = firstName;
- }
-
- public String getLastName() {
- return lastName;
- }
-
- public void setLastName(String lastName) {
- this.lastName = lastName;
- }
-
- public Integer getAge() {
- return age;
- }
-
- public void setAge(Integer age) {
- this.age = age;
- }
-
- public String getAbout() {
- return about;
- }
-
- public void setAbout(String about) {
- this.about = about;
- }
- }
4.实体类对应的dao接口如下所示:
- package com.elasticsearch.dao;
-
- /**
- * Created by 19130 on 2018/5/16.
- */
- import com.elasticsearch.entity.Employee;
- import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
- import org.springframework.stereotype.Component;
- /**
- * @author linzhiqiang
- */
- @Component
- public interface EmployeeRepository extends ElasticsearchRepository<Employee,String>{
-
- /**
- * 查询雇员信息
- * @param id
- * @return
- */
- Employee queryEmployeeById(String id);
- }
5.实体类对应的控制类如下所示:
- package com.elasticsearch.controller;
- import com.elasticsearch.dao.EmployeeRepository;
- import com.elasticsearch.entity.Employee;
- import com.google.gson.Gson;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- /**
- * @author linzhiqiang
- */
- @RestController
- @RequestMapping("es")
- public class EmployeeController {
-
- @Autowired
- private EmployeeRepository employeeRepository;
-
- /**
- * 添加
- * @return
- */
- @RequestMapping("add")
- public String add() {
- Employee employee = new Employee();
- employee.setId("1");
- employee.setFirstName("xuxu");
- employee.setLastName("zh");
- employee.setAge(26);
- employee.setAbout("i am in peking");
- employeeRepository.save(employee);
- System.err.println("add a obj");
- return "success";
- }
-
- /**
- * 删除
- * @return
- */
- @RequestMapping("delete")
- public String delete() {
- Employee employee = employeeRepository.queryEmployeeById("1");
- employeeRepository.delete(employee);
- return "success";
- }
-
- /**
- * 局部更新
- * @return
- */
- @RequestMapping("update")
- public String update() {
- Employee employee = employeeRepository.queryEmployeeById("1");
- employee.setFirstName("哈哈");
- employeeRepository.save(employee);
- System.err.println("update a obj");
- return "success";
- }
- /**
- * 查询
- * @return
- */
- @RequestMapping("query")
- public Employee query() {
- Employee accountInfo = employeeRepository.queryEmployeeById("1");
- System.err.println(new Gson().toJson(accountInfo));
- return accountInfo;
- }
- }
以上所有的增删改查操作都要基于搭建好的ES系统(关于Linux上面如何搭建ES系统大家可以自行google)
关于我踩过的坑:
1.ES中API的端口号是9300而不是9200。
2.ES系统中Elasticsearch.yml配置文件中要加入network.host: 0.0.0.0,否则外网地址访问不了。
3.最新的资料一定要去官网上面查看,博客上面好多都是过时的。官网地址:Elasticsearch Platform — Find real-time answers at scale | Elastic
4.注意JDK、ES、Springboot三者之间的版本,很多时候错误都是版本冲突引起的。
下一节课程将会给大家介绍一下,Java是如何通过ES提供的API进行索引的增删改查操作。(和今天的模式不一样哦)
对文章有什么疑问或者想要看更多文章可以加我订阅号,欢迎大家的踩踩~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。