* @param sortBuilderList
* @param highlightBuilder
* @param aggregationBuilderList
* @return
*/
public ElasticSearchList getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, HighlightBuilder highlightBuilder, List aggregationBuilderList);
/**
* 通过queryBuilder分页查询
* @param index
* @param type
* @param queryBuilder
* @param pageNo
* @param pageSize
* @return
*/
public ElasticSearchPage getIndex(String index, String type, QueryBuilder queryBuilder, int pageNo, int pageSize);
/**
* 通过queryBuilder+SortBuilder分页查询
*
* @param index
* @param type
* @param queryBuilder
* @param sortBuilderList
* @param pageNo
* @param pageSize
* @return
*/
public ElasticSearchPage getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, int pageNo, int pageSize);
/**
* 通过queryBuilder+SortBuilder+highlightBuilder分页查询
*
* @param index
* @param type
* @param queryBuilder
* @param sortBuilderList
* @param highlightBuilder
* @param pageNo
* @param pageSize
* @return
*/
public ElasticSearchPage getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, HighlightBuilder highlightBuilder, int pageNo, int pageSize);
/**
* 通过queryBuilder+SortBuilder+highlightBuilder+AggregationBuilder分页查询
*
* @param index
* @param type
* @param queryBuilder
* @param sortBuilderList
* @param highlightBuilder
* @param aggregationBuilderList
* @param pageNo
* @param pageSize
* @return
*/
public ElasticSearchPage getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, HighlightBuilder highlightBuilder, List aggregationBuilderList, int pageNo, int pageSize);
/**
* 创建索引
*
* @param index
* @param type
* @param id
* @param vo
*/
public boolean createIndex(String index, String type, String id, T vo);
/**
* 批量创建索引
*
* @param index
* @param type
* @param ids
* @param list
*/
public boolean createIndex(String index, String type, List ids, List<? extends T> list);
/**
* 删除索引
*
* @param index
* @param type
* @param id
*/
public boolean removeIndex(String index, String type, String id);
/**
* 批量删除索引
*
* @param index
* @param type
* @param ids
*/
public boolean removeIndex(String index, String type, List ids);
/**
* 更新索引
*
* @param index
* @param type
* @param id
* @param vo
*/
public boolean modifyIndex(String index, String type, String id, T vo);
/**
* 批量更新索引
*
* @param index
* @param type
* @param ids
* @param list
*/
public boolean modifyIndex(String index, String type, List ids, List<? extends T> list);
}
从这个接口可以看出,我们这里实现了索引单个和批量的添加,修改,删除。还有就是我们的重头戏,索引查询,这边查询我们分为两大部分:分页、不分页,然后又具体划分为:queryBuilder(查询)、SortBuilder(排序)、highlightBuilder(高亮)、AggregationBuilder(聚合)。另外为了可以保存多个实体类到索引中,我们这边用了泛型。
2.接下来看一下接口具体实现类,我们是如何来实现这个接口的。具体代码如下所示:
package com.infun.platform.es.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.infun.platform.common.base.Constants;
import com.infun.platform.common.exception.ApiElasticSearchException;
import com.infun.platform.es.service.ElasticsearchService;
import com.infun.platform.es.util.ElasticSearchUtil;
import com.infun.platform.model.ElasticSearchList;
import com.infun.platform.model.ElasticSearchPage;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
/**
* @author linzhiqiang
* @date 2018/7/10
*/
@Service
public class ElasticsearchServiceImpl implements ElasticsearchService {
/**
* 锁标记
*/
private static final Object obj = new Object();
/**
* TransportClient连接
*/
@Autowired
private TransportClient getTransportClient;
/**
* 通过索引id查询
* @param index
* @param type
* @param id
* @return
*/
@Override
public ElasticSearchList getIndex(String index, String type, String id) {
ElasticSearchList result = new ElasticSearchList();
try {
JSONArray jsonArray = new JSONArray();
SearchRequestBuilder searchRequestBuilder = getTransportClient.prepareSearch(index).setTypes(type);
searchRequestBuilder.setQuery(QueryBuilders.idsQuery().addIds(id));
SearchResponse searchResponse = searchRequestBuilder.setExplain(true).execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
for (SearchHit searchHit : searchHits) {
Map<String, Object> source = searchHit.getSourceAsMap();
jsonArray.add(JSONObject.parseObject(JSON.toJSONString(source)));
}
JSONObject jsonObject = ElasticSearchUtil.handleAggregations(searchResponse.getAggregations());
result.setList(jsonArray);
result.setAggregation(jsonObject);
} catch (Exception e) {
throw new ApiElasticSearchException(“【查询索引异常】查询索引异常,失败原因:” + e.getMessage());
}
return result;
}
/**
* 通过queryBuilder查询
*
* @param index
* @param type
* @param queryBuilder
* @return
*/
@Override
public ElasticSearchList getIndex(String index, String type, QueryBuilder queryBuilder) {
ElasticSearchList result = new ElasticSearchList();
try {
JSONArray jsonArray = new JSONArray();
SearchRequestBuilder searchRequestBuilder = getTransportClient.prepareSearch(index).setTypes(type);
if (null != queryBuilder) {
searchRequestBuilder.setQuery(queryBuilder);
}
SearchResponse searchResponse = searchRequestBuilder.setExplain(true).execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
for (SearchHit searchHit : searchHits) {
Map<String, Object> source = searchHit.getSourceAsMap();
jsonArray.add(JSONObject.parseObject(JSON.toJSONString(source)));
}
result.setList(jsonArray);
} catch (Exception e) {
throw new ApiElasticSearchException(“【查询索引】” + e.getMessage());
}
return result;
}
/**
* 通过queryBuilder+SortBuilder查询
*
* @param index
* @param type
* @param queryBuilder
* @param sortBuilderList
* @return
*/
@Override
public ElasticSearchList getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList) {
ElasticSearchList result = new ElasticSearchList();
try {
JSONArray jsonArray = new JSONArray();
SearchRequestBuilder searchRequestBuilder = getTransportClient.prepareSearch(index).setTypes(type);
if (null != queryBuilder) {
searchRequestBuilder.setQuery(queryBuilder);
}
if (null != sortBuilderList) {
for (SortBuilder<?> sortBuilder : sortBuilderList) {
searchRequestBuilder.addSort(sortBuilder);
}
}
SearchResponse searchResponse = searchRequestBuilder.setExplain(true).execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
for (SearchHit searchHit : searchHits) {
Map<String, Object> source = searchHit.getSourceAsMap();
jsonArray.add(JSONObject.parseObject(JSON.toJSONString(source)));
}
result.setList(jsonArray);
} catch (Exception e) {
throw new ApiElasticSearchException(“【查询索引】” + e.getMessage());
}
return result;
}
/**
* 通过queryBuilder+SortBuilder+highlightBuilder查询
*
* @param index
* @param type
* @param queryBuilder
* @param sortBuilderList
* @param highlightBuilder
* @return
*/
@Override
public ElasticSearchList getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, HighlightBuilder highlightBuilder) {
ElasticSearchList result = new ElasticSearchList();
try {
SearchRequestBuilder searchRequestBuilder = getTransportClient.prepareSearch(index).setTypes(type);
if (null != queryBuilder) {
searchRequestBuilder.setQuery(queryBuilder);
}
if (null != sortBuilderList) {
for (SortBuilder<?> sortBuilder : sortBuilderList) {
searchRequestBuilder.addSort(sortBuilder);
}
}
if (null != highlightBuilder) {
searchRequestBuilder.highlighter(highlightBuilder);
}
SearchResponse searchResponse = searchRequestBuilder.setExplain(true).execute().actionGet();
// 处理高亮
result.setList(ElasticSearchUtil.handleHighlight(searchResponse.getHits()));
} catch (Exception e) {
throw new ApiElasticSearchException(“【查询索引】” + e.getMessage());
}
return result;
}
/**
* 通过queryBuilder+SortBuilder+highlightBuilder+AggregationBuilder查询
*
* @param index
* @param type
* @param queryBuilder
* @param sortBuilderList
* @param highlightBuilder
* @param aggregationBuilderList
* @return
*/
@Override
public ElasticSearchList getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, HighlightBuilder highlightBuilder, List aggregationBuilderList) {
ElasticSearchList result = new ElasticSearchList();
try {
SearchRequestBuilder searchRequestBuilder = getTransportClient.prepareSearch(index).setTypes(type);
if (null != queryBuilder) {
searchRequestBuilder.setQuery(queryBuilder);
}
if (null != sortBuilderList) {
for (SortBuilder<?> sortBuilder : sortBuilderList) {
searchRequestBuilder.addSort(sortBuilder);
}
}
if (null != highlightBuilder) {
searchRequestBuilder.highlighter(highlightBuilder);
}
if (null != aggregationBuilderList) {
for (AggregationBuilder aggregationBuilder : aggregationBuilderList) {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
SearchResponse searchResponse = searchRequestBuilder.setExplain(true).execute().actionGet();
// 处理高亮
result.setList(ElasticSearchUtil.handleHighlight(searchResponse.getHits()));
// 处理聚合
result.setAggregation(ElasticSearchUtil.handleAggregations(searchResponse.getAggregations()));
} catch (Exception e) {
throw new ApiElasticSearchException(“【查询索引】” + e.getMessage());
}
return result;
}
/**
* 通过queryBuilder分页查询
*
* @param index
* @param type
* @param queryBuilder
* @param pageNo
* @param pageSize
* @return
*/
@Override
public ElasticSearchPage getIndex(String index, String type, QueryBuilder queryBuilder, int pageNo, int pageSize) {
ElasticSearchPage result = new ElasticSearchPage();
try {
SearchRequestBuilder searchRequestBuilder = getTransportClient.prepareSearch(index).setTypes(type);
if (null != queryBuilder) {
searchRequestBuilder.setQuery(queryBuilder);
}
SearchResponse searchResponse = searchRequestBuilder.setFrom((pageNo - 1) * pageSize).setSize(pageSize).setExplain(true).execute().actionGet();
result = new ElasticSearchPage(searchResponse.getHits(), pageNo, pageSize);
} catch (Exception e) {
throw new ApiElasticSearchException(“【分页查询索引】” + e.getMessage());
}
return result;
}
/**
* 通过queryBuilder+SortBuilder分页查询
*
* @param index
* @param type
* @param queryBuilder
* @param sortBuilderList
* @param pageNo
* @param pageSize
* @return
*/
@Override
public ElasticSearchPage getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, int pageNo, int pageSize) {
ElasticSearchPage result = new ElasticSearchPage();
try {
SearchRequestBuilder searchRequestBuilder = getTransportClient.prepareSearch(index).setTypes(type);
if (null != queryBuilder) {
searchRequestBuilder.setQuery(queryBuilder);
}
if (null != sortBuilderList) {
for (SortBuilder<?> sortBuilder : sortBuilderList) {
searchRequestBuilder.addSort(sortBuilder);
}
}
SearchResponse searchResponse = searchRequestBuilder.setFrom((pageNo - 1) * pageSize).setSize(pageSize).setExplain(true).execute().actionGet();
result = new ElasticSearchPage(searchResponse.getHits(), pageNo, pageSize);
} catch (Exception e) {
throw new ApiElasticSearchException(“【分页查询索引】” + e.getMessage());
}
return result;
}
/**
* 通过queryBuilder+SortBuilder+highlightBuilder分页查询
*
* @param index
* @param type
* @param queryBuilder
* @param sortBuilderList
* @param highlightBuilder
* @param pageNo
* @param pageSize
* @return
*/
@Override
public ElasticSearchPage getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, HighlightBuilder highlightBuilder, int pageNo, int pageSize) {
ElasticSearchPage result = new ElasticSearchPage();
try {
SearchRequestBuilder searchRequestBuilder = getTransportClient.prepareSearch(index).setTypes(type);
if (null != queryBuilder) {
searchRequestBuilder.setQuery(queryBuilder);
}
if (null != sortBuilderList) {
for (SortBuilder<?> sortBuilder : sortBuilderList) {
searchRequestBuilder.addSort(sortBuilder);
}
}
if (null != highlightBuilder) {
searchRequestBuilder.highlighter(highlightBuilder);
}
SearchResponse searchResponse = searchRequestBuilder.setFrom((pageNo - 1) * pageSize).setSize(pageSize).setExplain(true).execute().actionGet();
result = new ElasticSearchPage(searchResponse.getHits(), pageNo, pageSize);
// 处理高亮
result.setList(ElasticSearchUtil.handleHighlight(searchResponse.getHits()));
} catch (Exception e) {
throw new ApiElasticSearchException(“【分页查询索引】” + e.getMessage());
}
return result;
}
/**
* 通过queryBuilder+SortBuilder+highlightBuilder+AggregationBuilder分页查询
*
* @param index
* @param type
* @param queryBuilder
* @param sortBuilderList
* @param highlightBuilder
* @param aggregationBuilderList
* @param pageNo
* @param pageSize
* @return
*/
@Override
public ElasticSearchPage getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, HighlightBuilder highlightBuilder, List aggregationBuilderList, int pageNo, int pageSize) {
ElasticSearchPage result = new ElasticSearchPage();
try {
SearchRequestBuilder searchRequestBuilder = getTransportClient.prepareSearch(index).setTypes(type);
if (null != queryBuilder) {
searchRequestBuilder.setQuery(queryBuilder);
}
if (null != sortBuilderList) {
for (SortBuilder<?> sortBuilder : sortBuilderList) {
searchRequestBuilder.addSort(sortBuilder);
}
}
if (null != highlightBuilder) {
searchRequestBuilder.highlighter(highlightBuilder);
}
if (null != aggregationBuilderList) {
for (AggregationBuilder aggregationBuilder : aggregationBuilderList) {
searchRequestBuilder.addAggregation(aggregationBuilder);
}
}
SearchResponse searchResponse = searchRequestBuilder.setFrom((pageNo - 1) * pageSize).setSize(pageSize).setExplain(true).execute().actionGet();
result = new ElasticSearchPage(searchResponse.getHits(), pageNo, pageSize);
// 处理高亮
result.setList(ElasticSearchUtil.handleHighlight(searchResponse.getHits()));
// 处理聚合
result.setAggregation(ElasticSearchUtil.handleAggregations(searchResponse.getAggregations()));
} catch (Exception e) {
throw new ApiElasticSearchException(“【分页查询索引】” + e.getMessage());
}
return result;
}
@Override
public boolean createIndex(String index, String type, String id, T vo){
synchronized (obj) {
try {
XContentBuilder xContentBuilder = jsonBuilder();
if (null != vo) {
xContentBuilder = ElasticSearchUtil.ObjectToJXContentBuilder(xContentBuilder, vo);
}
IndexRequestBuilder indexRequestBuilder = getTransportClient.prepareIndex(index, type, id).setSource(xContentBuilder);
IndexResponse indexResponse = indexRequestBuilder.get();
if (Constants.ELASTICSEARCH_SUCCESS.equals(indexResponse.status())) {
return true;
}
} catch (Exception e) {
throw new ApiElasticSearchException(“【创建索引】创建索引失败,失败原因:”+e.getMessage());
}
return true;
}
}
@Override
public boolean createIndex(String index, String type, List ids, List<? extends T> list) {
synchronized (obj) {
try {
if (null != ids && null != list && ids.size() == list.size()) {
BulkRequestBuilder bulkRequestBuilder = getTransportClient.prepareBulk();
for (int i = 0; i < ids.size(); i++) {
XContentBuilder xContentBuilder = ElasticSearchUtil.ObjectToJXContentBuilder(jsonBuilder(), list.get(i));
IndexRequestBuilder indexRequestBuilder = getTransportClient.prepareIndex(index, type, ids.get(i)).setSource(xContentBuilder);
bulkRequestBuilder.add(indexRequestBuilder);
}
BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet();
if (bulkResponse.hasFailures()) {
throw new ApiElasticSearchException(“【批量创建索引】量创建索引失败,失败原因:” + bulkResponse.buildFailureMessage());
} else {
if (Constants.ELASTICSEARCH_SUCCESS.equals(bulkResponse.status())) {
return true;
}
}
} else {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
文章中涉及到的知识点我都已经整理成了资料,录制了视频供大家下载学习,诚意满满,希望可以帮助在这个行业发展的朋友,在论坛博客等地方少花些时间找资料,把有限的时间,真正花在学习上,所以我把这些资料,分享出来。相信对于已经工作和遇到技术瓶颈的朋友们,在这份资料中一定都有你需要的内容。文章来源:https://www.toymoban.com/news/detail-850228.html
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
age());
} else {
if (Constants.ELASTICSEARCH_SUCCESS.equals(bulkResponse.status())) {
return true;
}
}
} else {
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。[外链图片转存中…(img-SDllXc04-1712571518531)]
[外链图片转存中…(img-2YGeKj04-1712571518532)]
[外链图片转存中…(img-WeA5GQAv-1712571518532)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
最后
文章中涉及到的知识点我都已经整理成了资料,录制了视频供大家下载学习,诚意满满,希望可以帮助在这个行业发展的朋友,在论坛博客等地方少花些时间找资料,把有限的时间,真正花在学习上,所以我把这些资料,分享出来。相信对于已经工作和遇到技术瓶颈的朋友们,在这份资料中一定都有你需要的内容。
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!文章来源地址https://www.toymoban.com/news/detail-850228.html
到了这里,关于elasticsearch实践之代码结构设计的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!