6、使用Java Low Level REST Client操作elasticsearch
1、 检视Index
建立Index时method使用PUT,检视Index时method使用GET
/**
* 检视api资讯
*
* @throws Exception
*/
public static void lookIndex(RestClient client) {
String method = "GET";
Stringendpoint = "/book";
try {
Request request = new Request(method, endpoint);
Response response= client.performRequest(request);
System.out.println(EntityUtils.toString(response.getEntity()));
}catch (Exception e) {
e.printStackTrace();
}
}
执行后结果如下:
{
"book": {
"aliases": { },
"mappings": { },
"settings": {
"index": {
"creation_date": "1561263334053",
"number_of_shards": "5",
"number_of_replicas": "1",
"uuid": "mEeGPmbRR2Cc3Rg6mK4YCA",
"version": {
"created": "5061399"
},
"provided_name": "book"
}
}
}
}
2、 新增文件
构造一个Model物件
package es;
import java.util.Date;
public class ItBook {
private String name;
private String writer;
private int count;
private Date publishDate;
// 省略 get set
}
建立基于该模型的Document
public static void addDocument(RestClient client) {
try{
String method = "PUT";
String endpoint = "/book/it/1"; // 索引:图书【DB】 型别:小说【table】 文件:【表里的资料】
ItBook book = new ItBook();
book.setName("三国演义");
book.setWriter("张飞");
book.setCount(10);
book.setPublishDate(new Date());
String jsonStr = JSON.toJSONString(book);
// JSON格式字串
HttpEntity entity = new NStringEntity(jsonStr, ContentType.APPLICATION_JSON);
Request request = new Request(method, endpoint);
request.setEntity(entity);
Response response = client.performRequest(request);
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("新增文件结束!!!");
}catch (Exception e) {
e.printStackTrace();
}
}
测试返回结果
{
"_index": "book",
"_type": "it",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
3、 查询文件
根据ID查询文件
public static void queryDocument(RestClient client) {
try{
String method = "GET";
String endpoint = "/book/it/1";
Request request = new Request(method, endpoint);
Response response = client.performRequest(request);
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("查询文件结束!!!");
}catch (Exception e) {
e.printStackTrace();
}
}
测试结果
{
"_index": "book",
"_type": "it",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"count": 10,
"name": "三国演义",
"publishDate": 1561471991012,
"writer": "张飞"
}
}
返回的json字串可以使用json类库转换成物件,例如使用fastjson。
查询所有it的文件
public static void queryAll(RestClient client) {
try{
String method = "POST";
String endpoint = "/book/it/_search";
HttpEntity entity = new NStringEntity("{ " +
" "query": { " +
" "match_all": {} " +
" } " +
"}", ContentType.APPLICATION_JSON);
Request request = new Request(method, endpoint);
request.setEntity(entity);
Response response = client.performRequest(request);
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("查询所有资料:queryAll !!!");
}catch (Exception e) {
e.printStackTrace();
}
}
测试结果:
{
"took": 441,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "it",
"_id": "1",
"_score": 1,
"_source": {
"count": 10,
"name": "三国演义",
"publishDate": 1561471991012,
"writer": "张飞"
}
}
]
}
}