目录
一、Maven项目集成Easticsearch
1)客户端对象
2)索引操作
3)文档操作
4)高级查询
二、springboot项目集成Spring Data操作Elasticsearch
1)pom文件
2)yaml
3)数据实体类
4)配置类
5)Dao数据访问对象
6)索引操作
7)文档操作
8)文档搜索
三、springboot项目集成bboss操作elasticsearch
1)pom文件和配置
2)getConfigRestClientUtil和getRestClientUtil区别
3)创建索引
4)删除索引
5)判断索引是否存在
6)判断索引类型是否存在
7)添加单个文档
8)批量添加文档
9)查询单个文档
Elasticsearch软件是由 Java 语言开发的,所以也可以通过 Java API 的方式对 Elasticsearch服务进行访问。 修改pom 文件,增加 Maven 依赖关系。
1.8
7.4.2
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.4.2
创建类,代码中创建 Elasticsearch 客户端对象因为早期版本的客户端对象已经不再推荐使用,且在未来版本中会被删除,所以这里我们采用高级 REST 客户端对象;
public static void main(String[] args) throws IOException {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
);
// 关闭客户端连接
client.close();
}
ES服务器正常启动后,可以通过 Java API 客户端对象对 ES 索引进行操作 ;
public class EsIndex {
public static void main(String[] args) throws IOException {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
);
// 关闭客户端连接
client.close();
}
// 创建索引
public static void createIndex(RestHighLevelClient client) throws IOException {
// 创建索引 - 请求对象
CreateIndexRequest request = new CreateIndexRequest("user");
// 发送请求,获取响应
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
boolean acknowledged = response.isAcknowledged();
// 响应状态
System.out.println("操作状态 = " + acknowledged);
}
// 查看索引
public static void getIndex(RestHighLevelClient client) throws IOException {
// 查询索引 - 请求对象
GetIndexRequest request = new GetIndexRequest("user");
// 发送请求,获取响应
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
System.out.println("aliases: " + response.getAliases());
System.out.println("mappings: " + response.getMappings());
System.out.println("settings: " + response.getSettings());
}
// 删除索引
public static void deleteIndex(RestHighLevelClient client) throws IOException {
// 删除索引 - 请求对象
DeleteIndexRequest request = new DeleteIndexRequest("user");
// 发送请求,获取响应
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
// 操作结果
System.out.println("操作结果: " + response.isAcknowledged());
}
}
public class EsDoc {
public static void main(String[] args) throws IOException {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
);
// 关闭客户端连接
client.close();
}
// 创建文档
public static void createDoc(RestHighLevelClient client) throws IOException {
// 新增文档 - 请求对象
IndexRequest request = new IndexRequest();
// 设置索引及唯一性标识
request.index("user").id("1001");
// 创建数据对象
User user = new User();
user.setAge(26);
user.setSex("男");
user.setName("jak");
ObjectMapper objectMapper = new ObjectMapper();
String productJson = objectMapper.writeValueAsString(user);
// 添加文档数据, 数据格式为Json格式
request.source(productJson, XContentType.JSON);
// 客户端发送请求,获取响应对象
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
// 打印结果信息
System.out.println("_index: " + response.getIndex());
System.out.println("id: " + response.getId());
System.out.println("_result: " + response.getResult());
}
// 修改文档
public static void updateDoc(RestHighLevelClient client) throws IOException {
// 修改文档 - 请求对象
UpdateRequest request = new UpdateRequest();
// 配置修改参数
request.index("user").id("1001");
// 设置请求体,对数据进行修改
request.doc(XContentType.JSON, "sex", "女");
// 客户端发送请求,获取响应对象
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
System.out.println("_index: " + response.getIndex());
System.out.println("_id: " + response.getId());
System.out.println("_result: " + response.getResult());
}
// 查询文档
public static void getDoc(RestHighLevelClient client) throws IOException {
// 创建请求对象
GetRequest request = new GetRequest().index("user").id("1001");
// 客户端发送请求,获取响应对象
GetResponse response = client.get(request, RequestOptions.DEFAULT);
// 打印结果信息
System.out.println("_index: " + response.getIndex());
System.out.println("_type: " + response.getType());
System.out.println("_id: " + response.getId());
System.out.println("source: " + response.getSourceAsString());
}
// 删除文档
public static void deleteDoc(RestHighLevelClient client) throws IOException {
// 创建请求对象
DeleteRequest request = new DeleteRequest().index("user").id("1");
// 客户端发送请求,获取响应对象
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
// 打印信息
System.out.println(response.toString());
}
// 批量新增
public static void bulkCreateDoc(RestHighLevelClient client) throws IOException {
// 创建批量新增请求对象
BulkRequest request = new BulkRequest();
request.add(new IndexRequest().index("user").id("1001").source(XContentType.JSON, "name", "zhangsan"));
request.add(new IndexRequest().index("user").id("1002").source(XContentType.JSON, "name", "lisi"));
request.add(new IndexRequest().index("user").id("1003").source(XContentType.JSON, "name", "wangwu"));
// 客户端发送请求,获取响应对象
BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
// 打印结果信息
System.out.println("took: " + responses.getTook());
System.out.println("items: " + Arrays.toString(responses.getItems()));
}
// 批量删除
public static void bulkDeleteDoc(RestHighLevelClient client) throws IOException {
// 创建批量删除请求对象
BulkRequest request = new BulkRequest();
request.add(new DeleteRequest().index("user").id("1001"));
request.add(new DeleteRequest().index("user").id("1002"));
request.add(new DeleteRequest().index("user").id("1003"));
// 客户端发送请求,获取响应对象
BulkResponse responses = client.bulk(request, RequestOptions.DEFAULT);
// 打印结果信息
System.out.println("took: " + responses.getTook());
System.out.println("items: " + Arrays.toString(responses.getItems()));
}
}
public class EsSearch {
public static void main(String[] args) throws IOException {
// 创建客户端对象
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1", 9200, "http"))
);
// 关闭客户端连接
client.close();
}
// 查询所有索引数据
public static void matchAllQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询所有数据
sourceBuilder.query(QueryBuilders.matchAllQuery());
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// term查询,查询条件为关键字
public static void termQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询所有数据
sourceBuilder.query(QueryBuilders.termQuery("age", "30"));
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// 分页查询
public static void pageQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询所有数据
sourceBuilder.query(QueryBuilders.matchAllQuery());
// 分页查询
// 当前页起始索引(第一条数据的顺序号),from
sourceBuilder.from(0);
// 每页显示多少条size
sourceBuilder.size(2);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// 数据排序
public static void sortOrderQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询所有数据
sourceBuilder.query(QueryBuilders.matchAllQuery());
// 排序
sourceBuilder.sort("age", SortOrder.ASC);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// 过滤字段
public static void filterFieldsQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询所有数据
sourceBuilder.query(QueryBuilders.matchAllQuery());
// 查询字段过滤
String[] excludes = {};
String[] includes = {"name", "age"};
sourceBuilder.fetchSource(includes, excludes);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// Bool查询
public static void boolQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
// 必须包含
boolQueryBuilder.must(QueryBuilders.matchQuery("age", "30"));
// 一定不含
boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name", "zhangsan"));
// 可能包含
boolQueryBuilder.should(QueryBuilders.matchQuery("sex", "男"));
// 查询所有数据
sourceBuilder.query(boolQueryBuilder);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// 范围查询
public static void rangeQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
// 大于等于
rangeQuery.gte("30");
// 小于等于
rangeQuery.lte("40");
// 查询所有数据
sourceBuilder.query(rangeQuery);
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// 范围查询
public static void fuzzyQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 查询所有数据
sourceBuilder.query(QueryBuilders.fuzzyQuery("name", "zhangsan").fuzziness(Fuzziness.ONE));
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// 高亮查询
public static void highLightQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// 构建查询方式: 高亮查询
TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("name", "zhangsan");
// 设置查询方式
sourceBuilder.query(termsQueryBuilder);
// 构建高亮字段
HighlightBuilder highlightBuilder = new HighlightBuilder();
// 设置标签前缀
highlightBuilder.preTags("");
// 设置标签后缀
highlightBuilder.postTags("");
// 设置高亮字段
highlightBuilder.field("name");
// 设置高亮构建对象
sourceBuilder.highlighter(highlightBuilder);
// 设置请求体
request.source(sourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// 聚合查询
public static void AggrQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.aggregation(AggregationBuilders.max("maxAge").field("age"));
// 设置请求体
request.source(sourceBuilder);
// 客户端发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
// 分组统计
public static void groupQuery(RestHighLevelClient client) throws IOException {
// 创建搜索请求对象
SearchRequest request = new SearchRequest();
request.indices("student");
// 构建查询的请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.aggregation(AggregationBuilders.terms("age_group_by").field("age"));
// 设置请求体
request.source(sourceBuilder);
// 客户端发送请求,获取响应对象
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 查询匹配
SearchHits hits = response.getHits();
System.out.println("took: " + response.getTook());
System.out.println("timeout: " + response.isTimedOut());
System.out.println("total: " + hits.getTotalHits());
System.out.println("MaxScore: " + hits.getMaxScore());
System.out.println("hits------------->");
for (SearchHit hit : hits) {
// 输出每条查询的结果信息
System.out.println(hit.getSourceAsString());
}
System.out.println("<-----------------");
}
}
Spring Data是一个用于简化数据库、非关系型数据库、索引库访问,并支持云服务的开源框架。 其主要目标是使得对数据的访问变得方便快捷,并支持 map reduce 框架和云计算数据服务。 Spring Data 可以极大的简化 JPA Elasticsearch …)的写法,可以在几乎不用写实现的情况下,实现对数据的访问和操作。 除了 CRUD 外,还包括如分页、排序等一些常用的功能。官网地址:https://spring.io/projects/spring-data
Spring Data Elasticsearch基于 spring data API 简化 Elasticsearch 操作,将原始操作Elasticsearch 的客户端 API 进行封装 。 Spring Data 为 Elasticsearch 项目提供集成搜索引擎。 Spring Data Elasticsearch POJO 的关键功能区域为中心的模型与 Elastichsearch 交互文档和轻松地编写一个存储索引库数据访问层。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.3.6.RELEASE
com.best
best-es
0.0.1-SNAPSHOT
best-es
Demo project for Spring Boot
8
8
org.projectlombok
lombok
1.16.12
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-data-elasticsearch
org.springframework.boot
spring-boot-devtools
runtime
true
org.springframework.boot
spring-boot-starter-test
test
org.elasticsearch
elasticsearch
7.8.0
org.elasticsearch.client
elasticsearch-rest-high-level-client
7.8.0
org.apache.logging.log4j
log4j-api
2.8.2
org.apache.logging.log4j
log4j-core
2.8.2
com.fasterxml.jackson.core
jackson-databind
2.9.9
junit
junit
4.12
org.springframework.boot
spring-boot-test
org.springframework
spring-test
org.springframework.boot
spring-boot-maven-plugin
# 端口号
server:
port: 8080
# es服务地址与端口
elasticsearch:
host: 127.0.0.1
port: 9200
# 配置日志级别,开启debug日志
logging:
level:
com:
best: debug
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document(indexName = "shopping", shards = 3, replicas = 1)
public class Product {
// 商品唯一标识, 必须有id, 这里的id 是全局唯一的标识,等同于es中的"_id"
@Id
private Long id;
/**
* type: 字段数据类型
* analyzer: 分词器类型
* index: 是否索引(默认:true)
* Keyword: 短语, 不进行分词
*/
// 商品名称
@Field(type = FieldType.Text, analyzer = "ik_max_word")
private String title;
// 分类名称
@Field(type = FieldType.Keyword)
private String category;
// 商品价格
@Field(type = FieldType.Double)
private Double price;
// 图片地址
@Field(type = FieldType.Keyword, index = false)
private String images;
}
ElasticsearchRestTemplate是spring-data-elasticsearch项目中的一个类,和其他spring项目中的 template类似。在新版的spring-data-elasticsearch 中,ElasticsearchRestTemplate 代替了原来的ElasticsearchTemplate。原因是ElasticsearchTemplate基于TransportClient,TransportClient即将在8.x 以后的版本中移除。
我们推荐使用ElasticsearchRestTemplate。ElasticsearchRestTemplate基于RestHighLevelClient客户端的。需要自定义配置类,继承AbstractElasticsearchConfiguration,并实现elasticsearchClient()抽象方法,创建RestHighLevelClient对象。
AbstractElasticsearchConfiguration源码:
public abstract class AbstractElasticsearchConfiguration extends ElasticsearchConfigurationSupport {
//需重写本方法
public abstract RestHighLevelClient elasticsearchClient();
@Bean(name = { "elasticsearchOperations", "elasticsearchTemplate" })
public ElasticsearchOperations elasticsearchOperations(ElasticsearchConverter elasticsearchConverter) {
return new ElasticsearchRestTemplate(elasticsearchClient(), elasticsearchConverter);
}
}
需要自定义配置类,继承AbstractElasticsearchConfiguration,并实现elasticsearchClient()抽象方法,创建RestHighLevelClient对象。
@Data
@Configuration
@ConfigurationProperties(prefix = "elasticsearch")
public class ElasticsearchConfig extends AbstractElasticsearchConfiguration {
private String host;
private Integer port;
@Override
public RestHighLevelClient elasticsearchClient() {
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port));
return new RestHighLevelClient(builder);
}
}
@Repository
public interface ProductDao extends ElasticsearchRepository {
}
@SpringBootTest
class BestEsApplicationTests {
@Autowired
private ElasticsearchRestTemplate elasticsearchRestTemplate;
@Test
public void creatIndex() {
// 创建索引并增加映射配置
// 创建索引,系统初始化会自动创建索引
System.out.println("创建索引");
}
@Test
public void deleteIndex() {
boolean flag = elasticsearchRestTemplate.deleteIndex(Product.class);
System.out.println("删除索引: " + flag);
}
}
@SpringBootTest
public class SpringDataEsProductDaoTest {
@Autowired
private ProductDao productDao;
/**
* 新增
*/
@Test
public void save(){
Product product = new Product();
product.setId(2L);
product.setTitle("华为手机");
product.setCategory("手机");
product.setPrice(2999.0);
product.setImages("http://www.test/hw.jpg");
productDao.save(product);
}
//POSTMAN, GET http://localhost:9200/shopping/_doc/2
//修改
@Test
public void update(){
Product product = new Product();
product.setId(2L);
product.setTitle("小米 2 手机");
product.setCategory("手机");
product.setPrice(9999.0);
product.setImages("http://www.test/xm.jpg");
productDao.save(product);
}
//POSTMAN, GET http://localhost:9200/shopping/_doc/2
//根据 id 查询
@Test
public void findById(){
Product product = productDao.findById(2L).get();
System.out.println(product);
}
@Test
public void findAll(){
Iterable products = productDao.findAll();
for (Product product : products) {
System.out.println(product);
}
}
//删除
@Test
public void delete(){
Product product = new Product();
product.setId(2L);
productDao.delete(product);
}
//POSTMAN, GET http://localhost:9200/shopping/_doc/2
//批量新增
@Test
public void saveAll(){
List productList = new ArrayList<>();
for (int i = 0; i < 10; i++) {
Product product = new Product();
product.setId(Long.valueOf(i));
product.setTitle("["+i+"]小米手机");
product.setCategory("手机");
product.setPrice(1999.0 + i);
product.setImages("http://www.test/xm.jpg");
productList.add(product);
}
productDao.saveAll(productList);
}
//分页查询
@Test
public void findByPageable(){
//设置排序(排序方式,正序还是倒序,排序的 id)
Sort sort = Sort.by(Sort.Direction.DESC,"id");
int currentPage=0;//当前页,第一页从 0 开始, 1 表示第二页
int pageSize = 5;//每页显示多少条
//设置查询分页
PageRequest pageRequest = PageRequest.of(currentPage, pageSize,sort);
//分页查询
Page productPage = productDao.findAll(pageRequest);
for (Product Product : productPage.getContent()) {
System.out.println(Product);
}
}
}
@SpringBootTest
public class SpringDataEsSearchTest {
@Autowired
private ProductDao productDao;
/**
* term 查询
* search(termQueryBuilder) 调用搜索方法,参数查询构建器对象
*/
@Test
public void termQuery(){
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("title", "小米");
Iterable products = productDao.search(termQueryBuilder);
for (Product product : products) {
System.out.println(product);
}
}
/**
* term 查询加分页
*/
@Test
public void termQueryByPage(){
int currentPage= 0 ;
int pageSize = 5;
//设置查询分页
PageRequest pageRequest = PageRequest.of(currentPage, pageSize);
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("category", "手机");
Iterable products =
productDao.search(termQueryBuilder,pageRequest);
for (Product product : products) {
System.out.println(product);
}
}
}
参考原文地址:
pom文件引入依赖:
com.bbossgroups.plugins
bboss-elasticsearch-rest-jdbc
6.1.8
com.bbossgroups.plugins
bboss-elasticsearch-spring-boot-starter
6.1.8
application.properties配置:
##ES集群配置,支持x-pack和searchguard
#spring.elasticsearch.bboss.elasticUser=elastic
#spring.elasticsearch.bboss.elasticPassword=changeme
spring.elasticsearch.bboss.elasticsearch.rest.hostNames=192.168.57.128:9200
#spring.elasticsearch.bboss.elasticsearch.rest.hostNames=10.180.211.27:9280,10.180.211.27:9281,10.180.211.27:9282
##https配置,添加https://协议头
#spring.elasticsearch.bboss.default.elasticsearch.rest.hostNames=https://10.180.211.27:9280,https://10.180.211.27:9281,https://10.180.211.27:9282
spring.elasticsearch.bboss.elasticsearch.dateFormat=yyyy.MM.dd
spring.elasticsearch.bboss.elasticsearch.timeZone=Asia/Shanghai
spring.elasticsearch.bboss.elasticsearch.ttl=2d
#在控制台输出脚本调试开关showTemplate,false关闭,true打开,同时log4j至少是info级别
spring.elasticsearch.bboss.elasticsearch.showTemplate=true
spring.elasticsearch.bboss.elasticsearch.discoverHost=false
# dsl配置文件热加载扫描时间间隔,毫秒为单位,默认5秒扫描一次,<= 0时关闭扫描机制
spring.elasticsearch.bboss.dslfile.refreshInterval = -1
##es client http连接池配置
spring.elasticsearch.bboss.http.timeoutConnection = 50000
spring.elasticsearch.bboss.http.timeoutSocket = 50000
spring.elasticsearch.bboss.http.connectionRequestTimeout=50000
spring.elasticsearch.bboss.http.retryTime = 1
spring.elasticsearch.bboss.http.maxLineLength = -1
spring.elasticsearch.bboss.http.maxHeaderCount = 200
spring.elasticsearch.bboss.http.maxTotal = 400
spring.elasticsearch.bboss.http.defaultMaxPerRoute = 200
spring.elasticsearch.bboss.http.soReuseAddress = false
spring.elasticsearch.bboss.http.soKeepAlive = false
spring.elasticsearch.bboss.http.timeToLive = 3600000
spring.elasticsearch.bboss.http.keepAlive = 3600000
spring.elasticsearch.bboss.http.keystore =
spring.elasticsearch.bboss.http.keyPassword =
# ssl 主机名称校验,是否采用default配置,
# 如果指定为default,就采用DefaultHostnameVerifier,否则采用 SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER
spring.elasticsearch.bboss.http.hostnameVerifier =
1. getConfigRestClientUtil获取在配置文件中根据DSL名称定义的DSL并执行它:
写入的params参数在xml文件中通过 #[参数名] 来自动注入到 DSL中
ClientInterface clientUtil = ElasticSearchHelper.getConfigRestClientUtil("esmapper/sql.xml");//define an instanceof ConfigRestClientUtil,It's single instance, multithreaded secure.
Map params = new HashMap();
params.put("channelId",1);
List
2. getRestClientUtil直接执行代码中定义的DSL:
ClientInterface clientUtil = ElasticSearchHelper.getRestClientUtil();//define an instanceof RestClientUtil,It's single instance, multithreaded secure.
List
1.普通创建
@SpringBootTest
class EsBbossTest {
@Autowired
private BBossESStarter bbossESStarter;
/**
*创建索引
*/
@Test
public void createIndex(){
String result = bbossESStarter.getRestClient().createIndiceMapping("blog","");
System.out.println(result);
}
}
2.自定义mapping
先创建一个dsl脚本文件,在里面自定义mapping规则:
//每个property中的name属性表示当前mapping的命名,一个xml文件里可设置多个dsl脚本
然后创建客户端,并且调用创建索引api:
@SpringBootTest
class EsBbossTest {
@Autowired
private BBossESStarter bbossESStarter;
/**
*创建索引
*/
@Test
public void createIndex(){
ClientInterface clientUtil = bbossESStarter.getConfigRestClient("esmapper/job.xml");
String result = clientUtil.createIndiceMapping("51job","create51jobIndex");
System.out.println(result);
}
}
@SpringBootTest
class EsBbossTest {
@Autowired
private BBossESStarter bbossESStarter;
/**
*删除索引
*/
@Test
public void dropIndex(){
String result = bbossESStarter.getRestClient().dropIndice("blog");
System.out.println(result);
}
}
@SpringBootTest
class EsBbossTest {
@Autowired
private BBossESStarter bbossESStarter;
/**
*判断索引是否存在
*/
@Test
public void indexIsExists(){
boolean exist = bbossESStarter.getRestClient().existIndice("51job");
System.out.println(exist);
}
}
@SpringBootTest
class EsBbossTest {
@Autowired
private BBossESStarter bbossESStarter;
/**
*判断索引类型是否存在
*/
@Test
public void typeIsExists(){
boolean exist = bbossESStarter.getRestClient().existIndiceType("51job","_doc");
System.out.println(exist);
}
}
@SpringBootTest
class EsBbossTest {
@Autowired
private BBossESStarter bbossESStarter;
/**
*指定索引添加单个文档
*/
@Test
public void addDocument(){
Qcpage qcpage=new Qcpage();
qcpage.setCompany("xxxxxx有限公司").setData("2022-11-22 21:18:12").setJob("Java开发工程师").setPlace("深圳南山").setSalar("13000/月");
String result = bbossESStarter.getRestClient().addDocument("51job",qcpage);
System.out.println(result);
}
}
@SpringBootTest
class EsBbossTest {
@Autowired
private BBossESStarter bbossESStarter;
@Autowired
QcpageService qcpageService;
/**
*批量添加文档(此处从mysql取出来1300条数据测试)
*/
@Test
public void batchAddDocument(){
List list=qcpageService.list();
long startTime = System.currentTimeMillis();
String result = bbossESStarter.getRestClient().addDocuments("51job",list);
long endTime = System.currentTimeMillis();
System.out.println("添加"+list.size()+"条数据耗时:" + (endTime - startTime)/1000 + "s");
System.out.println(result);
}
}
@SpringBootTest
class EsBbossTest {
@Autowired
private BBossESStarter bbossESStarter;
/**
*查询一个文档
*/
@Test
public void getDocument(){
String result = bbossESStarter.getRestClient().getDocument("51job","7pJsGXMBBreT5daW4X1w");
System.out.println(result);
}
}
参考原文地址:http://t.csdn.cn/hm0y7