package com.dd.cc;
import com.alibaba.fastjson.JSON;
import com.dd.cc.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.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
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 org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
@SpringBootTest
class CcApplicationTests {
@Autowired
private RestHighLevelClient restHighLevelClient;
/**
* 创建索引 (等于mysql创建table)
*/
@Test
public void createIndex(){
//创建索引
CreateIndexRequest request = new CreateIndexRequest("rwj");
try {
//通过客户端发送请求
restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取索引
*/
@Test
public void getIndex(){
GetIndexRequest request = new GetIndexRequest("test");
try {
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(exists);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 删除索引
*/
@Test
public void deleteIndex(){
DeleteIndexRequest test = new DeleteIndexRequest("test");
try {
restHighLevelClient.indices().delete(test,RequestOptions.DEFAULT);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 从一个索引里添加数据
*/
@Test
public void getText(){
User user = new User("dddd","aaaa");
IndexRequest request = new IndexRequest("rwj");
request.id("1");
request.source(JSON.toJSONString(user), XContentType.JSON);
try {
IndexResponse index = restHighLevelClient.index(request, RequestOptions.DEFAULT);
System.out.println(index.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 判断文档里是否包含某条数据
*/
@Test
public void isExists(){
GetRequest rwj = new GetRequest("rwj", "1");
try {
boolean exists = restHighLevelClient.exists(rwj, RequestOptions.DEFAULT);
System.out.println(exists);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取文档信息
*/
@Test
public void getDocument(){
GetRequest rwj = new GetRequest("rwj", "1");
try {
GetResponse response = restHighLevelClient.get(rwj, RequestOptions.DEFAULT);
Map<String, Object> source = response.getSource();
System.out.println(source.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 更新文档记录
*/
@Test
public void updateText(){
UpdateRequest rwj = new UpdateRequest("rwj", "1");
User user = new User("rwjnb"