1. 程式人生 > >基於java的ES開發

基於java的ES開發

lis 精通 spa exec upd 進行 版本 get() bool

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開發