package com.zanglikun;
import com.alibaba.fastjson.JSON;
import com.zanglikun.domain.Persion;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
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.support.master.AcknowledgedResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.xcontent.XContentType;
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.HashMap;
import java.util.Map;
import java.util.Set;
@SpringBootTest
public class RunAppTest {
@Autowired
private RestHighLevelClient client;
@Test
void contextLoads(){
/* RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(
new HttpHost("192.168.3.10",9200,"http")
));*/
System.out.println(client);
}
/**
* 添加索引
* @throws IOException
*/
@Test
void addIndex() throws IOException {
// 使用clent获取操作索引的对象
IndicesClient indices = client.indices();
// 指定索引名称
CreateIndexRequest createRequest = new CreateIndexRequest("itheima");
// 创建索引
CreateIndexResponse response = indices.create(createRequest, RequestOptions.DEFAULT);
// 根据返回值 判断结果。
System.out.println(response.isAcknowledged());
}
/**
* 添加索引的时候,加上映射
* @throws IOException
*/
@Test
void addIndexWithMapping() throws IOException {
// 使用clent获取操作索引的对象
IndicesClient indices = client.indices();
// 指定索引名称
CreateIndexRequest createRequest = new CreateIndexRequest("persion2");
// 制作 Mapping 内容
String mapping = "{\n" +
" \"properties\": {\n" +
" \"name\":{\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" },\n" +
" \"age\":{\n" +
" \"type\": \"integer\"\n" +
" },\n" +
" \"addtess\": {\n" +
" \"type\": \"text\"}\n" +
" }\n" +
" }";
// 绑定 Mapping 指定Json格式 与数据
createRequest.mapping("_doc",mapping, XContentType.JSON);
// 创建索引
CreateIndexResponse response = indices.create(createRequest, RequestOptions.DEFAULT);
// 根据返回值 判断结果。
System.out.println(response.isAcknowledged());
}
/**
* 查询索引结构
* @throws IOException
*/
@Test
void queryIndex() throws IOException {
// 使用clent获取操作索引的对象
IndicesClient indices = client.indices();
// 指定查询的索引 信息 persion2
GetIndexRequest getIndexRequest = new GetIndexRequest("persion2");
// 开始查询
GetIndexResponse response = indices.get(getIndexRequest, RequestOptions.DEFAULT);
// 获取 查询中的 Mapping信息
Map mappings = response.getMappings();
// 遍历它
Set strings = mappings.keySet();
for (String string : strings) {
System.out.println(string + " " +mappings.get(string).getSourceAsMap() );
}
}
/**
* 删除索引(数据库)
* @throws IOException
*/
@Test
void deleteIndex() throws IOException {
// 使用clent获取操作索引的对象
IndicesClient indices = client.indices();
// 指定删除的索引 信息 persion2
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("persion2");
// 删除数据
AcknowledgedResponse delete = indices.delete(deleteIndexRequest, RequestOptions.DEFAULT);
// 输出 删除 结果
System.out.println(delete.isAcknowledged());
}
/**
* 判断索引是否存在
* @throws IOException
*/
@Test
void existIndex() throws IOException {
// 使用clent获取操作索引的对象
IndicesClient indices = client.indices();
// 指定判断是否存在的索引 信息 persion2
GetIndexRequest getIndexRequest = new GetIndexRequest("persion2");
// 拿到 结果
boolean exists = indices.exists(getIndexRequest, RequestOptions.DEFAULT);
// 输出 结果
System.out.println(exists);
}
/**
* 添加一个文档
* @throws IOException
*/
@Test
void addDoc() throws IOException {
// 数据对象
Map data = new HashMap<>();
data.put("address","北京海淀");
data.put("age","20");
data.put("name","李四");
// 创建文档操作对象
IndexRequest indexRequest = new IndexRequest("persion2").id("1").source(data);
// 使用clent 添加文档
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
// 打印相应结果
System.out.println(response.toString());
}
/**
* 添加一个Java对象文档
* @throws IOException
*/
@Test
void addDocWithJAVAObject() throws IOException {
// 数据对象
Persion persion = new Persion();
persion.setName("小王八蛋");
persion.setAddress("北京朝阳区");
persion.setAge(18);
persion.setId("2");
// 使用FastJson 将对象转位Json
String data = JSON.toJSONString(persion);
// 创建文档操作对象
IndexRequest indexRequest = new IndexRequest("persion2").id(persion.getId()).source(data,XContentType.JSON);
// 使用clent 添加文档
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
// 打印相应结果
System.out.println(response.toString());
}
/**
* 更新文档 (没有就是插入,有就更新)
* @throws IOException
*/
@Test
void updateDocWithJAVAObject() throws IOException {
// 数据对象
Persion persion = new Persion();
persion.setName("小王八蛋");
persion.setAddress("北京朝阳区");
// 将ID 变成 88
persion.setAge(88);
persion.setId("2");
// 使用FastJson 将对象转位Json
String data = JSON.toJSONString(persion);
// 创建文档操作对象
IndexRequest indexRequest = new IndexRequest("persion2").id(persion.getId()).source(data,XContentType.JSON);
// 使用clent 添加文档对象
IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
// 打印相应结果
System.out.println(response.toString());
}
/**
* 查询文档 指定 索引(数据库) id
* @throws IOException
*/
@Test
void selectDocWithJAVAObject() throws IOException {
// 创建 将要查询的文档数据 GetRequest
GetRequest getRquest = new GetRequest("persion2","2");
// 通过 client 进行查询 GetRequest
GetResponse response = client.get(getRquest, RequestOptions.DEFAULT);
// 获取结果 并输出
System.out.println(response.getSourceAsString());
}
/**
* 删除 文档
* @throws IOException
*/
@Test
void deleteDocWithJAVAObject() throws IOException {
// 创建 将要查询的文档数据 GetRequest
// 通过 client 进行查询 GetRequest ID 不添加可能会报错。
DeleteRequest deleteRquest = new DeleteRequest("persion2","2");
DeleteResponse response = client.delete(deleteRquest, RequestOptions.DEFAULT);
// 因为删除的时候会返回我们输入ID,切记,相应ID了 不代表删除成功,应该以 下面的status为准 我怀疑 status是 Http的响应码
System.out.println("删除的ID是"+response.getId());
System.out.println(response.status()); //OK 或者 NOT_FOUND
}
}