pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>groupId</groupId>
  7. <artifactId>TestES</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <name>es-core-first</name>
  10. <url>http://maven.apache.org</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. </properties>
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.elasticsearch.client</groupId>
  17. <artifactId>transport</artifactId>
  18. <version>6.7.0</version>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.apache.logging.log4j</groupId>
  22. <artifactId>log4j-api</artifactId>
  23. <version>2.7</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.apache.logging.log4j</groupId>
  27. <artifactId>log4j-core</artifactId>
  28. <version>2.7</version>
  29. </dependency>
  30. </dependencies>
  31. </project>

CURD 增删该查

  1. import org.elasticsearch.action.delete.DeleteResponse;
  2. import org.elasticsearch.action.get.GetResponse;
  3. import org.elasticsearch.action.index.IndexResponse;
  4. import org.elasticsearch.action.update.UpdateResponse;
  5. import org.elasticsearch.client.transport.TransportClient;
  6. import org.elasticsearch.common.settings.Settings;
  7. import org.elasticsearch.common.xcontent.XContentBuilder;
  8. import org.elasticsearch.common.xcontent.XContentFactory;
  9. import org.elasticsearch.transport.client.PreBuiltTransportClient;
  10. import org.elasticsearch.common.transport.TransportAddress;
  11. import java.io.IOException;
  12. import java.net.InetAddress;
  13. import java.net.InetSocketAddress;
  14. import java.net.UnknownHostException;
  15. public class TestESCURD {
  16. public static void main(String[] args) {
  17. Settings settings = Settings.builder()
  18. .put("cluster.name", "elasticsearch")
  19. .build();
  20. try {
  21. TransportClient client = new PreBuiltTransportClient(settings)
  22. .addTransportAddress(new TransportAddress(InetAddress.getByName("sa0"),9300));
  23. // CreateEmployee(client);
  24. // UpdateEmployee(client);
  25. // getEmployee(client);
  26. deleteEmployee(client);
  27. client.close();
  28. } catch (UnknownHostException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. // 增加
  33. public static void CreateEmployee(TransportClient client) {
  34. try {
  35. IndexResponse response = client.prepareIndex("company","employee", "1")
  36. .setSource(XContentFactory.jsonBuilder()
  37. .startObject()
  38. .field("name","jack")
  39. .field("age",27)
  40. .field("position","technique")
  41. .field("country","china")
  42. .field("join_date","2017-01-01")
  43. .field("slary",10000)
  44. .endObject()).get();
  45. System.out.println(response.getResult());
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. // 查询
  51. public static void getEmployee(TransportClient client) {
  52. GetResponse response = client.prepareGet("company","employee","1").get();
  53. System.out.println(response.getSourceAsString());
  54. }
  55. // 更新
  56. public static void UpdateEmployee(TransportClient client) {
  57. UpdateResponse response = null;
  58. try {
  59. response = client.prepareUpdate("company", "employee", "1")
  60. .setDoc(XContentFactory.jsonBuilder()
  61. .startObject()
  62. .field("position","techine manger")
  63. .endObject()).get();
  64. System.out.println(response.getResult());
  65. } catch (IOException e) {
  66. e.printStackTrace();
  67. }
  68. }
  69. // 删除
  70. public static void deleteEmployee(TransportClient client) {
  71. DeleteResponse response = client.prepareDelete("company", "employee", "1").get();
  72. System.out.println(response.getResult());
  73. }
  74. }

查询

  1. import org.elasticsearch.action.search.SearchResponse;
  2. import org.elasticsearch.client.transport.TransportClient;
  3. import org.elasticsearch.common.settings.Settings;
  4. import org.elasticsearch.common.transport.TransportAddress;
  5. import org.elasticsearch.common.xcontent.XContentFactory;
  6. import org.elasticsearch.index.query.QueryBuilders;
  7. import org.elasticsearch.search.SearchHit;
  8. import org.elasticsearch.transport.client.PreBuiltTransportClient;
  9. import java.net.InetAddress;
  10. import java.net.UnknownHostException;
  11. public class EmployeeSearchApp {
  12. public static void main(String[] args) {
  13. Settings settings = Settings.builder()
  14. .put("cluster.name","elasticsearch")
  15. .build();
  16. try {
  17. TransportClient client = new PreBuiltTransportClient(settings)
  18. .addTransportAddress(new TransportAddress(InetAddress.getByName("sa0"),9300));
  19. // prepareData(client);
  20. executeSearch(client);
  21. client.close();
  22. } catch (UnknownHostException e) {
  23. e.printStackTrace();
  24. } catch (Exception e) {
  25. e.printStackTrace();
  26. }
  27. }
  28. public static void executeSearch(TransportClient client) {
  29. SearchResponse response = client.prepareSearch("company")
  30. .setQuery(QueryBuilders.matchQuery("position", "technique"))
  31. .setPostFilter(QueryBuilders.rangeQuery("age").from(30).to(40))
  32. .setFrom(0).setSize(1)
  33. .get();
  34. SearchHit[] searchHits = response.getHits().getHits();
  35. for( int i = 0; i < searchHits.length; i++ ) {
  36. System.out.println(searchHits[i].getSourceAsString());
  37. }
  38. }
  39. public static void prepareData(TransportClient client) throws Exception {
  40. client.prepareIndex("company", "employee", "1")
  41. .setSource(XContentFactory.jsonBuilder()
  42. .startObject()
  43. .field("name", "jack")
  44. .field("age", 27)
  45. .field("position", "technique software")
  46. .field("country", "china")
  47. .field("join_date", "2017-01-01")
  48. .field("salary", 10000)
  49. .endObject())
  50. .get();
  51. client.prepareIndex("company", "employee", "2")
  52. .setSource(XContentFactory.jsonBuilder()
  53. .startObject()
  54. .field("name", "marry")
  55. .field("age", 35)
  56. .field("position", "technique manager")
  57. .field("country", "china")
  58. .field("join_date", "2017-01-01")
  59. .field("salary", 12000)
  60. .endObject())
  61. .get();
  62. client.prepareIndex("company", "employee", "3")
  63. .setSource(XContentFactory.jsonBuilder()
  64. .startObject()
  65. .field("name", "tom")
  66. .field("age", 32)
  67. .field("position", "senior technique software")
  68. .field("country", "china")
  69. .field("join_date", "2016-01-01")
  70. .field("salary", 11000)
  71. .endObject())
  72. .get();
  73. client.prepareIndex("company", "employee", "4")
  74. .setSource(XContentFactory.jsonBuilder()
  75. .startObject()
  76. .field("name", "jen")
  77. .field("age", 25)
  78. .field("position", "junior finance")
  79. .field("country", "usa")
  80. .field("join_date", "2016-01-01")
  81. .field("salary", 7000)
  82. .endObject())
  83. .get();
  84. client.prepareIndex("company", "employee", "5")
  85. .setSource(XContentFactory.jsonBuilder()
  86. .startObject()
  87. .field("name", "mike")
  88. .field("age", 37)
  89. .field("position", "finance manager")
  90. .field("country", "usa")
  91. .field("join_date", "2015-01-01")
  92. .field("salary", 15000)
  93. .endObject())
  94. .get();
  95. }
  96. }

实例:

  1. Settings settings = ImmutableSettings
  2. .settingsBuilder()
  3. .put("cluster.name", "elasticsearch")
  4. .build();
  5. TransportClient client = new TransportClient(settings)
  6. .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.253"),9300));
  7. GetResponse response = client.prepareGet("qx_day", "qx_day", "45853483780199").get();
  8. System.out.println(response.getSourceAsString());
  9. SearchResponse searchResponse = client.prepareSearch("qx_day").get();
  10. SearchHit[] searchHits = searchResponse.getHits().getHits();
  11. for ( int i = 0; i < searchHits.length; i++ ) {
  12. System.out.println(searchHits[i].getSourceAsString());
  13. }

精确匹配过滤

  1. public static void SearchByReq() {
  2. Settings settings = ImmutableSettings.settingsBuilder()
  3. .put("cluster.name", "elasticsearch").build();
  4. TransportClient client = null;
  5. client = new TransportClient(settings);
  6. try {
  7. client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.253"), 9300));
  8. } catch (UnknownHostException e) {
  9. e.printStackTrace();
  10. }
  11. QueryBuilder queryBuilder = QueryBuilders.matchPhraseQuery("stationid", "53983");
  12. SearchRequestBuilder searchRequestBuilder = null;
  13. searchRequestBuilder = client.prepareSearch("qx_file").setTypes("qx_file").setSearchType(SearchType.QUERY_THEN_FETCH);
  14. searchRequestBuilder.setQuery(queryBuilder);
  15. SearchResponse searchRespons = searchRequestBuilder.execute().actionGet();
  16. SearchHit[] hitArray = searchRespons.getHits().getHits();
  17. for( int i = 0; i < hitArray.length; i++) {
  18. System.out.println(hitArray[i].id());
  19. }
  20. }

按照范围过滤

  1. // 按照起始条件和结束条件查询
  2. public static void SearchByReq1() {
  3. Settings settings = ImmutableSettings.settingsBuilder()
  4. .put("cluster.name", "elasticsearch").build();
  5. TransportClient client = null;
  6. client = new TransportClient(settings);
  7. try {
  8. client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("192.168.1.253"), 9300));
  9. } catch (UnknownHostException e) {
  10. e.printStackTrace();
  11. }
  12. SearchRequestBuilder searchRequestBuilder = null;
  13. searchRequestBuilder = client.prepareSearch("qx_file").setTypes("qx_file").setSearchType(SearchType.QUERY_THEN_FETCH);
  14. QueryBuilder queryBuilder1 = QueryBuilders.rangeQuery("stationid").gt("03547");
  15. QueryBuilder queryBuilder2 = QueryBuilders.rangeQuery("stationid").lt("53889");
  16. searchRequestBuilder.setQuery(queryBuilder1).setQuery(queryBuilder2);
  17. SearchResponse searchRespons = searchRequestBuilder.execute().actionGet();
  18. SearchHit[] hitArray = searchRespons.getHits().getHits();
  19. for( int i = 0; i < hitArray.length; i++) {
  20. System.out.println(hitArray[i].id());
  21. System.out.println(hitArray[i].sourceAsMap());
  22. }
  23. }