SpringBoot集成Elasticsearch并进行增删改查操作

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.最新的资料一定要去官网上面查看,博客上面好多都是过时的。官网地址:https://www.elastic.co

4.注意JDK、ES、Springboot三者之间的版本,很多时候错误都是版本冲突引起的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值