ElasticSearch工具类

  1. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  2. import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
  3. import org.elasticsearch.action.delete.DeleteResponse;
  4. import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
  5. import org.elasticsearch.action.support.master.AcknowledgedResponse;
  6. import org.elasticsearch.client.IndicesAdminClient;
  7. import org.elasticsearch.client.transport.TransportClient;
  8. import org.elasticsearch.common.settings.Settings;
  9. import org.elasticsearch.common.transport.TransportAddress;
  10. import org.elasticsearch.common.xcontent.XContentType;
  11. import org.elasticsearch.transport.client.PreBuiltTransportClient;
  12. import java.net.InetAddress;
  13. import java.net.UnknownHostException;
  14. public class ESUtil {
  15. //集群名,默认值elasticsearch
  16. private static final String CLUSTER_NAME = "elasticsearch";
  17. //ES集群中某个节点
  18. private static final String HOSTNAME = "node1";
  19. //连接端口号
  20. private static final int TCP_PORT = 9300;
  21. //构建Settings对象
  22. private static Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME).build();
  23. //TransportClient对象,用于连接ES集群
  24. private static volatile TransportClient client;
  25. /**
  26. * 同步synchronized(*.class)代码块的作用和synchronized static方法作用一样,
  27. * 对当前对应的*.class进行持锁,static方法和.class一样都是锁的该类本身,同一个监听器
  28. * @return
  29. * @throws UnknownHostException
  30. */
  31. public static TransportClient getClient(){
  32. if(client==null){
  33. synchronized (TransportClient.class){
  34. try {
  35. client=new PreBuiltTransportClient(settings)
  36. .addTransportAddress(new TransportAddress(InetAddress.getByName(HOSTNAME), TCP_PORT));
  37. } catch (UnknownHostException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. return client;
  43. }
  44. /**
  45. * 获取索引管理的IndicesAdminClient
  46. */
  47. public static IndicesAdminClient getAdminClient() {
  48. return getClient().admin().indices();
  49. }
  50. /**
  51. * 判定索引是否存在
  52. * @param indexName
  53. * @return
  54. */
  55. public static boolean isExists(String indexName){
  56. IndicesExistsResponse response=getAdminClient().prepareExists(indexName).get();
  57. return response.isExists()?true:false;
  58. }
  59. /**
  60. * 创建索引
  61. * @param indexName
  62. * @return
  63. */
  64. public static boolean createIndex(String indexName){
  65. CreateIndexResponse createIndexResponse = getAdminClient()
  66. .prepareCreate(indexName.toLowerCase())
  67. .get();
  68. return createIndexResponse.isAcknowledged()?true:false;
  69. }
  70. /**
  71. * 创建索引
  72. * @param indexName 索引名
  73. * @param shards 分片数
  74. * @param replicas 副本数
  75. * @return
  76. */
  77. public static boolean createIndex(String indexName, int shards, int replicas) {
  78. Settings settings = Settings.builder()
  79. .put("index.number_of_shards", shards)
  80. .put("index.number_of_replicas", replicas)
  81. .build();
  82. CreateIndexResponse createIndexResponse = getAdminClient()
  83. .prepareCreate(indexName.toLowerCase())
  84. .setSettings(settings)
  85. .execute().actionGet();
  86. return createIndexResponse.isAcknowledged()?true:false;
  87. }
  88. /**
  89. * 位索引indexName设置mapping
  90. * @param indexName
  91. * @param typeName
  92. * @param mapping
  93. */
  94. public static void setMapping(String indexName, String typeName, String mapping) {
  95. getAdminClient().preparePutMapping(indexName)
  96. .setType(typeName)
  97. .setSource(mapping, XContentType.JSON)
  98. .get();
  99. }
  100. /**
  101. * 删除索引
  102. * @param indexName
  103. * @return
  104. */
  105. public static boolean deleteIndex(String indexName) {
  106. // DeleteResponse response = client.prepareDelete("twitter", "_doc", "1").get();
  107. // DeleteResponse deleteResponse = getAdminClient()
  108. // .prepareDelete(indexName.toLowerCase())
  109. // .get();
  110. // DeleteResponse response = getAdminClient().prepareDelete(indexName).get();
  111. AcknowledgedResponse response = getAdminClient().prepareDelete(indexName).get();
  112. // DeleteResponse response = getClient().prepareDelete().get();
  113. return response.isAcknowledged()?true:false;
  114. }
  115. }