基於java的ES開發

來源:互聯網
上載者:User

標籤:shm   comm   under   people   auth   精通   環境   XML   基於   

3.1 環境配置

Jdk 1.8及以上

Elasticsearch.client 5.5.2(與伺服器版本一致)

Log4j 2.7及以下

maven工程必要的jar包依賴

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion>

   <groupId>com.rodge</groupId>

   <artifactId>elasticFirstDemo</artifactId>

   <version>0.0.1-SNAPSHOT</version>

 

   <properties>

      <elasticSearch.version>5.5.2</elasticSearch.version>

   </properties>

 

   <dependencies>

      <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->

      <dependency>

         <groupId>org.apache.logging.log4j</groupId>

         <artifactId>log4j-core</artifactId>

         <version>2.7</version>

      </dependency>

      <!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->

      <dependency>

         <groupId>org.elasticsearch.client</groupId>

         <artifactId>transport</artifactId>

         <version>${elasticSearch.version}</version>

      </dependency>

   </dependencies>

</project>

 

記錄檔log4j2.properties檔案內容

appender.console.type = Console

appender.console.name = console

appender.console.layout.type = PatternLayout

appender.console.layout.pattern = [%t] %-5p %c - %m%n

 

rootLogger.level = info

rootLogger.appenderRef.console.ref = console

 

3.2 簡單查詢

測試案例, 查詢book索引中novel中id為1的文檔

package com.rodge.elasticSearch.firstDemo;

 

import java.net.InetAddress;

import java.net.UnknownHostException;

 

import org.elasticsearch.action.get.GetResponse;

import org.elasticsearch.client.transport.TransportClient;

import org.elasticsearch.common.settings.Settings;

import org.elasticsearch.common.transport.InetSocketTransportAddress;

import org.elasticsearch.transport.client.PreBuiltTransportClient;

import org.junit.Test;

 

public class ESFirstDemo {

 

      @SuppressWarnings("unchecked")

      public TransportClient client() throws UnknownHostException {

           // es地址, 地址為192.168.253.129(此處地址不能加"http://"), 此處的連接埠為es的tcp連接埠為9300,

           // 而不是之前的http9200連接埠, 如果es有多可節點, 可以建立多個node, 然後再client中add進去

           // 注意,這裡的連接埠號碼不是9200,而是9300。9200連接埠是用來讓HTTP REST

           // API來訪問Elasticsearch,而9300連接埠是傳輸層監聽的預設連接埠。

           InetSocketTransportAddress node = new InetSocketTransportAddress(

                      InetAddress.getByName("192.168.253.129"), 9300);

           // es的配置類

           Settings settings = Settings.builder().put("cluster.name", "wali").build();

           // TransportClient是es啟動的核心類, 後續的關於es的開發都是圍繞著TransportClient進行的

           TransportClient client = new PreBuiltTransportClient(settings);

           client.addTransportAddress(node);

           return client;

      }

 

      @Test

      public void get() throws UnknownHostException {

           GetResponse result = client().prepareGet("book", "novel", "1").get();

           System.out.println(result.getSource().toString());

      }

 

}

3.3 插入3.3.1 指定id插入

// 添加文檔, 指定id插入

   @Test

   public void addWithID() throws IOException {

      String title = "一陽指";

      String author = "王重陽";

      int wordCount = 5000;

      Date publishDate = new Date();

      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      String format = dateFormat.format(publishDate);

     

      // 構造es文檔

      // 使用es內建的json工具構造

      XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject()

            .field("title", title).field("author", author).field("word_count", wordCount)

            .field("publish_date", format).endObject();

      //將資料插入到index為book, type為novel的索引中, 並且指定id為15

      IndexResponse indexResponse = client().prepareIndex("book", "novel").setId("15")

            .setSource(xContentBuilder).get();

      System.out.println(indexResponse.getId());

   }

3.3.2 es隨機產生id插入

