PageRenderTime 98ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/elasticsearch-practice/src/main/java/com/doctor/elasticsearch_1_4_2/ElasticsearchGuideClient.java

https://gitlab.com/doctorwho1986/doctor
Java | 284 lines | 220 code | 42 blank | 22 comment | 7 complexity | bf96c4e282de0c3f2815cd79aa392767 MD5 | raw file
  1. package com.doctor.elasticsearch_1_4_2;
  2. import java.time.LocalDateTime;
  3. import java.util.Arrays;
  4. import java.util.HashMap;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.elasticsearch.action.bulk.BulkRequestBuilder;
  8. import org.elasticsearch.action.bulk.BulkResponse;
  9. import org.elasticsearch.action.delete.DeleteResponse;
  10. import org.elasticsearch.action.get.GetResponse;
  11. import org.elasticsearch.action.index.IndexRequestBuilder;
  12. import org.elasticsearch.action.index.IndexResponse;
  13. import org.elasticsearch.action.search.SearchResponse;
  14. import org.elasticsearch.action.search.SearchType;
  15. import org.elasticsearch.action.update.UpdateRequest;
  16. import org.elasticsearch.action.update.UpdateResponse;
  17. import org.elasticsearch.client.transport.TransportClient;
  18. import org.elasticsearch.common.settings.ImmutableSettings;
  19. import org.elasticsearch.common.settings.Settings;
  20. import org.elasticsearch.common.transport.InetSocketTransportAddress;
  21. import org.elasticsearch.common.unit.TimeValue;
  22. import org.elasticsearch.index.query.QueryBuilders;
  23. import org.elasticsearch.script.ScriptService;
  24. import org.elasticsearch.search.SearchHit;
  25. import org.junit.After;
  26. import org.junit.Before;
  27. import org.junit.Test;
  28. import com.alibaba.fastjson.JSON;
  29. /**
  30. * 官方文档练习
  31. * http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.html
  32. * @author doctor
  33. *
  34. * @since 2015年1月20日 上午12:28:38
  35. */
  36. public class ElasticsearchGuideClient {
  37. private TransportClient transportClient;
  38. @Before
  39. public void init() {
  40. Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "elasticsearch").build();
  41. transportClient = new TransportClient(settings);
  42. transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
  43. }
  44. @After
  45. public void destroy() {
  46. transportClient.close();
  47. }
  48. /**
  49. * index api
  50. * @see http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.html
  51. */
  52. @Test
  53. public void test_index() {
  54. for (Tweet tweet : TweetDb.Tweets) {
  55. IndexResponse actionGet = transportClient.prepareIndex(TweetField.index, TweetField.type, String.valueOf(tweet.getId()))
  56. .setSource(tweet.toString()).execute().actionGet();
  57. System.out.println(tweet+ "index :" + actionGet.isCreated());
  58. }
  59. }
  60. /**
  61. * @see http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/get.html
  62. */
  63. @Test
  64. public void test_get_api(){
  65. GetResponse actionGet = transportClient.prepareGet(TweetField.index, TweetField.type, String.valueOf(2)).execute().actionGet();
  66. System.out.println("---------------test_get_api----------------");
  67. System.out.println("getIndex:" + actionGet.getIndex());
  68. System.out.println("getType:" + actionGet.getType());
  69. System.out.println("getId:" + actionGet.getId());
  70. System.out.println("getVersion:" + actionGet.getVersion());
  71. System.out.println("getSourceAsString:" + actionGet.getSourceAsString());
  72. System.out.println("isExists:" + actionGet.isExists());
  73. }
  74. @Test
  75. public void test_delete_api(){
  76. DeleteResponse deleteResponse = transportClient.prepareDelete(TweetField.index, TweetField.type, String.valueOf(1)).execute().actionGet();
  77. System.out.println("----------------test_delete_api----------------");
  78. System.out.println("getIndex:" + deleteResponse.getIndex());
  79. System.out.println("getType:" + deleteResponse.getType());
  80. System.out.println("getId:" + deleteResponse.getId());
  81. System.out.println("isFound:" + deleteResponse.isFound());
  82. }
  83. /**
  84. * @see http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/java-update-api.html
  85. */
  86. @Test
  87. public void test_update_api_use_UpdateRequest(){
  88. //1.use UpdateRequest
  89. UpdateRequest updateRequest = new UpdateRequest();
  90. updateRequest.index(TweetField.index);
  91. updateRequest.type(TweetField.type);
  92. updateRequest.id(String.valueOf(2L));
  93. updateRequest.doc(TweetField.message, "use UpdateRequest");
  94. UpdateResponse actionGet = transportClient.update(updateRequest).actionGet();
  95. System.out.println("-----------use UpdateRequest-----------");
  96. }
  97. @Test
  98. public void test_update_use_setScript(){
  99. UpdateResponse updateResponse = transportClient.prepareUpdate(TweetField.index, TweetField.type, String.valueOf(2L))
  100. .setScript("ctx._source."+TweetField.message+"=\"update_use_setScript\"", ScriptService.ScriptType.INLINE).execute().actionGet();
  101. test_get_api();
  102. }
  103. /**
  104. * setScript setDoc 不能同时用,setDoc 用的时候,只能用一次,即 .setDoc 出现一次,如果两次,最后的.setDoc只生效
  105. *
  106. */
  107. @Test
  108. public void test_update_use_setDoc(){
  109. UpdateResponse updateResponse = transportClient.prepareUpdate(TweetField.index, TweetField.type, String.valueOf(2L))
  110. .setDoc(TweetField.message, "update_use_doc")
  111. .execute().actionGet();
  112. test_get_api();
  113. Map<String, Object> map = new HashMap<>();
  114. map.put(TweetField.message, "update_use_doc 2");
  115. map.put(TweetField.user, "doctor 12");
  116. updateResponse = transportClient.prepareUpdate(TweetField.index, TweetField.type, String.valueOf(2L))
  117. .setDoc(map)
  118. .execute().actionGet();
  119. test_get_api();
  120. }
  121. @Test
  122. public void test_bulk_update(){
  123. BulkRequestBuilder bulkRequestBuilder = transportClient.prepareBulk();
  124. for (Tweet tweet : TweetDb.Tweets2) {
  125. IndexRequestBuilder indexRequestBuilder = transportClient.prepareIndex(TweetField.index, TweetField.type, String.valueOf(tweet.getId()))
  126. .setSource(tweet.toString());
  127. bulkRequestBuilder.add(indexRequestBuilder);
  128. }
  129. BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
  130. System.out.println("hasFailures:" + bulkResponse.hasFailures());
  131. System.out.println("---------bulk_update------------");
  132. SearchResponse searchResponse = transportClient.prepareSearch(TweetField.index).setTypes(TweetField.type)
  133. .setQuery(QueryBuilders.rangeQuery(TweetField.id).gte(0L).lte(100L))
  134. .setSize(Integer.MAX_VALUE) //这个默认返回10个,必须设定
  135. .execute().actionGet();
  136. System.out.println(searchResponse.getHits().getHits().length);
  137. for (SearchHit searchHit : searchResponse.getHits().getHits()) {
  138. print(searchHit);
  139. }
  140. }
  141. @Test
  142. public void test_use_scroll_search(){
  143. SearchResponse searchResponse = transportClient.prepareSearch(TweetField.index)
  144. .setTypes(TweetField.type)
  145. .setSearchType(SearchType.SCAN)
  146. .setScroll(new TimeValue(600000))
  147. .setQuery(QueryBuilders.rangeQuery(TweetField.id).gte(1L).lte(100L))
  148. .setSize(10).execute().actionGet();
  149. while (true) {
  150. for (SearchHit searchHit : searchResponse.getHits().getHits()) {
  151. print(searchHit);
  152. }
  153. searchResponse = transportClient
  154. .prepareSearchScroll(searchResponse.getScrollId())
  155. .setScroll(new TimeValue(600000)).execute()
  156. .actionGet();
  157. if (searchResponse.getHits().getHits().length == 0) {
  158. break;
  159. }
  160. }
  161. }
  162. private static void print(SearchHit searchHit){
  163. System.out.println("getIndex:" + searchHit.getIndex());
  164. System.out.println("getType:" + searchHit.getType());
  165. System.out.println("getId:" + searchHit.getId());
  166. System.out.println("getScore:" + searchHit.getScore());
  167. System.out.println("getVersion:" + searchHit.getVersion());
  168. System.out.println("getSourceAsString:" + searchHit.getSourceAsString());
  169. }
  170. private static interface TweetField{
  171. String index = "twitter";
  172. String type = "tweet";
  173. String user = "user";
  174. String message = "message";
  175. String postDate = "postDate";
  176. String id = "id";
  177. }
  178. private static class TweetDb {
  179. public static final List<Tweet> Tweets = Arrays.asList(
  180. new Tweet(1L, "doctor", LocalDateTime.now(), "hello es"),
  181. new Tweet(2L, "kimchy", LocalDateTime.now().minusDays(2L), "hello kimchy"),
  182. new Tweet(3L, "sim", LocalDateTime.now().minusDays(4L), "hello sim"),
  183. new Tweet(4L, "madi", LocalDateTime.now().minusMonths(1L), "hello madi"),
  184. new Tweet(5L, "ming", LocalDateTime.now().plusDays(3L), "hello min")
  185. );
  186. public static final List<Tweet> Tweets2 = Arrays.asList(
  187. new Tweet(6L, "udoctor6", LocalDateTime.now(), "uhello es8"),
  188. new Tweet(7L, "ukimchy6", LocalDateTime.now().minusDays(21L), "uhello g kimchy"),
  189. new Tweet(8L, "usim6", LocalDateTime.now().minusDays(14L), "uhello m sim"),
  190. new Tweet(9L, "umadi6", LocalDateTime.now().minusMonths(11L), " uh hello madi"),
  191. new Tweet(10L, "uming6", LocalDateTime.now().plusDays(13L), "m hello min")
  192. );
  193. }
  194. private static class Tweet {
  195. private Long id;
  196. private String user;
  197. private LocalDateTime postDate;
  198. private String message;
  199. public Tweet() {
  200. }
  201. public Tweet(Long id, String user, LocalDateTime postDate, String message) {
  202. this.id = id;
  203. this.user = user;
  204. this.postDate = postDate;
  205. this.message = message;
  206. }
  207. public Long getId() {
  208. return id;
  209. }
  210. public void setId(Long id) {
  211. this.id = id;
  212. }
  213. public String getUser() {
  214. return user;
  215. }
  216. public void setUser(String user) {
  217. this.user = user;
  218. }
  219. public LocalDateTime getPostDate() {
  220. return postDate;
  221. }
  222. public void setPostDate(LocalDateTime postDate) {
  223. this.postDate = postDate;
  224. }
  225. public String getMessage() {
  226. return message;
  227. }
  228. public void setMessage(String message) {
  229. this.message = message;
  230. }
  231. @Override
  232. public String toString() {
  233. return JSON.toJSONString(this);
  234. }
  235. public static void main(String[] args) {
  236. Tweet tweet = new Tweet(1L, "doctor", LocalDateTime.now(), "hello");
  237. System.out.println(tweet);
  238. Tweet parseObject = JSON.parseObject(tweet.toString(), Tweet.class);
  239. System.out.println(parseObject);
  240. }
  241. }
  242. }