官网介绍:Building Queries | Java REST Client [7.17] | Elastic
一、集成方法
需要在pom文件引入spring-starter-data-elasticsearch的依赖,注意版本对应关系。
三、demo
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>org.example</groupId>
<artifactId>es-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<elasticsearch.version>6.4.1</elasticsearch.version>
<spring.data.elasticsearch.version>3.1.0.RELEASE</spring.data.elasticsearch.version>
</properties>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.plugin</groupId>
<artifactId>transport-netty4-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>${spring.data.elasticsearch.version}</version>
</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>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!-- <repositories>-->
<!-- <repository>-->
<!-- <id>spring-milestones</id>-->
<!-- <name>Spring Milestones</name>-->
<!-- <url>https://repo.spring.io/libs-milestone</url>-->
<!-- <snapshots>-->
<!-- <enabled>false</enabled>-->
<!-- </snapshots>-->
<!-- </repository>-->
<!-- </repositories>-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、配置文件application.properties:
server.port=9999
server.servlet.path =/es
# elasticsearch集群名称,默认的是elasticsearch
spring.data.elasticsearch.cluster-name=elasticsearch
#节点的地址 注意api模式下端口号是9300,千万不要写成9200
spring.data.elasticsearch.cluster-nodes=xxx.xx.xxx.xx:9300
#是否开启本地存储
spring.data.elasticsearch.repositories.enable=true
3、实体:
package com.es.demo.dto;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
@Data
//index命名小写字母
@Document(indexName = "wtyy")
public class UserDTO implements Serializable {
@Id
private String id;
private String name;
private String address;
private Integer age;
//省略所有get set方法
@Override
public String toString() {
return "UserDTO{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", address='" + address + '\'' +
", age=" + age +
'}';
}
}
4、repository:
package com.es.demo.dao;
import com.es.demo.dto.UserDTO;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends ElasticsearchRepository<UserDTO, String> {
}
5、service:
package com.es.demo.service;
import com.es.demo.dto.UserDTO;
import org.springframework.data.domain.Page;
import java.util.List;
public interface UserService {
long count();
UserDTO save(UserDTO commodity);
void delete(UserDTO commodity);
Iterable<UserDTO> getAll();
}
实现类:
package com.es.demo.service.impl;
import com.es.demo.dao.UserRepository;
import com.es.demo.dto.UserDTO;
import com.es.demo.service.UserService;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;
//新增
@Override
public UserDTO save(UserDTO user) {
return userRepository.save(user);
}
//删除
@Override
public void delete(UserDTO user) {
userRepository.delete(user);
//userRepository.deleteById(user.getSkuId());
}
//查询总数
@Override
public long count() {
return userRepository.count();
}
//查询全部列表
@Override
public Iterable<UserDTO> getAll() {
return userRepository.findAll();
}
}
6、controller:
package com.es.demo.controller;
import com.es.demo.dto.UserDTO;
import com.es.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//新增
@RequestMapping("/add")
public void testInsert(UserDTO user) {
userService.save(user);
}
//删除
@RequestMapping("/delete")
public void testDelete(String id) {
UserDTO user = new UserDTO();
user.setId(id);
userService.delete(user);
}
//修改
//查询总数
@RequestMapping("/getCount")
public Long contextLoads() {
return userService.count();
}
//查询全部列表
@RequestMapping("/getAll")
public Iterable<UserDTO> testGetAll() {
Iterable<UserDTO> iterable = userService.getAll();
iterable.forEach(e->System.out.println(e.toString()));
return iterable;
}
}
7、启动类:
package com.es.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@SpringBootApplication
//@EnableElasticsearchRepositories(basePackages = "com.es.demo.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
三、测试:post调用接口,从kibana可视化工具验证结果:
1、新增:
(1)指定id: http://localhost:9999/es/user/add?id=1&name=用户1&age=1&address=地址1
(2)不指定id:http://localhost:9999/es/user/add?name=用户2&age=2&address=地址2
2、删除:多新增几个后删除:http://localhost:9999/es/user/delete?id=5
3、修改:
4、查询总数:http://localhost:9999/es/user/getCount
5、查询全部列表:http://localhost:9999/es/user/getAll,返回结果:
{
"content": [
{
"id": "4",
"name": "用户4",
"address": "地址4",
"age": 4
},
{
"id": "1",
"name": "用户1",
"address": "地址1",
"age": 1
},
{
"id": "HZBHjXUBYIFzjJZSeQNp",
"name": "用户2",
"address": "地址2",
"age": 2
},
{
"id": "3",
"name": "用户3",
"address": "地址3",
"age": 3
}
],
"pageable": {
"sort": {
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageSize": 4,
"pageNumber": 0,
"paged": true,
"unpaged": false
},
"facets": [],
"aggregations": null,
"scrollId": null,
"maxScore": 1,
"totalPages": 1,
"totalElements": 4,
"number": 0,
"size": 4,
"sort": {
"sorted": false,
"unsorted": true
},
"numberOfElements": 4,
"first": true,
"last": true
}