java API参考

    1. package com.hfqx.decodelpd.dao;
    2. import com.hfqx.decodelpd.utils.PropertiesUtils;
    3. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
    4. import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
    5. import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
    6. import org.elasticsearch.action.bulk.BulkRequestBuilder;
    7. import org.elasticsearch.action.search.SearchRequestBuilder;
    8. import org.elasticsearch.action.search.SearchResponse;
    9. import org.elasticsearch.action.search.SearchType;
    10. import org.elasticsearch.client.Requests;
    11. import org.elasticsearch.client.transport.TransportClient;
    12. import org.elasticsearch.common.settings.ImmutableSettings;
    13. import org.elasticsearch.common.settings.Settings;
    14. import org.elasticsearch.common.transport.InetSocketTransportAddress;
    15. import org.elasticsearch.common.xcontent.XContentBuilder;
    16. import org.elasticsearch.index.query.QueryBuilder;
    17. import org.elasticsearch.index.query.QueryBuilders;
    18. import org.elasticsearch.search.SearchHit;
    19. import javax.annotation.PreDestroy;
    20. import java.io.IOException;
    21. import java.net.InetAddress;
    22. import java.net.UnknownHostException;
    23. import java.util.ArrayList;
    24. import java.util.List;
    25. import java.util.Map;
    26. import java.util.Properties;
    27. import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
    28. /**
    29. * description
    30. *
    31. * @author ${user}
    32. * @Time 2019-06-19
    33. */
    34. public class ElasticSearch {
    35. private static String eshost;
    36. private static int esport;
    37. private static String esclustername;
    38. private static TransportClient client = null;
    39. // private static BulkRequestBuilder bulkRequest = null;
    40. private static PropertiesUtils properties = new PropertiesUtils();
    41. private SearchRequestBuilder searchRequestBuilder = null;
    42. static {
    43. eshost = properties.getProperty("es.node-host");
    44. esport = Integer.valueOf(properties.getProperty("es.node-port"));
    45. esclustername = properties.getProperty("es.cluster-name");
    46. System.out.println(eshost);
    47. System.out.println(esport);
    48. System.out.println(esclustername);
    49. Settings settings = ImmutableSettings.settingsBuilder()
    50. .put("cluster.name", esclustername).build();
    51. client = new TransportClient(settings);
    52. try {
    53. client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(eshost), 9300));
    54. // bulkRequest = client.prepareBulk();
    55. } catch (UnknownHostException e) {
    56. e.printStackTrace();
    57. }
    58. }
    59. /**
    60. * 构建索引 mapping
    61. * @return
    62. */
    63. public XContentBuilder getMapping() {
    64. XContentBuilder mapping = null;
    65. try {
    66. mapping = jsonBuilder().startObject()
    67. .startObject("properties")
    68. .startObject("province")
    69. .field("type","string")
    70. .field("index","not_analyzed")
    71. .endObject()
    72. .startObject("city")
    73. .field("type","string")
    74. .field("index","not_analyzed")
    75. .endObject()
    76. .startObject("county")
    77. .field("type","string")
    78. .field("index","not_analyzed")
    79. .endObject()
    80. .endObject()
    81. .endObject();
    82. } catch (IOException e) {
    83. e.printStackTrace();
    84. }
    85. return mapping;
    86. }
    87. /**
    88. * apply to create mapping that implement the fields
    89. * @param index
    90. * @param type
    91. */
    92. // 创建映射关系
    93. public void createMapping(String index, String type) {
    94. if ( !existIndex(index) ) {
    95. createIndex(index);
    96. }
    97. PutMappingRequest mappingRequest = Requests.putMappingRequest(index).type(type).source(getMapping());
    98. client.admin().indices().putMapping(mappingRequest).actionGet();
    99. }
    100. // 创建索引
    101. public void createIndex(String index) {
    102. CreateIndexRequest request = new CreateIndexRequest(index);
    103. client.admin().indices().create(request);
    104. }
    105. // 检查索引是否存储
    106. public boolean existIndex(String index) {
    107. boolean flag = client.admin().indices().exists(new IndicesExistsRequest()
    108. .indices(new String[]{index})).actionGet().isExists();
    109. return flag;
    110. }
    111. public void SyncIndex(String index, String type, String id, Map<String, Object> map) {
    112. BulkRequestBuilder bulkRequest = null;
    113. bulkRequest = client.prepareBulk();
    114. bulkRequest.add(client.prepareIndex(index, type, id).setSource(map));
    115. bulkRequest.execute().actionGet();
    116. return;
    117. }
    118. public List<Map<String, Object>> SearchByQuery(String index, String type, List<QueryBuilder> queryBuildersList) {
    119. List<Map<String, Object>> result = new ArrayList<>();
    120. searchRequestBuilder = client.prepareSearch(index).setTypes(type).setSearchType(SearchType.DEFAULT);
    121. // for(QueryBuilder builder:queryBuildersList) {
    122. // searchRequestBuilder.setQuery(builder);
    123. // }
    124. searchRequestBuilder.setQuery(QueryBuilders.boolQuery().must(queryBuildersList.get(0)).must(queryBuildersList.get(1)));
    125. // SearchResponse searchRespons = searchRequestBuilder.execute().actionGet();
    126. SearchResponse searchRespons = searchRequestBuilder.execute().actionGet();
    127. SearchHit[] hitArray = searchRespons.getHits().getHits();
    128. for( int i = 0; i < hitArray.length; i++ ) {
    129. System.out.println(hitArray[i].id());
    130. System.out.println(hitArray[i].sourceAsMap());
    131. result.add(hitArray[i].sourceAsMap());
    132. }
    133. return result;
    134. }
    135. }