ElasticSearch JAVA API使用

2018/12/21 14:31
阅读数 42

POM文件中

<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.dwg</groupId>
<artifactId>ElasticTest</artifactId>
<version>0.0.1-SNAPSHOT</version>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.2.4</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

 

package com.dwg.test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.reindex.BulkByScrollResponse;
import org.elasticsearch.index.reindex.DeleteByQueryAction;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.filter.FiltersAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.filter.FiltersAggregator;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.metrics.cardinality.Cardinality;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

public class EsDemo {

// 从es查询数据
@Test
public void test1() throws UnknownHostException {

Settings settings = Settings.builder().put("cluster.name", "my-application").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

GetResponse response = client.prepareGet("megacorp", "employee", "1").execute().actionGet();
System.out.println(response.getSourceAsString());
client.close();
}

/**
* 添加文档
*
* @throws IOException
*/
@Test
public void test2() throws IOException {

Settings settings = Settings.builder().put("cluster.name", "my-application").build();
TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));
// 添加文档
/**
* put /index1 {"settings":{ "number_of_shards":3, "number_of_replicas":0 },
* "mapping":{ "blog":{ "properties":{ "id":{ "type":"long" }, "title":{
* "type":"text", "analyzer":"ik_max_word" }, "content":{ "type":"text",
* "analyzer":"ik_max_word" }, "postdate":{ "type":"date" }, "url":{
* "type":"text" } } } } }
*/
XContentBuilder doc = XContentFactory.jsonBuilder().startObject().field("id", "1")
.field("title", "Java设置模式之装饰者模式").field("content", "在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能")
.field("postdate", "2018-05-20").field("url", "csdn.net/79239072").endObject();
// 添加文档
IndexResponse response = client.prepareIndex("index1", "blog", "10").setSource(doc).get();

System.out.println(response.status());

}

// 删除文档
@Test
public void test3() throws UnknownHostException {

Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));
// 刪除文档
DeleteResponse response = client.prepareDelete("index1", "blog", "10").get();
System.out.println(response.status());
}

// 更新文档
@Test
public void test4() throws IOException, InterruptedException, ExecutionException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

UpdateRequest request = new UpdateRequest();
request.index("index1").type("blog").id("10")
.doc(XContentFactory.jsonBuilder().startObject().field("title", "单例设计模式").endObject());
UpdateResponse response = client.update(request).get();
System.out.println(response.status());
}

// upsert更新方式
@Test
public void test5() throws IOException, InterruptedException, ExecutionException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

IndexRequest request1 = new IndexRequest("index1", "blog", "8").source(XContentFactory.jsonBuilder()
.startObject().field("id", "2").field("title", "Java工厂设计模式").field("content", "静态工厂模式,实例工厂模式")
.field("postdate", "2018-05-20").field("url", "csdn.net/79239072").endObject());
UpdateRequest request2 = new UpdateRequest("index1", "blog", "8")
.doc(XContentFactory.jsonBuilder().startObject().field("title", "设计模式").endObject()).upsert(request1);

UpdateResponse response = client.update(request2).get();
System.out.println(response.status());
client.close();

}

// mget批量查询
@Test
public void test6() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

MultiGetResponse reponse = client.prepareMultiGet().add("index1", "blog", "8", "10")
.add("lib3", "user", "1", "2", "3").get();

for (MultiGetItemResponse item : reponse) {
GetResponse gr = item.getResponse();
if (gr != null && gr.isExists()) {
System.out.println(gr.getSourceAsString());
}
}
client.close();
}

// bulk批量操作
@Test
public void test7() throws IOException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

BulkRequestBuilder bulkBuild = client.prepareBulk();
// 批量添加
bulkBuild.add(client.prepareIndex("lib2", "books", "8").setSource(
XContentFactory.jsonBuilder().startObject().field("title", "python").field("price", 99).endObject()));
bulkBuild.add(client.prepareIndex("lib2", "books", "8").setSource(
XContentFactory.jsonBuilder().startObject().field("title", "VR").field("price", 29).endObject()));
BulkResponse response = bulkBuild.get();
System.out.println(response.status());
if (response.hasFailures()) {
System.out.println("失败了");
}
}

