1、ElasticSearch学习随笔之基础介绍
2、ElasticSearch学习随笔之简单操作
3、ElasticSearch学习随笔之java api 操作
4、ElasticSearch学习随笔之SpringBoot Starter 操作
5、ElasticSearch学习随笔之嵌套操作
6、ElasticSearch学习随笔之分词算法
7、ElasticSearch学习随笔之高级检索
ElasticSearch学习随笔之SpringBoot Starter 操作ElasticSearch,创始人 Shay Banon(谢巴农)
本文主讲 SpringBoot Starter 操作ES。
本文主要针对 SpringBoot Starter 如何操作 ES 记录简单案例。
话不多说,直接copy,后面要用 Junit Test 测试,加入测试包
<!--整合 ES-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
server:
port: 8001
spring:
application:
name: xxxx-server #随便写个名字
elasticsearch: #以下配置 主要用于 ElasticsearchRepository (两种操作方式,方式一配置)
rest:
uris: http://localhost:9200 #连接地址
data: #以下配置主要用于 ElasticsearchRestTemplate (两种操作方式,方式二配置)
elasticsearch:
repositories:
enabled: true
client:
reactive:
endpoints: localhost:9200
package com.wesh.es;
import com.wesh.home.HomeApplication;
import com.wesh.home.dao.es.TenderTestRepository;
import com.wesh.home.model.Tender;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.Query;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HomeApplication.class)
public class ESTest {
}
package com.wesh.home.model;
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;
@Document(indexName = "tender_test3")
public class Tender {
@Id
private String id;
@Field(name = "product_agency", type = FieldType.Keyword)
private String productAgency;
@Field(name = "notice_type_id", type = FieldType.Keyword)
private String noticeTypeId;
@Field(name = "product_name", type = FieldType.Keyword)
private String productName;
@Field(name = "notice_type", type = FieldType.Keyword)
private String noticeType;
/*get set 方法省略*/
我们只需要定义接口即可进行相关的 ES 简单操作,不过要集成 Spring Data 提供的接口 ElasticsearchRepository。
Spring Data 提供的 接口中,按照 我们定义的方法名称 进行 ES 查询操作。具体规则可查阅 Spring官网总结方法命名规则
package com.wesh.home.dao.es;
import com.wesh.home.model.Tender;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface TenderTestRepository extends ElasticsearchRepository<Tender, String> {
/**
* 根据 productName 查询,根据 方法 名称查询
* @param productName
* @param pageRequest
* @return
*/
Page<Tender> findByProductName(String productName, PageRequest pageRequest);
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HomeApplication.class)
public class ESTest {
@Resource
private TenderTestRepository tenderTestRepository;
@Test
public void save(){
Tender tender = new Tender();
tender.setNoticeType("招标广告");
tender.setNoticeTypeId("12");
tender.setProductName("血培养仪");
tender.setProductAgency("山东方中工程管理有限公司");
tenderTestRepository.save(tender);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HomeApplication.class)
public class ESTest {
@Resource
private TenderTestRepository tenderTestRepository;
@Test
public void get(){
Tender tender = tenderTestRepository.findById("kmfaxoABYWTnJb2BeotB").get();
System.out.println(tender);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HomeApplication.class)
public class ESTest {
@Resource
private TenderTestRepository tenderTestRepository;
/**
* 这里调用的就是 根据 productName 字段查询,spring data 提供的接口会根据名称组装查询
*/
@Test
public void findByProductName(){
Page<Tender> tenders = tenderTestRepository.findByProductName("血培养仪", PageRequest.of(0,10));
tenders.forEach(tender -> System.out.println(tender));
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HomeApplication.class)
public class ESTest {
@Resource
private TenderTestRepository tenderTestRepository;
@Test
public void update(){
Tender tender = tenderTestRepository.findById("kmfaxoABYWTnJb2BeotB").get();
tender.setProductAgency("微生物鉴定药敏鉴定器");
tender.setProductName("鉴定药敏鉴定器");
tenderTestRepository.save(tender);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HomeApplication.class)
public class ESTest {
@Resource
private TenderTestRepository tenderTestRepository;
@Test
public void delete(){
Tender tender = tenderTestRepository.findById("kmfaxoABYWTnJb2BeotB").get();
tenderTestRepository.delete(tender);
}
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes = HomeApplication.class)
public class ESTest {
@Autowired
private ElasticsearchRestTemplate esRestTemplate;
/**
* 用 restTemplate操作
*/
@Test
public void findByQuery(){
QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("山东方中工程管理有限公司", "product_agency");
Query query = new NativeSearchQueryBuilder().withQuery(queryBuilder).build();
SearchHits<Tender> tenderSearchHits = esRestTemplate.search(query, Tender.class, IndexCoordinates.of("tender_test3"));
tenderSearchHits.get().forEach(tenderSearchHit -> {
Tender content = tenderSearchHit.getContent();
System.out.println(content);
});
}
}
以上是Spring Data 提供的接口对 ES 的增删改查的简单案例,方便公司里面临时有需要对ES操作大量数据是,可以直接copy 执行。
后期有时间持续更新。