// 添加文檔, id為es隨機產生

   @Test

   public void add() throws IOException {

      String title = "落英劍法";

      String author = "黃藥師";

      int wordCount = 3000;

      Date publishDate = new Date();

      SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

      String format = dateFormat.format(publishDate);

 

      // 構造es文檔

      // 使用es內建的json工具構造

      XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject()

            .field("title", title).field("author", author).field("word_count", wordCount)

            .field("publish_date", format).endObject();

      //將資料插入到index為book, type為novel中的索引中, id有es隨機產生, 並擷取返回資訊

      IndexResponse indexResponse = client().prepareIndex("book", "novel")

            .setSource(xContentBuilder).get();

      System.out.println(indexResponse.toString());

   }

3.3.4 批量插入

@Test

   public void dbImportEs() {

      Long count = 10000L;// 每次採集條數

      String index = "rodge";// es索引名稱

      String type = "user";// es中類型名稱

      BulkRequestBuilder prepareBulk = transportClient.prepareBulk();

      long startTime = System.currentTimeMillis();

      System.out.println("startTime: " + startTime);

      for (int i = 1; i <= count; i++) {

         Map<String, Object> ret = new HashMap<String, Object>();

         ret.put("recordtime", System.currentTimeMillis());

         ret.put("area", "北京" + i);

         ret.put("usertype", 33 + i);

         ret.put("count",  i);

         prepareBulk.add(transportClient.prepareIndex(index, type).setSource(ret));

         // 每10000條提交一次

         if (i % 1000 == 0) {

            BulkResponse actionGet = prepareBulk.execute().actionGet();

            System.out.println(actionGet.toString());

            long endTime = System.currentTimeMillis();

            System.out.println("endTime: " + endTime);

            System.out.println("消耗的時間: " + (endTime - startTime) / 1000);

         }

      }

   }

3.4 刪除3.4.1 刪除指定id的文檔

// 刪除 -- 刪除指定id的文檔

   @Test

   public void deleteById() throws UnknownHostException {

      DeleteResponse deleteResponse = client()

            .prepareDelete("book", "novel", "AV8aBzNyHGrPjnHoRYlK").get();

      System.out.println(deleteResponse.getVersion());

   }

 

3.4.2 刪除索引

刪除索引是無法復原的, 慎用!

// 刪除 -- 刪除索引

   @Test

   public void deleteIndex() throws UnknownHostException {

      DeleteIndexResponse deleteIndexResponse = client().admin().indices().prepareDelete("people")

            .get();

      System.out.println(deleteIndexResponse.toString());

   }

 

3.5 修改3.5.1 根據id更新指定的文檔

// 更新 -- 根據id更新文檔

   @Test

   public void updateById() throws IOException, InterruptedException, ExecutionException {

      UpdateRequest updateRequest = new UpdateRequest("book", "novel", "1");

      XContentBuilder contentBuilder = XContentFactory.jsonBuilder().startObject();

      contentBuilder.field("author", "張三_update");

      contentBuilder.field("title", "移魂大法_update");

      contentBuilder.field("word_count", 200);

      contentBuilder.field("publish_date", "2017-10-16");

      contentBuilder.endObject();

      updateRequest.doc(contentBuilder);

     

      UpdateResponse updateResponse = client().update(updateRequest).get();

      System.out.println(updateResponse.toString());

   }

 

3.6 複合查詢介面開發

// 複合查詢

   @Test

   public void complexQuery() throws UnknownHostException {

      // 匹配查詢

      BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

      boolQueryBuilder.must(QueryBuilders.matchQuery("author", "瓦力"));

      boolQueryBuilder.must(QueryBuilders.matchQuery("title", "elasticsearch精通"));

      //boolQueryBuilder.must(QueryBuilders.matchQuery("publish_date", ""));

 

      // 範圍查詢

      RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("word_count");

      rangeQueryBuilder.from(200);

      rangeQueryBuilder.to(3000);

      boolQueryBuilder.filter(rangeQueryBuilder);

 

      SearchRequestBuilder requestBuilder = client().

            prepareSearch("book").setTypes("novel")

            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)

            .setQuery(boolQueryBuilder)

            .setFrom(0)

            .setSize(10);

      System.out.println("requestBuilder " + requestBuilder);

     

      SearchResponse searchResponse = requestBuilder.get();

      List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>();

      for (SearchHit hit : searchResponse.getHits()) {

         resultList.add(hit.getSource());

      }

      System.out.println("resultList: " + resultList);

     

   }

基於java的ES開發

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.