基于java的ES开发
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开发 的全部内容, 来源链接: utcz.com/z/390230.html