之前说过我们项目要集成es搜索引擎模块,这几天一直在做集成个事情。这过程中遇到过很多的坑,也尝试过很多的解决办法,今天给大家分享一下elasticsearch的实践过程。首先我们的接口要怎么命名、接口的字段要设置多少、分别是什么类型、实现类中要如何实现接口、工具类要定义哪一些的方法。
1.首先我们先看看接口中要定义哪些通用方法,如下所示:
package com.infun.platform.es.service; import com.infun.platform.model.ElasticSearchList; import com.infun.platform.model.ElasticSearchPage; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.sort.SortBuilder; import java.util.List; /** * * es相关API调用 * @author linzhiqiang * @date 2018/7/10 */ public interface ElasticsearchService<T>{ /** * 通过索引id查询 * @param index * @param type * @param id * @return */ public ElasticSearchList getIndex(String index, String type, String id); /** * 通过queryBuilder查询 * * @param index * @param type * @param queryBuilder * @return */ public ElasticSearchList getIndex(String index, String type,QueryBuilder queryBuilder); /** * 通过queryBuilder+SortBuilder查询 * * @param index * @param type * @param queryBuilder * @param sortBuilderList * @return */ public ElasticSearchList getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList); /** * 通过queryBuilder+SortBuilder+highlightBuilder查询 * * @param index * @param type * @param queryBuilder * @param sortBuilderList * @param highlightBuilder * @return */ public ElasticSearchList getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, HighlightBuilder highlightBuilder); /** * 通过queryBuilder+SortBuilder+highlightBuilder+AggregationBuilder查询 * * @param index * @param type * @param queryBuilder * @param sortBuilderList * @param highlightBuilder * @param aggregationBuilderList * @return */ public ElasticSearchList getIndex(String index, String type, QueryBuilder queryBuilder, List<SortBuilder<?>> sortBuilderList, HighlightBuilder highlightBuilder, List<AggregationBuilder> 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<AggregationBuilder> 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<String> 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<String> 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<String> 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<T> implements ElasticsearchService<T> { /** * 锁标记 */ 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<AggregationBuilder> 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<AggregationBuilder> 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<String> 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 { throw new ApiElasticSearchException("【批量创建索引】创建索引失败,createIndex传入的ids与list参数必须对应一致。"); } } catch (Exception e) { throw new ApiElasticSearchException("【批量创建索引】量创建索引失败,失败原因:" + e.getMessage()); } return false; } } @Override public boolean modifyIndex(String index, String type, String id, T vo) { synchronized (obj) { try { XContentBuilder xContentBuilder = jsonBuilder(); if (null != vo) { xContentBuilder = ElasticSearchUtil.ObjectToJXContentBuilder(xContentBuilder, vo); } UpdateRequestBuilder updateRequestBuilder = getTransportClient.prepareUpdate(index, type, id).setDoc(xContentBuilder); UpdateResponse updateResponse = updateRequestBuilder.execute().actionGet(); if (Constants.ELASTICSEARCH_SUCCESS.equals(updateResponse.status())) { return true; } } catch (Exception e) { throw new ApiElasticSearchException("【更新索引】更新索引失败,失败原因:" + e.getMessage()); } return false; } } @Override public boolean modifyIndex(String index, String type, List<String> 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 = xContentBuilder = ElasticSearchUtil.ObjectToJXContentBuilder(jsonBuilder(), list.get(i)); UpdateRequestBuilder updateRequestBuilder = getTransportClient.prepareUpdate(index, type, ids.get(i)).setDoc(xContentBuilder); bulkRequestBuilder.add(updateRequestBuilder); } BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet(); if (bulkResponse.hasFailures()) { throw new ApiElasticSearchException("【批量更新索引】批量创建索引失败,失败原因::" + bulkResponse.buildFailureMessage()); } else { if (Constants.ELASTICSEARCH_SUCCESS.equals(bulkResponse.status())) { return true; } } } else { throw new ApiElasticSearchException("【批量更新索引】更新索引失败,updateIndex传入的ids与list参数必须对应一致。"); } } catch (Exception e) { throw new ApiElasticSearchException("【批量更新索引】" + e.getMessage()); } return false; } } @Override public boolean removeIndex(String index, String type, String id) { synchronized (obj) { try { DeleteRequestBuilder deleteRequestBuilder = getTransportClient.prepareDelete(index, type, id); DeleteResponse deleteResponse = deleteRequestBuilder.execute().actionGet(); if (Constants.ELASTICSEARCH_SUCCESS.equals(deleteResponse.status())) { return true; } } catch (Exception e) { throw new ApiElasticSearchException("【删除索引】" + e.getMessage()); } return false; } } @Override public boolean removeIndex(String index, String type, List<String> ids) { synchronized (obj) { try { BulkRequestBuilder bulkRequestBuilder = getTransportClient.prepareBulk(); if (null != ids) { for (String id : ids) { DeleteRequestBuilder deleteRequestBuilder = getTransportClient.prepareDelete(index, type, id); bulkRequestBuilder.add(deleteRequestBuilder); } } BulkResponse bulkResponse = bulkRequestBuilder.execute().actionGet(); if (bulkResponse.hasFailures()) { throw new ApiElasticSearchException("【批量删除索引失败,失败原因:】" + bulkResponse.buildFailureMessage()); } else { if (Constants.ELASTICSEARCH_SUCCESS.equals(bulkResponse.status())) { return true; } } } catch (Exception e) { throw new ApiElasticSearchException("【批量删除索引】" + e.getMessage()); } return false; } } }
从代码里面,大家也可以看出泛型的作用是什么,这边我们统一抛出自定义api错误,将异常进行转型处理。还有最重要的一点是,我们在添加,修改,删除索引的时候采用了锁,这样就可以保证在并发情况下,不会造成数据错误。
3.上面可以很明显的看到,我们通过工具类来处理高亮和聚合,所有这边也给大家介绍一下工具类里面的实现,如下所示:
package com.infun.platform.es.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.infun.platform.es.constant.ElasticsearchConstant; import com.infun.platform.model.entity.EsConfig; import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.bucket.range.Range; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * ElasticSearch工具类 * @author linzhiqiang */ public class ElasticSearchUtil { /** * ES桶聚合数目(默认是10条,设为1000条) */ public final static int ES_BUCKETS_NUMBER = 1000; /** * 处理高亮 * * @param searchHits * @return */ public static JSONArray handleHighlight(SearchHits searchHits) { JSONArray result = new JSONArray(); if (null != searchHits) { for (SearchHit searchHit : searchHits) { Map<String, Object> source = searchHit.getSourceAsMap(); // 获取高亮 Map<String, HighlightField> highlightMap = searchHit.getHighlightFields(); for (String key : highlightMap.keySet()) { for (Text text : highlightMap.get(key).getFragments()) { // 高亮字段,如name的高亮字段为nameHighlight source.put(key + "Highlight", text.toString()); } } result.add(JSONObject.parseObject(JSON.toJSONString(source))); } } return result; } /** * 处理聚合 * * @param aggregations * @return */ public static JSONObject handleAggregations(Aggregations aggregations) { JSONObject result = new JSONObject(); if (null != aggregations) { Map<String, Aggregation> aggregationMap = aggregations.asMap(); for (String aggregationKey : aggregationMap.keySet()) { // 遍历聚合字段 Aggregation aggregation = aggregations.get(aggregationKey); if (aggregation instanceof Terms) { // term聚合 Terms terms = (Terms) aggregation; if (null != terms && null != terms.getBuckets()) { JSONArray bucketJsonArray = new JSONArray(); for (Terms.Bucket bucket : terms.getBuckets()) { // 封装组合字段 JSONObject bucketJsonObject = new JSONObject(); bucketJsonObject.put("key", bucket.getKey()); bucketJsonObject.put("docCount", bucket.getDocCount()); bucketJsonArray.add(bucketJsonObject); } result.put(aggregationKey, bucketJsonArray); } else { result.put(aggregationKey, new JSONArray()); } } else if (aggregation instanceof Range) { // range、daterange聚合 Range range = (Range) aggregation; if (null != range && null != range.getBuckets()) { JSONArray bucketJsonArray = new JSONArray(); for (Range.Bucket bucket : range.getBuckets()) { // 封装组合字段 JSONObject bucketJsonObject = new JSONObject(); bucketJsonObject.put("key", bucket.getKey()); bucketJsonObject.put("docCount", bucket.getDocCount()); bucketJsonObject.put("from", bucket.getFrom()); bucketJsonObject.put("to", bucket.getTo()); bucketJsonArray.add(bucketJsonObject); } result.put(aggregationKey, bucketJsonArray); } else { result.put(aggregationKey, new JSONArray()); } } } } return result; } /** * 数据类型处理 * @param xContentBuilder * @param object * @return * @throws IOException */ public static XContentBuilder ObjectToJXContentBuilder(XContentBuilder xContentBuilder, Object object) throws IOException { // 数据类型格式处理 JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(object)); // 判断是否为null if (!jsonObject.isEmpty()) { xContentBuilder.startObject(); for (Object keyObject : jsonObject.keySet()) { // 设置值到content中 xContentBuilder.field(keyObject.toString(), jsonObject.get(keyObject)); } xContentBuilder.endObject(); } return xContentBuilder; } /** * 创建索引与mapping模板 * * @param index 索引字段 * @param type 类型 * @param client 客户端 * @throws IOException */ public static void createMapping(String index, String type, TransportClient client) throws IOException { CreateIndexRequestBuilder cib = client.admin() .indices().prepareCreate(index); XContentBuilder mapping = XContentFactory.jsonBuilder() .startObject() .startObject("properties") .startObject("id") .field("type", "long") .field("store", "yes") .endObject() .startObject("categoryName") .field("type", "string") .field("index", "not_analyzed") .endObject() .startObject("attribute") .field("type", "object") .endObject() .endObject() .endObject(); cib.addMapping(type, mapping); cib.execute().actionGet(); } }
工具类主要作用减少重复代码,将公用的方法抽离成通用工具类,一般都是static静态方法。
总结:
接口、接口实现类、工具类有了,接下来就是开始写具体的实现类了。这边之所以先介绍这三个模块的内容,是因为这三个属于es的公用模块,以后所有的实现类都是基于这三个模块进行开发的。下一节课将给大家介绍springboot是如何集成elasticsearch搜索引擎的内容的。
想要更多干货、技术猛料的孩子,快点拿起手机扫码关注我,我在这里等你哦~