// 查询删除
@Test
public void test8() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

BulkByScrollResponse response = DeleteByQueryAction.INSTANCE.newRequestBuilder(client)
.filter(QueryBuilders.matchQuery("title", "工厂")).source("index1").get();
long counts = response.getDeleted();
System.out.println(counts);
}

// match_all
@Test
public void test9() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

QueryBuilder qb = QueryBuilders.matchAllQuery();

SearchResponse sr = client.prepareSearch("index1").setQuery(qb).setSize(3).get();

SearchHits hits = sr.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
Map<String, Object> map = hit.getSourceAsMap();
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
}
}

// match query
@Test
public void test10() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));
QueryBuilder builder = QueryBuilders.matchQuery("title", "设计");

SearchResponse response = client.prepareSearch("index1").setQuery(builder).setSize(3).get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
Map<String, Object> map = hit.getSourceAsMap();
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
}

}

// multiMatchQuery
@Test
public void test11() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));
QueryBuilder qbBuilder = QueryBuilders.multiMatchQuery("继承", "title", "content");

SearchResponse response = client.prepareSearch("index1").setQuery(qbBuilder).setSize(3).get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
Map<String, Object> map = hit.getSourceAsMap();
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
}
}

// term查询
@Test
public void test12() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

QueryBuilder build = QueryBuilders.termQuery("title", "设计");

SearchResponse response = client.prepareSearch("index1").setQuery(build).setSize(2).get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
Map<String, Object> map = hit.getSourceAsMap();
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
}
}

// terms查询
@Test
public void test13() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

QueryBuilder build = QueryBuilders.termsQuery("title", "设计", "膳魔");

SearchResponse response = client.prepareSearch("index1").setQuery(build).setSize(2).get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
Map<String, Object> map = hit.getSourceAsMap();
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
}

}

// 各种查询
@Test
public void test14() throws UnknownHostException {

Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));
// range查询
// QueryBuilder build=QueryBuilders.rangeQuery("birthday").from("1990-01-01")
// .to("2000-12-30").format("yyyy-MM-dd");

// 前缀prefix查询
// QueryBuilder build=QueryBuilders.prefixQuery("name", "赵");

// 通配符查询wildcard查询
// QueryBuilder build=QueryBuilders.wildcardQuery("name", "zhao*");

// fuzzy查询模糊查询
// QueryBuilder build=QueryBuilders.fuzzyQuery("interests", "chag");//changge

// type查询
// QueryBuilder build=QueryBuilders.typeQuery("blog");

// ids查询
QueryBuilder build = QueryBuilders.idsQuery().addIds("1", "3");

SearchResponse response = client.prepareSearch("index1").setQuery(build).setSize(2).get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
Map<String, Object> map = hit.getSourceAsMap();
for (String key : map.keySet()) {
System.out.println(key + "=" + map.get(key));
}
}
}

// 聚合查询
@Test
public void test15() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

// 求最大值
// AggregationBuilder agg=AggregationBuilders.max("aggMax").field("age");
//
// SearchResponse response=client.prepareSearch("lib3").addAggregation(agg).get();
//
// Max max=response.getAggregations().get("aggMax");
//
// System.out.println(max.getValue());

// 求最小值
// AggregationBuilder agg=AggregationBuilders.min("aggMin").field("age");
//
// SearchResponse response=client.prepareSearch("lib3").addAggregation(agg).get();
//
// Min min=response.getAggregations().get("aggMin");
//
// System.out.println(min.getValue());

// 平均值
// AggregationBuilder agg=AggregationBuilders.avg("aggAvg").field("age");
//
// SearchResponse response=client.prepareSearch("lib3").addAggregation(agg).get();
//
// Avg avg=response.getAggregations().get("aggAvg");
//
// System.out.println(avg.getValue());

// 总和
// AggregationBuilder agg=AggregationBuilders.sum("aggSum").field("age");
//
// SearchResponse response=client.prepareSearch("lib3").addAggregation(agg).get();
//
// Sum sum=response.getAggregations().get("aggSum");
//
// System.out.println(sum.getValue());

