Elasticsearch java api操作(二)(Java High Level Rest Client)
一、说明:
一、Elasticsearch提供了两个JAVA REST Client版本:
1、java low level rest client:
低级别的rest客户端,通过http与集群交互,用户需自己编组请求JSON串,及解析响应JSON串。兼容所有Elasticsearch版本。
特点:maven引入
使用介绍: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html
2、java high rest client:
高级别的REST客户端,基于低级别的REST客户端,增加了编组请求JSON串、解析响应JSON串等相关API,使用的版本需要保存和ES服务一致的版本,否则会有版本问题。
从6.0.0开始加入的,目的是以java面向对象的方式进行请求、响应处理。
每个API支持 同步、异步 两种方式,同步方法之间返回一个结果对象。异步的方法以async为后缀,通过listener参数来通知结果。高级java resy客户端依赖Elasticsearch core pproject
兼容性说明:
依赖jdk1.8和Elasticsearch core project
二、Java Low Level Rest Client的使用
1、JAVA High Level Rest Client maven集成
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.1</version>
</dependency>
版本:
Elasticsearch 6.3.1
pom文件:
<dependencies>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.7</version>
</dependency> <dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>0.9</version>
</dependency>
</dependencies>
给定集群的多个节点地址,将客户端负载均衡地向这个节点地址集发请求:
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient; /**
* @Author: xiaolaotou
* @Date: 2019/4/28
*/
public class ClientUtil {
public static RestHighLevelClient getClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("192.168.200.100",9200,"http"),
new HttpHost("192.168.200.101",9200,"http"),
new HttpHost("192.168.200.102",9200,"http"))
);
return client;
}
}
2、创建索引
/**
* 创建索引
* @throws Exception
*/
public static void createindex()throws Exception{
RestHighLevelClient client = ClientUtil.getClient();
//1、创建索引,设置索引名
CreateIndexRequest request = new CreateIndexRequest("mess");
//2、设置索引的settings
request.settings(Settings.builder()
.put("index.number_of_shards",3)//分片数
.put("index.number_of_replicas",1)//副本数
.put("analysis.analyzer.default.tokenizer","ik_smart"))//默认分词器
;
//3、设置索引的mappings
request.mapping("_doc",
" {\n" +
" \"_doc\": {\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }",
XContentType.JSON);
//4、设置索引的别名
request.alias(new Alias("mmm"));
//5、发送方式
// //5、1采用同步方式发送请求
CreateIndexResponse response = client.indices().create(request);
// 处理响应
boolean acknowledged = response.isAcknowledged();
boolean shardsAcknowledged = response.isShardsAcknowledged();
System.out.println("acknowledged = " + acknowledged);
System.out.println("shardsAcknowledged = " + shardsAcknowledged);
client.close();
//5、2采用异步方法发送请求
// ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() {
// @Override
// public void onResponse(CreateIndexResponse createIndexResponse) {
// //处理响应
// boolean acknowledged = createIndexResponse.isAcknowledged();
// boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
// System.out.println("shardsAcknowledged = " + shardsAcknowledged);
// System.out.println("acknowledged = " + acknowledged);
// }
// @Override
// public void onFailure(Exception e) {
// System.out.println("创建索引异常 = " + e.getMessage());
// }
// };
// client.indices().createAsync(request,listener);
// client.close();
}
3、index document
public static void indexDocument() throws IOException {
RestHighLevelClient client = ClientUtil.getClient();
//1、创建索引请求
IndexRequest request = new IndexRequest("mess", "_doc");
//2、准备文档数据
//方式一:直接给JSON串
String jsonString = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-31\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
request.source(jsonString, XContentType.JSON); //方式二:以map对象来表示文档
// HashMap<Object, Object> jsonMap = new HashMap<>();
// jsonMap.put("user", "kimchy");
// jsonMap.put("postDate", new Date());
// jsonMap.put("message", "trying out Elasticsearch");
// request.source(jsonMap); //方式三:用XContentBuilder来构建文档
// XContentBuilder builder = XContentFactory.jsonBuilder();
// builder.startObject();
// {
// builder.field("user", "kimchy");
// builder.field("postDate", new Date());
// builder.field("message", "trying out Elasticsearch");
// }
// builder.endObject();
// request.source(builder); // //方式四:直接用key-value对给出
// request.source("user", "kimchy",
// "postDate", new Date(),
// "message", "trying out Elasticsearch"); //3、其他的一些可选设置
/*
request.routing("routing"); //设置routing值
request.timeout(TimeValue.timeValueSeconds(1)); //设置主分片等待时长
request.setRefreshPolicy("wait_for"); //设置重刷新策略
request.version(2); //设置版本号
request.opType(DocWriteRequest.OpType.CREATE); //操作类别
*/
//4、发送请求-->同步方式
IndexResponse indexResponse = null;
try {
// 同步方式
indexResponse = client.index(request);
} catch(ElasticsearchException e) {
// 捕获,并处理异常
//判断是否版本冲突、create但文档已存在冲突
if (e.status() == RestStatus.CONFLICT) {
logger.error("冲突了,请在此写冲突处理逻辑!\n" + e.getDetailedMessage());
} logger.error("索引异常", e);
}
//处理响应
if(indexResponse != null) {
String index = indexResponse.getIndex();
String type = indexResponse.getType();
String id = indexResponse.getId();
long version = indexResponse.getVersion();
if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
System.out.println("新增文档成功,处理逻辑代码写到这里。");
} else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
System.out.println("修改文档成功,处理逻辑代码写到这里。");
}
// 分片处理信息
ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo();
if (shardInfo.getTotal() != shardInfo.getSuccessful()) { }
// 如果有分片副本失败,可以获得失败原因信息
if (shardInfo.getFailed() > 0) {
for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) {
String reason = failure.reason();
System.out.println("副本失败原因:" + reason);
}
}
}
client.close(); //异步方式
// ActionListener<IndexResponse> listener = new ActionListener<IndexResponse>(){
// @Override
// public void onResponse(IndexResponse indexResponse) {
// //5、处理响应
// if (indexResponse != null) {
// String index = indexResponse.getIndex();
// String type = indexResponse.getType();
// String id = indexResponse.getId();
// long version = indexResponse.getVersion();
// if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
// System.out.println("新增文档成功,处理逻辑代码写到这里。");
// } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) {
// System.out.println("修改文档成功,处理逻辑代码写到这里。");
// }
// }
// }
// @Override
// public void onFailure(Exception e) {
// System.out.println("创建索引异常:" + e.getMessage());
// }
// };
// client.indexAsync(request,listener);
// client.close();
}
4、get document(获取文档数据)
/**
* 获取文档数据
* @throws IOException
*/
public static void getDocument() throws IOException {
RestHighLevelClient client = ClientUtil.getClient();
//1、创建获取文档请求
GetRequest request = new GetRequest("mess","_doc","bcUli2oBM0IXAkB-Yvpr");
//2、可选的设置
// request.routing("routing");
// request.version(2);
// request.fetchSourceContext(new FetchSourceContext(false));//是否获取_source字段 //选择返回的字段
String[] includes=new String[]{"message","postDate"};
String[] excludes = Strings.EMPTY_ARRAY;
FetchSourceContext fetchSourceContext =
new FetchSourceContext(true, includes, excludes);
request.fetchSourceContext(fetchSourceContext);
// 取stored字段
/*request.storedFields("message");
GetResponse getResponse = client.get(request);
String message = getResponse.getField("message").getValue();*/
//发送请求
GetResponse getResponse = null;
try {
// 同步请求
getResponse = client.get(request);
System.out.println("getResponse = " + getResponse);
} catch (ElasticsearchException e) {
if (e.status() == RestStatus.NOT_FOUND) {
logger.error("没有找到该id的文档" );
}
if (e.status() == RestStatus.CONFLICT) {
logger.error("获取时版本冲突了,请在此写冲突处理逻辑!" );
}
logger.error("获取文档异常", e);
} catch (IOException e) {
e.printStackTrace();
}
//处理响应
if(getResponse != null) {
String index = getResponse.getIndex();
String type = getResponse.getType();
String id = getResponse.getId();
if (getResponse.isExists()) { // 文档存在
long version = getResponse.getVersion();
String sourceAsString = getResponse.getSourceAsString(); //结果取成 String
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap(); // 结果取成Map
byte[] sourceAsBytes = getResponse.getSourceAsBytes(); //结果取成字节数组 logger.info("index:" + index + " type:" + type + " id:" + id);
logger.info(sourceAsString); } else {
logger.error("没有找到该id的文档" );
}
}
client.close();
5、bulk批量索引
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; /**
* @Author: xiaolaotou
* @Date: 2019/5/6
*/
public class BulkIndex {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = ClientUtil.getClient();
//1、创建批量操作请求
BulkRequest request = new BulkRequest();
request.add(new IndexRequest("mess","_doc","1")
.source(XContentType.JSON,"field","foo"));
request.add(new IndexRequest("mess","_doc","2")
.source(XContentType.JSON,"field","bar"));
request.add(new IndexRequest("mess","_doc","3")
.source(XContentType.JSON,"field","baz"));
// 2、可选的设置
/*
request.timeout("2m");
request.setRefreshPolicy("wait_for");
request.waitForActiveShards(2);
*/
//发送请求--》同步方式
BulkResponse bulkResponse = client.bulk(request);
//处理响应
if(bulkResponse != null) {
for (BulkItemResponse bulkItemResponse : bulkResponse) {
DocWriteResponse itemResponse = bulkItemResponse.getResponse(); if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX
|| bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {
IndexResponse indexResponse = (IndexResponse) itemResponse;
//TODO 新增成功的处理 } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {
UpdateResponse updateResponse = (UpdateResponse) itemResponse;
//TODO 修改成功的处理 } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {
DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
//TODO 删除成功的处理
}
}
}
client.close();
}
}
6、Search查询数据
package com.study.es_hrset_client; import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.ShardSearchFailure;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.term.TermSuggestion; /**
*
* @author xiaolaotou
* @date 2019/5/6
*
*/
public class SearchDemo { private static Logger logger = LogManager.getRootLogger(); public static void main(String[] args) {
try (RestHighLevelClient client = InitDemo.getClient();) { // 1、创建search请求
//SearchRequest searchRequest = new SearchRequest();
SearchRequest searchRequest = new SearchRequest("bank");
searchRequest.types("_doc"); // 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //构造QueryBuilder
/*QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy")
.fuzziness(Fuzziness.AUTO)
.prefixLength(3)
.maxExpansions(10);
sourceBuilder.query(matchQueryBuilder);*/ sourceBuilder.query(QueryBuilders.termQuery("age", 24));
sourceBuilder.from(0);
sourceBuilder.size(10);
sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //是否返回_source字段
//sourceBuilder.fetchSource(false); //设置返回哪些字段
/*String[] includeFields = new String[] {"title", "user", "innerObject.*"};
String[] excludeFields = new String[] {"_type"};
sourceBuilder.fetchSource(includeFields, excludeFields);*/ //指定排序
//sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
//sourceBuilder.sort(new FieldSortBuilder("_uid").order(SortOrder.ASC)); // 设置返回 profile
//sourceBuilder.profile(true); //将请求体加入到请求中
searchRequest.source(sourceBuilder); // 可选的设置
//searchRequest.routing("routing"); // 高亮设置
/*
HighlightBuilder highlightBuilder = new HighlightBuilder();
HighlightBuilder.Field highlightTitle =
new HighlightBuilder.Field("title");
highlightTitle.highlighterType("unified");
highlightBuilder.field(highlightTitle);
HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
highlightBuilder.field(highlightUser);
sourceBuilder.highlighter(highlightBuilder);*/ //加入聚合
/*TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company")
.field("company.keyword");
aggregation.subAggregation(AggregationBuilders.avg("average_age")
.field("age"));
sourceBuilder.aggregation(aggregation);*/ //做查询建议
/*SuggestionBuilder termSuggestionBuilder =
SuggestBuilders.termSuggestion("user").text("kmichy");
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
sourceBuilder.suggest(suggestBuilder);*/ //3、发送请求
SearchResponse searchResponse = client.search(searchRequest); //4、处理响应
//搜索结果状态信息
RestStatus status = searchResponse.status();
TimeValue took = searchResponse.getTook();
Boolean terminatedEarly = searchResponse.isTerminatedEarly();
boolean timedOut = searchResponse.isTimedOut(); //分片搜索情况
int totalShards = searchResponse.getTotalShards();
int successfulShards = searchResponse.getSuccessfulShards();
int failedShards = searchResponse.getFailedShards();
for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
// failures should be handled here
} //处理搜索命中文档结果
SearchHits hits = searchResponse.getHits(); long totalHits = hits.getTotalHits();
float maxScore = hits.getMaxScore(); SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
// do something with the SearchHit String index = hit.getIndex();
String type = hit.getType();
String id = hit.getId();
float score = hit.getScore(); //取_source字段值
String sourceAsString = hit.getSourceAsString(); //取成json串
Map<String, Object> sourceAsMap = hit.getSourceAsMap(); // 取成map对象
//从map中取字段值
/*
String documentTitle = (String) sourceAsMap.get("title");
List<Object> users = (List<Object>) sourceAsMap.get("user");
Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject");
*/
logger.info("index:" + index + " type:" + type + " id:" + id);
logger.info(sourceAsString); //取高亮结果
/*Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField highlight = highlightFields.get("title");
Text[] fragments = highlight.fragments();
String fragmentString = fragments[0].string();*/
} // 获取聚合结果
/*
Aggregations aggregations = searchResponse.getAggregations();
Terms byCompanyAggregation = aggregations.get("by_company");
Bucket elasticBucket = byCompanyAggregation.getBucketByKey("Elastic");
Avg averageAge = elasticBucket.getAggregations().get("average_age");
double avg = averageAge.getValue();
*/ // 获取建议结果
/*Suggest suggest = searchResponse.getSuggest();
TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
for (TermSuggestion.Entry.Option option : entry) {
String suggestText = option.getText().string();
}
}
*/ //异步方式发送获查询请求
/*
ActionListener<SearchResponse> listener = new ActionListener<SearchResponse>() {
@Override
public void onResponse(SearchResponse getResponse) {
//结果获取
} @Override
public void onFailure(Exception e) {
//失败处理
}
};
client.searchAsync(searchRequest, listener);
*/ } catch (IOException e) {
logger.error(e);
}
}
}
7、highlight 高亮
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import java.io.IOException;
import java.util.Map; /**
* @Author: xiaolaotou
* @Date: 2019/5/6
*/
public class highlight {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = ClientUtil.getClient();
//1、创建请求
SearchRequest searchRequest = new SearchRequest("mess");
//2、用SearchSourceBuilder来构造查询请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//构造QueryBuilder
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy");
sourceBuilder.query(matchQueryBuilder);
//高亮设置
HighlightBuilder highlightBuilder = new HighlightBuilder();
//不同字段可有不同设置,如不同标签
HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("user");
highlightTitle.preTags("<strong>").postTags("</strong>");
highlightBuilder.field(highlightTitle);
sourceBuilder.highlighter(highlightBuilder);
searchRequest.source(sourceBuilder);
//3、发送请求
SearchResponse searchResponse = client.search(searchRequest);
//4、处理响应
if (RestStatus.OK.equals(searchResponse.status())) {
//处理搜索命中文档结果
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
//取_source字段值
Map<String, Object> sourceAsMap = hit.getSourceAsMap(); // 取成map对象
//从map中取字段值
/*String title = (String) sourceAsMap.get("title");
String content = (String) sourceAsMap.get("content"); */
System.out.println("sourceAsMap = " + sourceAsMap);
//取高亮结果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField highlight = highlightFields.get("user");
if (highlight != null) {
Text[] fragments = highlight.fragments(); //多值的字段会有多个值
if (fragments != null) {
String fragmentString = fragments[0].string();
System.out.println("user highlight = " + fragmentString);
//可用高亮字符串替换上面sourceAsMap中的对应字段返回到上一级调用
//sourceAsMap.put("title", fragmentString);
}
}
}
}
client.close();
}
}
8、查询建议
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.SuggestionBuilder;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.elasticsearch.search.suggest.term.TermSuggestion;
import org.elasticsearch.search.suggest.term.TermSuggestionBuilder;
import java.io.IOException; /**
* @Author: xiaolaotou
* @Date: 2019/5/6
*/
public class SearchSuggestion {
public static void termSuggest() throws IOException {
RestHighLevelClient client = ClientUtil.getClient();
//1、创建search请求
SearchRequest searchRequest = new SearchRequest("mess");
// 2、用SearchSourceBuilder来构造查询请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//做查询建议
//词项建议
TermSuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("kimchy");
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("user",termSuggestionBuilder);
sourceBuilder.suggest(suggestBuilder); //发送请求
SearchResponse searchResponse = client.search(searchRequest);
//处理响应
//搜索结果状态信息
if(RestStatus.OK.equals(searchResponse.status())) {
// 获取建议结果
Suggest suggest = searchResponse.getSuggest();
TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
System.out.println("entry = " + entry.getText().string());
for (TermSuggestion.Entry.Option option : entry) {
String suggestText = option.getText().string();
System.out.println("suggest option = " + suggestText);
}
}
}
} /**
* 自动补全,根据用户的输入联想到可能的词或者短语
*/
public static void completionSuggester() throws IOException {
RestHighLevelClient client = ClientUtil.getClient();
//1、创建请求
SearchRequest searchRequest = new SearchRequest("mess");
//2、用SearchSourceBuilder来构造查询请求体
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
SuggestionBuilder termSuggestionBuilder =
SuggestBuilders.completionSuggestion("suggest").prefix("lucene s")
.skipDuplicates(true);
SuggestBuilder suggestBuilder = new SuggestBuilder();
suggestBuilder.addSuggestion("song-suggest", termSuggestionBuilder);
sourceBuilder.suggest(suggestBuilder); searchRequest.source(sourceBuilder); //3、发送请求
SearchResponse searchResponse = client.search(searchRequest);
//4、处理响应
//搜索结果状态信息
if(RestStatus.OK.equals(searchResponse.status())) {
// 获取建议结果
Suggest suggest = searchResponse.getSuggest();
CompletionSuggestion termSuggestion = suggest.getSuggestion("song-suggest");
for (CompletionSuggestion.Entry entry : termSuggestion.getEntries()) {
System.out.println("text: " + entry.getText().string());
for (CompletionSuggestion.Entry.Option option : entry) {
String suggestText = option.getText().string();
System.out.println("suggest option = " + suggestText);
}
}
} }
public static void main(String[] args) throws IOException {
termSuggest();
// completionSuggester();
}
}
9、aggregation 聚合分析
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.BucketOrder;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.avg.Avg;
import org.elasticsearch.search.builder.SearchSourceBuilder; public class AggregationDemo { private static Logger logger = LogManager.getRootLogger(); public static void main(String[] args) {
try (RestHighLevelClient client = ClientUtil.getClient();) { // 1、创建search请求
//SearchRequest searchRequest = new SearchRequest();
SearchRequest searchRequest = new SearchRequest("bank"); // 2、用SearchSourceBuilder来构造查询请求体 ,请仔细查看它的方法,构造各种查询的方法都在这。
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.size(0); //加入聚合
//字段值项分组聚合
TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_age")
.field("age").order(BucketOrder.aggregation("average_balance", true));
//计算每组的平均balance指标
aggregation.subAggregation(AggregationBuilders.avg("average_balance")
.field("balance"));
sourceBuilder.aggregation(aggregation); searchRequest.source(sourceBuilder); //3、发送请求
SearchResponse searchResponse = client.search(searchRequest); //4、处理响应
//搜索结果状态信息
if(RestStatus.OK.equals(searchResponse.status())) {
// 获取聚合结果
Aggregations aggregations = searchResponse.getAggregations();
Terms byAgeAggregation = aggregations.get("by_age");
logger.info("aggregation by_age 结果");
logger.info("docCountError: " + byAgeAggregation.getDocCountError());
logger.info("sumOfOtherDocCounts: " + byAgeAggregation.getSumOfOtherDocCounts());
logger.info("------------------------------------");
for(Bucket buck : byAgeAggregation.getBuckets()) {
logger.info("key: " + buck.getKeyAsNumber());
logger.info("docCount: " + buck.getDocCount());
//logger.info("docCountError: " + buck.getDocCountError());
//取子聚合
Avg averageBalance = buck.getAggregations().get("average_balance"); logger.info("average_balance: " + averageBalance.getValue());
logger.info("------------------------------------");
}
//直接用key 来去分组
/*Bucket elasticBucket = byCompanyAggregation.getBucketByKey("24");
Avg averageAge = elasticBucket.getAggregations().get("average_age");
double avg = averageAge.getValue();*/ } } catch (Exception e) {
logger.error(e);
}
}
}
Elasticsearch java api操作(二)(Java High Level Rest Client)的更多相关文章
- MongoDB Java API操作很全的整理
MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写,一般生产上建议以共享分片的形式来部署. 但是MongoDB官方也提供了其它语言的客户端操作API.如下图所示: 提供了C.C++ ...
- hive-通过Java API操作
通过Java API操作hive,算是测试hive第三种对外接口 测试hive 服务启动 package org.admln.hive; import java.sql.SQLException; i ...
- hadoop2-HBase的Java API操作
Hbase提供了丰富的Java API,以及线程池操作,下面我用线程池来展示一下使用Java API操作Hbase. 项目结构如下: 我使用的Hbase的版本是 hbase-0.98.9-hadoop ...
- 使用Java API操作HDFS文件系统
使用Junit封装HFDS import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.*; import org ...
- Kafka系列三 java API操作
使用java API操作kafka 1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xs ...
- Hadoop之HDFS(三)HDFS的JAVA API操作
HDFS的JAVA API操作 HDFS 在生产应用中主要是客户端的开发,其核心步骤是从 HDFS 提供的 api中构造一个 HDFS 的访问客户端对象,然后通过该客户端对象操作(增删改查)HDFS ...
- zookeeper的java api操作
zookeeper的java api操作 创建会话: Zookeeper(String connectString,int sessionTimeout,Watcher watcher) Zookee ...
- java api操作
java api操作 导入开发包 将hbase安装包中lib下包导入java项目 创建表 Configuration conf = HBaseConfiguration.create(); c ...
- HDFS 05 - HDFS 常用的 Java API 操作
目录 0 - 配置 Hadoop 环境(Windows系统) 1 - 导入 Maven 依赖 2 - 常用类介绍 3 - 常见 API 操作 3.1 获取文件系统(重要) 3.2 创建目录.写入文件 ...
随机推荐
- PyQt(Python+Qt)学习随笔:QListView的resizeMode属性
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QListView的resizeMode属性用于控制调整视图大小时是否再次排列视图中的数据项,其类型 ...
- Python学习随笔:PyCharm的错误检测使用及调整配置减少错误数量
老猿使用PyCharm有将近一个月了,发现PyCharm并不能很好的完成语法检查,有时运行时突然终止,仔细核查却发现是基本的语法错误,不过有次无意中移动鼠标到代码最右边的边框时发现其实PyCharm有 ...
- 《Eroico》关卡与操作设计
操作设计: 没有给明操作教程,操作全靠蒙,只有改建的位置可以看到. 但游戏的难度并没有给玩家适应操作感,随着难度提升怪物血量增厚,但怪物并没有僵直英雄却有僵直.第一个小猫妖便给了玩家一个痛击. 方向键 ...
- 实现一个类型判断函数,需要鉴别出基本类型、function、null、NaN、数组、对象?
只需要鉴别这些类型那么使用typeof即可,要鉴别null先判断双等判断是否为null,之后使用typeof判断,如果是obejct的话,再用Array.isArray判断是否为数组,如果是数字再使用 ...
- P5785 [SDOI2012]任务安排
本题解用于本蒟蒻加深算法印象,也欢迎大家阅读 本篇题解将分为四块,一步一步地讲解本题, Part 1: O(n^3) \(n^3\) 算法应该非常的显然,我们设 \(f_{i,j}\) 为到 \(i\ ...
- linux 上安装部署python
一般在linux中使用python 需要安装pyenv 进行版本控制 因为linux6.9自带的Python是2.6的 同时很多命令都是基于2.6开发的 所以系统环境不能改 我们要开发 只能用pyen ...
- sql server如何把退款总金额拆分到尽量少的多个订单中
一.问题 原来有三个充值订单,现在要退款450元,如何分配才能让本次退款涉及的充值订单数量最少?具体数据参考下图: 二.解决方案 Step 1:对可退金额进行降序排列,以便优先使用可退金额比较大的订单 ...
- sqli-labs less-24(二次注入)
less-24 原理: 在网站处理用户提交的数据的时候,只是将某些敏感字符进行了转义.因而使得用户第一次提交的时候不会被当做代码执行.但是这些数据存入数据库的时候却没有转义,而网站程序默认数据库中的数 ...
- Eclipse的基本设置与使用
下载完eclipse后,还不能立即来写代码,需要完成一些必要的设置 设置 1.对整个工作区设置编码格式 选择菜单栏中的"Window"选项,然后选择"Preference ...
- 2020 史上最全IDEA插件总结
最喜欢的一句话: 1.01的365次方=37.78343433289 >>>1 0.99的365次方= 0.02551796445229, 每天进步一点点的目标,贵在坚持 IDEA ...