1. JAVA API

官网的资料才是最新的,最全的:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-overview.html

引入maven

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  4. <version>7.11.1</version>
  5. </dependency>
  6. <repositories>
  7. <repository>
  8. <id>es-snapshots</id>
  9. <name>elasticsearch snapshot repo</name>
  10. <url>https://snapshots.elastic.co/maven/</url>
  11. </repository>
  12. </repositories>
  13. <repository>
  14. <id>elastic-lucene-snapshots</id>
  15. <name>Elastic Lucene Snapshots</name>
  16. <url>https://s3.amazonaws.com/download.elasticsearch.org/lucenesnapshots/83f9835</url>
  17. <releases><enabled>true</enabled></releases>
  18. <snapshots><enabled>false</enabled></snapshots>
  19. </repository>

1.1 基本增删改查

  1. import org.apache.http.HttpHost;
  2. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  3. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;
  6. import org.elasticsearch.action.bulk.BulkRequest;
  7. import org.elasticsearch.action.bulk.BulkResponse;
  8. import org.elasticsearch.action.delete.DeleteRequest;
  9. import org.elasticsearch.action.delete.DeleteResponse;
  10. import org.elasticsearch.action.get.GetRequest;
  11. import org.elasticsearch.action.get.GetResponse;
  12. import org.elasticsearch.action.index.IndexRequest;
  13. import org.elasticsearch.action.index.IndexResponse;
  14. import org.elasticsearch.action.update.UpdateRequest;
  15. import org.elasticsearch.action.update.UpdateResponse;
  16. import org.elasticsearch.client.RequestOptions;
  17. import org.elasticsearch.client.RestClient;
  18. import org.elasticsearch.client.RestHighLevelClient;
  19. import org.elasticsearch.common.settings.Settings;
  20. import org.elasticsearch.common.xcontent.XContentType;
  21. import java.io.IOException;
  22. import java.util.HashMap;
  23. import java.util.Map;
  24. public class TestElasticsearch {
  25. public static void main(String[] args) throws IOException {
  26. RestHighLevelClient client = createClient();//创建链接,创建客户端
  27. try {
  28. // createIndex(client); //创建索引
  29. // addData(client); //添加数据
  30. // queryData(client); //查询数据
  31. // updateData(client); //修改数据
  32. // queryData(client); //查询数据
  33. // deleteData(client); //删除数据
  34. // deleteIndex(client); //删除索引
  35. bulk(client); //批量操作
  36. }catch (Exception e){
  37. e.printStackTrace();
  38. }finally {
  39. if (client != null){
  40. closeClient(client); //关闭客户端
  41. }
  42. }
  43. }
  44. /**
  45. * 创建链接,创建客户端
  46. * @return
  47. */
  48. public static RestHighLevelClient createClient(){
  49. //创建链接,创建客户端
  50. HttpHost http9200 = new HttpHost("8.140.122.156", 9200, "http");
  51. // HttpHost http9201 = new HttpHost("8.140.122.156", 9201, "http");
  52. RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(http9200));
  53. return client;
  54. }
  55. /**
  56. * 关闭客户端
  57. * @param client
  58. * @throws IOException
  59. */
  60. public static void closeClient(RestHighLevelClient client) throws IOException {
  61. client.close();
  62. }
  63. /**
  64. * 创建索引
  65. * PUT order1
  66. * {
  67. * "settings": {
  68. * "number_of_shards": 3,
  69. * "number_of_replicas": 2
  70. * },
  71. * "mappings": {
  72. * "properties": {
  73. * "id":{
  74. * "type": "long"
  75. * },
  76. * "age":{
  77. * "type": "short"
  78. * },
  79. * "mail":{
  80. * "type": "text",
  81. * "fields": {
  82. * "keyword" : {
  83. * "type" : "keyword",
  84. * "ignore_above" : 64
  85. * }
  86. * }
  87. * },
  88. * "name":{
  89. * "type": "text",
  90. * "fields": {
  91. * "keyword" : {
  92. * "type" : "keyword",
  93. * "ignore_above" : 64
  94. * }
  95. * }
  96. * },
  97. * "createTime":{
  98. * "type": "date",
  99. * "format": ["yyyy-MM-dd HH:mm:ss"]
  100. * }
  101. * }
  102. * }
  103. * }
  104. * @param client
  105. * @throws IOException
  106. */
  107. public static void createIndex(RestHighLevelClient client) throws IOException {
  108. //设置3个分片,每个分片2副本
  109. Settings settings = Settings.builder()
  110. .put("number_of_shards",3)
  111. .put("number_of_replicas",2)
  112. .build();
  113. //新增索引
  114. CreateIndexRequest createIndexRequest = new CreateIndexRequest("order1");
  115. createIndexRequest.mapping("properties","{\"id\":{\"type\":\"long\"},\"age\":{\"type\":\"short\"},\"mail\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":64}}},\"name\":{\"type\":\"text\",\"fields\":{\"keyword\":{\"type\":\"keyword\",\"ignore_above\":64}}},\"createTime\":{\"type\":\"date\",\"format\":[\"yyyy-MM-dd HH:mm:ss\"]}}",XContentType.JSON);
  116. createIndexRequest.settings(settings);
  117. //执行
  118. CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
  119. System.out.println("创建索引结果: "+createIndexResponse.isAcknowledged());
  120. }
  121. /**
  122. * 插入数据
  123. *
  124. * @param client
  125. * @throws IOException
  126. */
  127. public static void addData(RestHighLevelClient client) throws IOException {
  128. //数据
  129. Map personMap = new HashMap();
  130. personMap.put("id",1);
  131. personMap.put("age",28);
  132. personMap.put("name","王帆");
  133. personMap.put("email","wangfan@yxinsur.com");
  134. personMap.put("createTime","2020-02-02 12:00:00");
  135. //新增索引
  136. IndexRequest indexRequest = new IndexRequest("order1","_doc","1");
  137. indexRequest.source(personMap);
  138. //执行
  139. IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
  140. System.out.println("插入数据结果: "+indexResponse.toString());
  141. }
  142. /**
  143. * 查询数据
  144. * GET order1/_doc/1
  145. * @param client
  146. * @throws IOException
  147. */
  148. public static void queryData(RestHighLevelClient client) throws IOException {
  149. //查询索引
  150. GetRequest getRequest = new GetRequest("order1","_doc","1");
  151. //执行
  152. GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
  153. System.out.println("查询数据结果: "+getResponse.toString());
  154. }
  155. /**
  156. * 修改数据
  157. * PUT order1/_doc/1
  158. * {
  159. * "name": "王帆",
  160. * "age": 29,
  161. * "email": "wangfan@yxinsur.com"
  162. * }
  163. * @param client
  164. * @throws IOException
  165. */
  166. public static void updateData(RestHighLevelClient client) throws IOException {
  167. //实际数据
  168. Map<String,Object> personMap = new HashMap<>();
  169. personMap.put("id",1);
  170. personMap.put("age",29);
  171. personMap.put("name","王帆");
  172. personMap.put("email","wangfan@yxinsur.com");
  173. personMap.put("createTime","2020-02-02 12:00:00");
  174. //修改索引
  175. UpdateRequest updateRequest = new UpdateRequest("order1","_doc","1");
  176. updateRequest.doc(personMap);
  177. //或者是upsert
  178. //updateRequest.upsert(person);
  179. //执行
  180. UpdateResponse updateResponse = client.update(updateRequest, RequestOptions.DEFAULT);
  181. System.out.println("修改数据结果: "+updateResponse.toString());
  182. }
  183. /**
  184. * 删除数据
  185. * DELETE order1/_doc/1
  186. * @param client
  187. * @throws IOException
  188. */
  189. public static void deleteData(RestHighLevelClient client) throws IOException {
  190. //删除数据
  191. DeleteRequest deleteRequest = new DeleteRequest("order1","_doc","1");
  192. //执行
  193. DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);
  194. System.out.println("删除数据结果: "+deleteResponse.toString());
  195. }
  196. /**
  197. * 删除索引
  198. * DELETE order1/_doc/1
  199. * @param client
  200. * @throws IOException
  201. */
  202. public static void deleteIndex(RestHighLevelClient client) throws IOException {
  203. //删除索引
  204. DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("order1");
  205. //执行
  206. DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
  207. System.out.println("删除索引结果: "+deleteIndexResponse.isAcknowledged());
  208. }
  209. /**
  210. * 批量操作
  211. * @param client
  212. */
  213. public static void bulk(RestHighLevelClient client) throws IOException {
  214. //准备数据
  215. Map<String,Object> personMap = new HashMap<>();
  216. personMap.put("id",2);
  217. personMap.put("age",29);
  218. personMap.put("name","王帆");
  219. personMap.put("email","wangfan@yxinsur.com");
  220. personMap.put("createTime","2020-02-02 12:00:00");
  221. //插入数据操作
  222. IndexRequest indexRequest2 = new IndexRequest("order1","_doc","2");
  223. indexRequest2.source(personMap,XContentType.JSON);
  224. IndexRequest indexRequest3 = new IndexRequest("order1","_doc","3");
  225. indexRequest3.source(personMap,XContentType.JSON);
  226. //修改数据操作
  227. personMap.put("age",30);
  228. UpdateRequest updateRequest = new UpdateRequest("order1","_doc","2");
  229. updateRequest.doc(personMap,XContentType.JSON);
  230. //删除数据操作
  231. DeleteRequest deleteRequest = new DeleteRequest("order1","_doc","2");
  232. //将操作都放入bulk里面
  233. BulkRequest bulkRequest = new BulkRequest();
  234. bulkRequest.add(indexRequest2);
  235. bulkRequest.add(indexRequest3);
  236. bulkRequest.add(updateRequest);
  237. bulkRequest.add(deleteRequest);
  238. //执行
  239. BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
  240. System.out.println("批量操作结果: "+bulkResponse.toString());
  241. }
  242. }