// 基数查询
AggregationBuilder agg = AggregationBuilders.cardinality("aggCardinality").field("age");

SearchResponse response = client.prepareSearch("lib3").addAggregation(agg).get();

Cardinality cardinality = response.getAggregations().get("aggCardinality");

System.out.println(cardinality.getValue());
}

// queryString
@Test
public void tests() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));
// QueryBuilder build=QueryBuilders.commonTermsQuery("name", "zhaoliu");
// 查找有唱歌,不含喝酒,严格满足
// QueryBuilder build=QueryBuilders.queryStringQuery("+changge -hejiu");
// 不严格满足
QueryBuilder build = QueryBuilders.simpleQueryStringQuery("+changge -hejiu");

SearchResponse response = client.prepareSearch("lib3").setQuery(build).get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}

// 组合查询
@Test
public void Test16() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

// QueryBuilder build = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("interests", "changge"))
// .mustNot(QueryBuilders.matchQuery("interests", "lv")).should(QueryBuilders.matchQuery("address", "北京"))
// .filter(QueryBuilders.rangeQuery("birthday").gte("1990-01-01").format("yyyy-MM-dd"));

// constantscore
QueryBuilder build = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name", "zhaoliu"));

SearchResponse response = client.prepareSearch("lib3").setQuery(build).get();
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
}

// 分组聚合
@Test
public void testterms() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

AggregationBuilder agg = AggregationBuilders.terms("terms").field("age");

SearchResponse response = client.prepareSearch("lib3").addAggregation(agg).execute().actionGet();

Terms terms = response.getAggregations().get("terms");
for (Terms.Bucket entry : terms.getBuckets()) {
System.out.println(entry.getKey() + ":" + entry.getDocCount());
}

}

// filter聚合
@Test
public void testfilter() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

QueryBuilder query = QueryBuilders.termQuery("age", 20);

AggregationBuilder agg = AggregationBuilders.filter("filter", query);

SearchResponse response = client.prepareSearch("lib3").addAggregation(agg).execute().actionGet();

Terms terms = response.getAggregations().get("terms");
for (Terms.Bucket entry : terms.getBuckets()) {
System.out.println(entry.getKey() + ":" + entry.getDocCount());
}
}

// 多个filter聚合
@Test
public void testfilters() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

QueryBuilder query = QueryBuilders.termQuery("age", 20);

FiltersAggregationBuilder agg = AggregationBuilders.filters("filters",
new FiltersAggregator.KeyedFilter("changge", QueryBuilders.termQuery("interests", "lv")),
new FiltersAggregator.KeyedFilter("changge", QueryBuilders.termQuery("interests", "changge")));

SearchResponse response = client.prepareSearch("lib3").addAggregation(agg).execute().actionGet();

Terms terms = response.getAggregations().get("terms");
for (Terms.Bucket entry : terms.getBuckets()) {
System.out.println(entry.getKey() + ":" + entry.getDocCount());
}
}

// range聚合
@Test
public void testrange() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

AggregationBuilder agg = AggregationBuilders.range("range").field("age").addUnboundedTo(50).addRange(25, 50)
.addUnboundedFrom(25);
SearchResponse response = client.prepareSearch("lib3").addAggregation(agg).execute().actionGet();

Terms terms = response.getAggregations().get("range");
for (Terms.Bucket entry : terms.getBuckets()) {
System.out.println(entry.getKey() + ":" + entry.getDocCount());
}
}


//missiong聚合,统计某个字段为空的
@Test
public void testmissing() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "my-application").build();

TransportClient client = new PreBuiltTransportClient(settings)
.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.0.130"), 9300));

AggregationBuilder agg = AggregationBuilders.range("missing").field("age");
SearchResponse response = client.prepareSearch("lib3").addAggregation(agg).execute().actionGet();

Terms terms = response.getAggregations().get("missing");
for (Terms.Bucket entry : terms.getBuckets()) {
System.out.println(entry.getKey() + ":" + entry.getDocCount());
}
}



}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部