package com.bwfw.ppq;
import com.bwfw.ppq.common.web.ResultJson;
import com.bwfw.ppq.util.ElasticSearchUtils;
import org.apache.ibatis.annotations.Update;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.IOException;
import java.time.LocalDateTime;
/**
* @ProjectName: ppq
* @Package: com.bwfw.ppq
* @ClassName: RestHighLevClientTests
* @Author: zxh
* @Description:
* @Date: 2022/1/4 20:33
* @Version: 1.0
*/
public class RestHighLevClientTests extends PpqApplicationTests {
private final RestHighLevelClient restHighLevelClient;
@Autowired
public RestHighLevClientTests(RestHighLevelClient restHighLevelClient) {
this.restHighLevelClient = restHighLevelClient;
}
/*
* 用于测试:创建索引&&创建映射
* */
@Test
public void test1() throws IOException {
CreateIndexRequest createIndexRequest = new CreateIndexRequest("moments");
createIndexRequest.mapping("{\n" +
" \"properties\":{\n" +
" \"userId\":{\n" +
" \"type\":\"integer\"\n" +
" },\n" +
" \"content\":{\n" +
" \"type\":\"text\",\n" +
" \"analyzer\":\"ik_max_word\"\n" +
" },\n" +
" \"createTime\":{\n" +
" \"type\":\"keyword\"\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON);
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
System.out.println(createIndexResponse.isAcknowledged());
restHighLevelClient.close();
}
/*
* 用于测试:删除索引
* */
@Test
public void test2() throws IOException {
AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(new DeleteIndexRequest("moments"), RequestOptions.DEFAULT);
System.out.println("acknowledgedResponse = " + acknowledgedResponse.isAcknowledged());
}
/*
* 用于测试:插入一条文档
* */
@Test
public void test3() throws IOException {
IndexRequest indexRequest = new IndexRequest("moments");
LocalDateTime now = LocalDateTime.now();
indexRequest
.id("3")//手动指定文档id
.source("{\"userId\":"+ 1 +",\"content\":\"内容\",\"createTime\":\""+ now.toString() +"\"}", XContentType.JSON);
//索引的请求对象 请求的配置对象
IndexResponse index = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.toString());
}
/*
* 用于测试:更新文档
* */
@Test
public void test4() throws IOException {
UpdateRequest products = new UpdateRequest("products", "1");
products.doc("{\n" +
" \"title\":\"小浣熊干脆面\",\n" +
" \"price\":1.99\n" +
" }",XContentType.JSON);
//参数1 更新的请求对象 参数2 请求配对对象
UpdateResponse update = restHighLevelClient.update(products, RequestOptions.DEFAULT);
System.out.println(update.status());
}
/*
* 用于测试:删除文档
* */
@Test
public void test5() throws IOException {
DeleteResponse products = restHighLevelClient.delete(new DeleteRequest("products", "1"), RequestOptions.DEFAULT);
System.out.println("products = " + products.toString());
}
/*
* 用于测试:基于id查询文档
* */
@Test
public void test6() throws IOException {
GetRequest getRequest = new GetRequest("products","1");
//请求对象
GetResponse documentFields = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
System.out.println("documentFields.getSource() = " + documentFields.getSource());
}
/*
* 用于测试:查询所有
* */
@Test
public void test7() throws IOException {
/*指定搜索的索引*/
SearchRequest searchRequest = new SearchRequest("moments");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());//查询所有
searchRequest.source(sourceBuilder);/*指定查询条件*/
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总条数 = " + search.getHits().getTotalHits().value);
System.out.println("最大得分 = "+search.getHits().getMaxScore());
SearchHit[] hits = search.getHits().getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
String sourceAsString = hit.getSourceAsString();
System.out.println("id = "+id+" source = "+sourceAsString);
}
}
/*
* 用于测试:不同条件的查询 term(关键词查询)
* */
@Test
public void test8() throws IOException {
SearchRequest searchRequest = new SearchRequest("products");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.termQuery("description","浣熊"));
searchRequest.source(sourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总条数 = " + search.getHits().getTotalHits().value);
SearchHit[] hits = search.getHits().getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
String sourceAsString = hit.getSourceAsString();
System.out.println("id = "+id+" source = "+sourceAsString);
}
}
/*
* 用于测试:不同条件的查询 : range 范围查询
* */
@Test
public void test9() throws IOException {
query(QueryBuilders.rangeQuery("price").gt(0).lt(3));
}
/*
* 用于测试:不同条件的查询:prefix 前缀查询
* */
@Test
public void test10() throws IOException {
query(QueryBuilders.prefixQuery("title","大"));
}
/*
* 用于测试:不同条件的查询: wildcard 通配符查询
* */
@Test
public void test11() throws IOException {
query(QueryBuilders.wildcardQuery("title","*熊"));
}
/*
* 用于测试:多个指定的id查询
* */
@Test
public void test12() throws IOException {
query(QueryBuilders.idsQuery().addIds("1").addIds("2"));
}
/*
* 用于测试:多字段查询 multi_match
* */
@Test
public void test13() throws IOException {
query(QueryBuilders.multiMatchQuery("熊","title","description"));
}
/*
* 用于测试:分页查询
* */
@Test
public void test14(){
}
public void query(QueryBuilder queryBuilder) throws IOException {
SearchRequest searchRequest = new SearchRequest("products");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(queryBuilder);
searchRequest.source(sourceBuilder);
SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
System.out.println("总条数 = " + search.getHits().getTotalHits().value);
SearchHit[] hits = search.getHits().getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
String sourceAsString = hit.getSourceAsString();
System.out.println("id = "+id+" source = "+sourceAsString);
}
}
}
ElasticSearch笔记-03 SpringBoot整合
最新推荐文章于 2024-11-06 11:47:40 发布