请注意,SpringBoot是2.2.0.RELEASE才兼容elasticsearch 7.x
废话不多说,直接上代码。
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.0.RELEASEversion>
<relativePath/>
parent>
<groupId>com.cntaiping.tpagroupId>
<artifactId>emsartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>emsname>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>11java.version>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
properties>
<repositories>
<repository>
<id>nexus-aliyunid>
<name>Nexus aliyunname>
<url>http://maven.aliyun.com/nexus/content/groups/publicurl>
repository>
repositories>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<mainClass>com.cntaiping.tpa.EmsApplicationmainClass>
configuration>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
server.port=8080
server.servlet.context-path=/ems
#https://blog.csdn.net/haohaifeng002/article/details/102887921
spring.elasticsearch.rest.uris=http://localhost:9200
spring.elasticsearch.rest.username=elastic
spring.elasticsearch.rest.password=123456
package com.cntaiping.tpa.bean;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
@Data
@NoArgsConstructor
@Accessors(chain = true)
@Document(indexName = "ems",type = "_doc", shards = 1, replicas = 0)
public class DocBean {
@Id
private Long id;
@Field(type = FieldType.Keyword)
private String firstCode;
@Field(type = FieldType.Keyword)
private String secordCode;
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String content;
@Field(type = FieldType.Integer)
private Integer type;
public DocBean(Long id,String firstCode,String secordCode,String content,Integer type){
this.id=id;
this.firstCode=firstCode;
this.secordCode=secordCode;
this.content=content;
this.type=type;
}
}
package com.cntaiping.tpa.dao;
import com.cntaiping.tpa.bean.DocBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.annotations.Query;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface ElasticRepository extends ElasticsearchRepository {
//默认的注释
//@Query("{\"bool\" : {\"must\" : {\"field\" : {\"content\" : \"?\"}}}}")
Page findByContent(String content, Pageable pageable);
@Query("{\"bool\" : {\"must\" : {\"field\" : {\"firstCode.keyword\" : \"?\"}}}}")
Page findByFirstCode(String firstCode, Pageable pageable);
@Query("{\"bool\" : {\"must\" : {\"field\" : {\"secordCode.keyword\" : \"?\"}}}}")
Page findBySecordCode(String secordCode, Pageable pageable);
}
package com.cntaiping.tpa.service;
import com.cntaiping.tpa.bean.DocBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Iterator;
import java.util.List;
public interface IElasticService {
void createIndex();
void deleteIndex(String index);
void save(DocBean docBean);
void saveAll(List list);
Iterator findAll();
Page findByContent(String content);
Page findByFirstCode(String firstCode);
Page findBySecordCode(String secordCode);
Page query(String key);
}
package com.cntaiping.tpa.service.impl;
import com.cntaiping.tpa.bean.DocBean;
import com.cntaiping.tpa.dao.ElasticRepository;
import com.cntaiping.tpa.service.IElasticService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.stereotype.Service;
import javax.print.Doc;
import java.util.Iterator;
import java.util.List;
@Service("elasticService")
public class ElasticServiceImpl implements IElasticService {
@Autowired
private ElasticsearchRestTemplate elasticsearchTemplate;
@Autowired
private ElasticRepository elasticRepository;
private Pageable pageable = PageRequest.of(0,10);
@Override
public void createIndex() {
elasticsearchTemplate.createIndex(DocBean.class);
}
@Override
public void deleteIndex(String index) {
elasticsearchTemplate.deleteIndex(index);
}
@Override
public void save(DocBean docBean) {
elasticRepository.save(docBean);
}
@Override
public void saveAll(List list) {
elasticRepository.saveAll(list);
}
@Override
public Iterator findAll() {
return elasticRepository.findAll().iterator();
}
@Override
public Page findByContent(String content) {
return elasticRepository.findByContent(content,pageable);
}
@Override
public Page findByFirstCode(String firstCode) {
return elasticRepository.findByFirstCode(firstCode,pageable);
}
@Override
public Page findBySecordCode(String secordCode) {
return elasticRepository.findBySecordCode(secordCode,pageable);
}
@Override
public Page query(String key) {
return elasticRepository.findByContent(key,pageable);
}
}
package com.cntaiping.tpa.controller;
import com.cntaiping.tpa.bean.DocBean;
//import com.cntaiping.tpa.bean.FundInfoBean;
import com.cntaiping.tpa.service.IElasticService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@Log4j2
@RestController
@RequestMapping("/elastic")
public class ElasticController {
@Autowired
private IElasticService elasticService;
@GetMapping("/init")
public void init(){
elasticService.createIndex();
List list =new ArrayList<>();
list.add(new DocBean(1L,"XX0193","XX8064","xxxxxx",1));
list.add(new DocBean(2L,"XX0210","XX7475","xxxxxxxxxx",1));
list.add(new DocBean(3L,"XX0257","XX8097","xxxxxxxxxxxxxxxxxx",1));
elasticService.saveAll(list);
}
@GetMapping("/all")
public Iterator all(){
return elasticService.findAll();
}
}
http://localhost:8080/ems/elastic/init