在咱们写创建索引,删除索引之前需要在咱们测试类中提前做好准备,第一是引入咱们的客户端(client),每次执行完都要将client释放资源,所以咱们可以用junit的After注解,在执行完方法后释放资源.over 废话不多说,直接上代码。
准备
引入客户端(client)
/**
* 日志记录
*/
private static final Logger logger= LoggerFactory.getLogger(ElasticsearchTest.class);
@Autowired
private RestHighLevelClient restHighLevelClient;
@After
/*
* 释放资源
*/
@After
public void close(){
try {
restHighLevelClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
over,做好了准备工作,就可以上手啦~
索引
1、是创建索引
/**
* 创建索引测试
*/
@Test
public void createIndex() throws IOException {
//创建索引请求
CreateIndexRequest request = new CreateIndexRequest("test_index");
//相关配置
request.settings(Settings.builder()
//分片数
.put("index.number_of_shards",3)
//备份数
.put("index.number_of_replicas",2)
);
//同步执行
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
logger.info("-------------------test_index created:"+createIndexResponse.isAcknowledged()+"--------------------------------");
}
2、查看索引是否存在
/**
* 判断索引是否存在
*/
@Test
public void exist() throws IOException {
GetIndexRequest request = new GetIndexRequest("test_index");
boolean flag = restHighLevelClient.indices().exists(request,RequestOptions.DEFAULT);
logger.info("-------------test_index exists:"+flag+"----------------------");
}
3、删除索引
/*
* 删除索引
*/
@Test
public void deleteIndex() throws IOException {
DeleteIndexRequest deleteRequest = new DeleteIndexRequest("test_index");
AcknowledgedResponse response = restHighLevelClient.indices().delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println("Delete document successfully! \n" + response.isAcknowledged());
}
弄完了,就可以在kibana中去查看咱们的所有索引啦,进入kibana 查看所有索引
GET _cat/indices
创建文档
1、使用字符串的形式构建文档信息
/**
* 创建文档(1)
*/
@Test
public void createDoc() throws IOException {
//索引
IndexRequest request = new IndexRequest("test_index");
request.id("1");
//使用字符串的形式来构建内容
String jsonStr="{"+"\"user\""+":"+"\"旺仔呀\""+"}";
request.source(jsonStr, XContentType.JSON);
//同步执行
restHighLevelClient.index(request, RequestOptions.DEFAULT);
}
2、map形式构建文档内容
/**
* 创建文档(2)
*/
@Test
public void createDoc2() throws IOException {
//索引
IndexRequest request = new IndexRequest("test_index");
request.id("2");
//使用map来构建内容
Map<String,Object> map=new HashMap<>();
map.put("user","马冬梅呀");
map.put("sex","女");
map.put("age",33);
request.source(map,XContentType.JSON);
//同步执行
restHighLevelClient.index(request,RequestOptions.DEFAULT);
}
3、使用xContentBuilder来构建内容
/**
* 创建文档(3)
*/
@Test
public void createDoc3() throws IOException {
//使用xContentBuilder来构建内容
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
xContentBuilder.startObject();
{
xContentBuilder.field("user","夏洛呀");
xContentBuilder.timeField("birthday",new Date());
xContentBuilder.field("sex","男");
}
xContentBuilder.endObject();
IndexRequest request = new IndexRequest("test_index").id("3").source(xContentBuilder);
//同步执行
restHighLevelClient.index(request,RequestOptions.DEFAULT);
}
4、直接用键值对对象架构数据
/**
* 创建文档(4)
*/
@Test
public void createDoc4() throws IOException {
//直接用键值对对象架构数据
IndexRequest request = new IndexRequest("test_index").id("4").source("user", "袁华", "sex", "男", "age", "18");
//同步执行
restHighLevelClient.index(request,RequestOptions.DEFAULT);
}
创建完索引咱们就可以在咱们的kibana里面去查看咱们的文档啦
查看所有test_index索引下所有的文档
GET test_index/_doc/_search
根据id查找文档
GET test_index/_doc/1