创建索引结果: true插入数据结果: IndexResponse[index=order1,type=_doc,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards={“total”:3,”successful”:1,”failed”:0}] 查询数据结果: {“_index”:”order1”,”_type”:”_doc”,”_id”:”1”,”_version”:1,”found”:true,”_source”:{“createTime”:”2020-02-02 12:00:00”,”name”:”王帆”,”id”:1,”age”:28,”email”:”wangfan@yxinsur.com”},”fields”:{“_seq_no”:[0],”_primary_term”:[1]}}修改数据结果: UpdateResponse[index=order1,type=_doc,id=1,version=2,seqNo=1,primaryTerm=1,result=updated,shards=ShardInfo{total=3, successful=1, failures=[]}]

查询数据结果: {“_index”:”order1”,”_type”:”_doc”,”_id”:”1”,”_version”:2,”found”:true,”_source”:{“createTime”:”2020-02-02 12:00:00”,”name”:”王帆”,”id”:1,”age”:29,”email”:”wangfan@yxinsur.com”},”fields”:{“_seq_no”:[1],”_primary_term”:[1]}}

删除数据结果: DeleteResponse[index=order1,type=_doc,id=1,version=3,result=deleted,shards=ShardInfo{total=3, successful=1, failures=[]}]

删除索引结果: true

批量操作结果: org.elasticsearch.action.bulk.BulkResponse@793be5ca