package com.example.elasticsearchdemo;
import com.alibaba.fastjson.JSON;
import com.example.elasticsearchdemo.pojo.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
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.ElasticsearchClient;
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.client.indices.GetIndexRequest;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@SpringBootTest
class ElasticsearchdemoApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
@Test
void TestcreateIndex() throws IOException {
CreateIndexRequest request=new CreateIndexRequest(“fgztest”);
CreateIndexResponse response=restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response);
}
@Test
void TestExistIndex() throws IOException {
GetIndexRequest request=new GetIndexRequest(“fgztest”);
boolean exist=restHighLevelClient.indices().exists(request,RequestOptions.DEFAULT);
System.out.println(exist);
}
@Test
void TestdeleteIndex() throws IOException {
DeleteIndexRequest request=new DeleteIndexRequest(“fgztest”);
AcknowledgedResponse delete = restHighLevelClient.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
@Test
void TestAddDocument() throws IOException{
User user=new User(“fgz”,22);
IndexRequest request=new IndexRequest(“fgztest”);
request.id(“1”);
//request.timeout(TimeValue.timeValueSeconds(1));
request.timeout(“1s”);
request.source(JSON.toJSONString(user), XContentType.JSON);
IndexResponse indexResponse=restHighLevelClient.index(request,RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
System.out.println(indexResponse.status());
}
@Test
void testIndexExist() throws IOException{
GetRequest getRequest=new GetRequest(“fgztest”,“1”);
getRequest.fetchSourceContext(new FetchSourceContext(true));//过滤
getRequest.storedFields(“none”);//过滤
boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);
System.out.println(exists);
}
@Test
void testGetDocument() throws IOException{
GetRequest getRequest=new GetRequest(“fgztest”,“1”);
GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
String source = getResponse.getSourceAsString();
System.out.println(source);
}
@Test
void testUpdateRequest() throws IOException{
UpdateRequest updateRequest=new UpdateRequest(“fgztest”,“1”);
updateRequest.timeout(“1s”);
User user=new User(“龙”,18);
updateRequest.doc(JSON.toJSONString(user),XContentType.JSON);
UpdateResponse updateResponse=restHighLevelClient.update(updateRequest,RequestOptions.DEFAULT);
System.out.println(updateResponse);
}
@Test
void testDeleteRequest() throws IOException{
DeleteRequest deleteRequest=new DeleteRequest(“fgztest”,“1”);
deleteRequest.timeout(“1s”);
DeleteResponse delete = restHighLevelClient.delete(deleteRequest,RequestOptions.DEFAULT);
System.out.println(delete);
}
@Test
void testBulkRequst() throws IOException{
BulkRequest bulkRequest=new BulkRequest();
bulkRequest.timeout(“10s”);
ArrayList userlist=new ArrayList<>();
userlist.add(new User(“狂神1”,3));
userlist.add(new User(“狂神2”,3));
userlist.add(new User(“狂神3”,3));
userlist.add(new User(“狂神4”,3));
for (int i=0;i
.id(""+(i+1))
.source(JSON.toJSONString(userlist.get(i)),XContentType.JSON));
}
BulkResponse bulkResponse=restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
System.out.println(bulkResponse.hasFailures());
}
@Test
void testSearch() throws IOException{
SearchRequest searchRequest=new SearchRequest(“fgztest”);
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
//全文匹配
MatchQueryBuilder termQueryBuilder=new MatchQueryBuilder(“name”,“狂神”);
sourceBuilder.query(termQueryBuilder);
String s[]={“name”,“price”};
FetchSourceContext fetchSourceContext=new FetchSourceContext(true,s,null);
sourceBuilder.fetchSource(fetchSourceContext);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
searchRequest.source(sourceBuilder);
SearchResponse response=restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response.getHits()));
for (SearchHit Field:response.getHits().getHits()){
System.out.println(Field.getSourceAsMap());
}
}
//高亮分页查询
@Test
void testSearchhightandpage() throws IOException{
SearchRequest searchRequest=new SearchRequest(“fgztest”);
SearchSourceBuilder sourceBuilder=new SearchSourceBuilder();
//分页
sourceBuilder.from(1);
sourceBuilder.size(2);
//全文匹配
MatchQueryBuilder termQueryBuilder=new MatchQueryBuilder(“name”,“狂神”);
sourceBuilder.query(termQueryBuilder);
String s[]={“name”,“price”};
FetchSourceContext fetchSourceContext=new FetchSourceContext(true,s,null);
sourceBuilder.fetchSource(fetchSourceContext);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
//高亮
HighlightBuilder highlightBuilder=new HighlightBuilder();
highlightBuilder.field("name");
highlightBuilder.preTags("");
highlightBuilder.postTags("");
sourceBuilder.highlighter(highlightBuilder);
searchRequest.source(sourceBuilder);
SearchResponse response=restHighLevelClient.search(searchRequest,RequestOptions.DEFAULT);
System.out.println(JSON.toJSONString(response.getHits()));
ArrayList
}
高亮客户端:
v-html修饰