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("{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " }\n" + "}", 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": "张飞" } } ] } }
本文暂时没有评论,来添加一个吧(●'◡'●)