elasticsearch--index 和 mapping的创建之java api
package cn.maitian.maimai.search.service.impl; import java.net.InetAddress; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.elasticsearch.action.admin.indices.alias.exists.AliasesExistRequestBuilder; import org.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest; import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse; import org.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest; import org.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; import org.elasticsearch.action.bulk.BulkRequestBuilder; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.deletebyquery.DeleteByQueryAction; import org.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder; import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse; import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.ClearScrollRequestBuilder; import org.elasticsearch.action.search.ClearScrollResponse; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.action.update.UpdateRequestBuilder; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.Client; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.query.AndQueryBuilder; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.ExistsQueryBuilder; import org.elasticsearch.index.query.GeoBoundingBoxQueryBuilder; import org.elasticsearch.index.query.GeoDistanceQueryBuilder; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.OrQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.RangeQueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHitField; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder; import org.elasticsearch.search.highlight.HighlightField; import org.elasticsearch.search.sort.SortBuilder; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import org.springframework.util.CollectionUtils; import com.alibaba.fastjson.JSON; import cn.maitian.maimai.search.common.ClientHelper; import cn.maitian.maimai.search.common.EsQueryModel; import cn.maitian.maimai.search.common.GeoPointModel; import cn.maitian.maimai.search.common.Pager; import cn.maitian.maimai.search.common.PropertyQueryModel; import cn.maitian.maimai.search.service.SearchService; /** * * @author LXINXIN * @company MAITIAN * @version 1.0 */ public class EsSearchServiceImpl implements SearchService { /** * log日志 */ private Log logger = LogFactory.getLog(EsSearchServiceImpl.class); private ClientHelper clientHelper; public boolean updateRecord(String clusterName, String index, String type, String id, Map<String, Object> newContent) { Client client = clientHelper.getClient(clusterName); XContentBuilder xBuild = null; Boolean result = false; try { xBuild = XContentFactory.jsonBuilder().startObject(); for (String key : newContent.keySet()) { xBuild.field(key, newContent.get(key)); } xBuild.endObject(); UpdateResponse response = client.prepareUpdate(index, type, id) .setDoc(xBuild).execute().actionGet(); Integer success = response.getShardInfo().getSuccessful(); result = success > 0 ? true : false; } catch (Exception e) { logger.error(e); result = false; } return result; } public boolean insertRecord(String clusterName, String index, String type, String id, Map<String, Object> content) { Integer sucess = 0; Client client = clientHelper.getClient(clusterName); IndexRequestBuilder indexRequest = client.prepareIndex(index, type, id); XContentBuilder builder = null; boolean result = false; try { builder = XContentFactory.jsonBuilder().startObject(); for (String key : content.keySet()) { builder.field(key, content.get(key)); } builder.endObject(); IndexResponse response = indexRequest.setSource(builder).execute() .get(); sucess = response.getShardInfo().getSuccessful(); } catch (Exception e) { logger.error(e); result = false; } result = sucess > 0 ? true : false; return result; } public boolean deleteRecordById(String clusterName, String index, String type, String id) { Integer num = 0; Client client = clientHelper.getClient(clusterName); DeleteResponse response = client.prepareDelete(index, type, id) .execute().actionGet(); num = response.getShardInfo().getSuccessful(); boolean success = num > 0 ? true : false; return success; } public boolean deleteRecordsByConditions(String clusterName, String index, String type, Map<String, Object> conditions) { long num = -1; Client client = clientHelper.getClient(clusterName); DeleteByQueryRequestBuilder deleteQueryBuilder = new DeleteByQueryRequestBuilder( client, DeleteByQueryAction.INSTANCE).setIndices(index) .setTypes(type); // 条件为空的时候,就将该type删除 if (conditions == null) { } else { // 条件多于1的时候,采用精准匹配 多于1个时,采用模糊匹配,并且同时存在 if (conditions.size() > 1) { BoolQueryBuilder booleanQuery = QueryBuilders.boolQuery(); for (String key : conditions.keySet()) { booleanQuery.must(QueryBuilders.matchQuery(key, conditions.get(key))); } try { DeleteByQueryResponse res = deleteQueryBuilder .setQuery(booleanQuery).execute().actionGet(); num = res.getTotalDeleted(); } catch (Exception e) { logger.error(e); } } else { String key = conditions.keySet().iterator().next(); MatchQueryBuilder matchQuery = QueryBuilders.matchQuery(key, conditions.get(key)); try { DeleteByQueryResponse res = deleteQueryBuilder .setQuery(matchQuery).execute().actionGet(); num = res.getTotalDeleted(); } catch (Exception e) { logger.error(e); } } } boolean success = num >= 0 ? true : false; return success; } public String queryDocumentsUseScroll(EsQueryModel model) { String clusterName = model.getClusterName(); String index = model.getIndex(); String type = model.getType(); Integer pageNo = model.getPageNo(); Integer size = model.getSize(); Map<String,Object> queryMaps = model.getQueryConditions(); List<Map<String, Object>> rangeLists = model.getRangeFields(); Map<String,Object> sortMaps = model.getSortFields(); List<String> fields = model.getFields(); Client client = clientHelper.getClient(clusterName); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index) .setTypes(type).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); if (pageNo == null || pageNo <= 0) { pageNo = 1; } if (size == null || size <= 0) { size = 20; } try { List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); /** 下面这一段是构造bool嵌套,就是构造一个在满足精确查找的条件下,再去进行多字段的或者关系的全文检索 **/ // 构造全文或关系的查询 BoolQueryBuilder bb = QueryBuilders.boolQuery(); if (queryMaps != null) { for (Object key : queryMaps.keySet()) { bb = bb.must(QueryBuilders.matchQuery((String) key, queryMaps.get(key))); } searchRequestBuilder.setQuery(bb); } // 构造范围查询参数 QueryBuilder qb = null; if (rangeLists != null && rangeLists.size() > 0) { for (Map<String, Object> map : rangeLists) { if (map != null && (!map.isEmpty())) { if (map.get("field") != null) { if (map.get("from") != null && map.get("to") != null) { qb = QueryBuilders .rangeQuery( StringUtils.trim(map.get( "field").toString())) .from(StringUtils.trim(map.get("from") .toString())) .to(StringUtils.trim(map.get("to") .toString())); } else if (map.get("from") == null && map.get("to") != null) { qb = QueryBuilders.rangeQuery( StringUtils.trim(map.get("field") .toString())).to( StringUtils.trim(map.get("to") .toString())); } else if (map.get("from") != null && map.get("to") == null) { qb = QueryBuilders.rangeQuery( StringUtils.trim(map.get("field") .toString())).from( StringUtils.trim(map.get("from") .toString())); } } } } searchRequestBuilder.setPostFilter(qb); } // 构造排序参数 SortBuilder sortBuilder = null; if (sortMaps != null && sortMaps.size() > 0) { for (Object key : sortMaps.keySet()) { sortBuilder = SortBuilders.fieldSort((String) key).order( StringUtils.trim(sortMaps.get(key).toString()) .equals("ASC") ? SortOrder.ASC : SortOrder.DESC); } searchRequestBuilder.addSort(sortBuilder); } searchRequestBuilder.setExplain(true); // 构造高亮字段 if (fields != null && fields.size() > 0) { for (String field : fields) { searchRequestBuilder.addHighlightedField(field); } searchRequestBuilder.setHighlighterEncoder("UTF-8") .setHighlighterPreTags("<span style=\"color:red\">") .setHighlighterPostTags("</span>"); } // 查询(该查询已返回第一分片的数据) SearchResponse response = searchRequestBuilder.setSize(size) .setScroll(TimeValue.timeValueMinutes(1)).execute() .actionGet(); // 取总计数 long count = response.getHits().getTotalHits(); for (Integer i = 1; i <= pageNo; i++) { if (i.equals(pageNo)) { // 取值 SearchHits hits = response.getHits(); for (SearchHit hit : hits) { Map<String, HighlightField> result = hit .highlightFields(); if (fields != null) { // 用高亮字段替换搜索字段 for (String field : fields) { HighlightField titleField = result.get(field); if (titleField == null) { continue; } Text[] titleTexts = titleField.fragments(); StringBuffer value = new StringBuffer(); for (Text text : titleTexts) { value.append(text); } hit.getSource().put(field, value.toString()); } } hit.getSource().put("id", hit.getId()); hit.getSource().put("index", hit.getIndex()); hit.getSource().put("type", hit.getType()); lists.add(hit.getSource()); } Pager result = new Pager(hits.getTotalHits(), lists, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } // 查询下一分页的数据 response = client.prepareSearchScroll(response.getScrollId()) .setScroll(TimeValue.timeValueMinutes(8)).execute() .actionGet(); } Pager result = new Pager(count, lists, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } catch (Exception e) { e.printStackTrace(); } return null; } public String queryMultiField(EsQueryModel model) { long startTime = System.currentTimeMillis(); String clusterName = model.getClusterName(); String index = model.getIndex(); String type = model.getType(); Integer pageNo = model.getPageNo(); Integer size = model.getSize(); Map<String, Object> must = model.getMust(); Map<String, Object> should = model.getShould(); List<Map<String, Object>> rangeLists = model.getRangeFields(); Map<String, Object> sortMaps = model.getSortFields(); List<String> fields = model.getFields(); Client client = clientHelper.getClient(clusterName); long startTime1 = System.currentTimeMillis(); System.out.println("ES queryMultiField getClient[" + (startTime1-startTime) + "]"); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index) .setTypes(type).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); long startTime2 = System.currentTimeMillis(); System.out.println("ES queryMultiField prepareSearch[" + (startTime2-startTime1) + "]"); if (pageNo == null || pageNo <= 0) pageNo = 1; if (size == null || size <= 0) size = 20; List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); try { OrQueryBuilder or = null; AndQueryBuilder and = null; // 构建or查询 if (should != null && !should.isEmpty()) { Iterator<String> keys = should.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); if (should.get(key) != null && StringUtils.isNotBlank(should.get(key) .toString())) { QueryBuilder condition = QueryBuilders.matchQuery(key, should.get(key)); if (or == null) { or = QueryBuilders.orQuery(condition); } else { or.add(condition); } } } } // 构建AND查询 if (must != null && !must.isEmpty()) { Iterator<String> keys = must.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); if (null != must.get(key) && StringUtils.isNotBlank(must.get(key).toString())) { QueryBuilder condition = QueryBuilders.matchQuery(key, must.get(key)); if (and == null) { and = QueryBuilders.andQuery(condition); } else { and.add(condition); } } } } if (or != null) { if (and != null) { and.add(or); searchRequestBuilder.setQuery(and); } else { searchRequestBuilder.setQuery(or); } } else { if (and != null) { searchRequestBuilder.setQuery(and); } else { searchRequestBuilder .setQuery(QueryBuilders.matchAllQuery()); } } // 构造范围查询参数 QueryBuilder qb = null; if (rangeLists != null && rangeLists.size() > 0) { for (Map<String, Object> map : rangeLists) { if (map != null && (!map.isEmpty())) { if (map.get("field") != null) { if (map.get("from") != null && map.get("to") != null) { qb = QueryBuilders .rangeQuery( StringUtils.trim(map.get( "field").toString())) .from(StringUtils.trim(map.get("from") .toString())) .to(StringUtils.trim(map.get("to") .toString())); } else if (map.get("from") == null && map.get("to") != null) { qb = QueryBuilders.rangeQuery( StringUtils.trim(map.get("field") .toString())).to( StringUtils.trim(map.get("to") .toString())); } else if (map.get("from") != null && map.get("to") == null) { qb = QueryBuilders.rangeQuery( StringUtils.trim(map.get("field") .toString())).from( StringUtils.trim(map.get("from") .toString())); } } } } searchRequestBuilder.setPostFilter(qb); } // 构造排序参数 SortBuilder sortBuilder = null; if (sortMaps != null && sortMaps.size() > 0) { for (Object key : sortMaps.keySet()) { sortBuilder = SortBuilders.fieldSort((String) key).order( StringUtils.trim(sortMaps.get(key).toString()) .equals("ASC") ? SortOrder.ASC : SortOrder.DESC); } searchRequestBuilder.addSort(sortBuilder); } searchRequestBuilder.setExplain(true); // 构造高亮字段 if (fields != null && fields.size() > 0) { for (String field : fields) { searchRequestBuilder.addHighlightedField(field); } searchRequestBuilder.setHighlighterEncoder("UTF-8") .setHighlighterPreTags("<span style=\"color:red\">") .setHighlighterPostTags("</span>"); } long startTime3 = System.currentTimeMillis(); System.out.println("ES queryMultiField prepareSearch[" + (startTime3-startTime2) + "]"); // 查询(该查询已返回第一分片的数据) SearchResponse response = searchRequestBuilder.setSize(size) .setScroll(TimeValue.timeValueMinutes(1)).execute() .actionGet(); long startTime4 = System.currentTimeMillis(); System.out.println("ES queryMultiField execute[" + (startTime4-startTime3) + "]"); // 取总计数 long count = response.getHits().getTotalHits(); long startTime5 = System.currentTimeMillis(); System.out.println("ES queryMultiField count[" + (startTime5-startTime4) + "]"); //scrollId用来清除 List<String> scrollIds = new ArrayList<String>(); for (Integer i = 1; i <= pageNo; i++) { scrollIds.add(response.getScrollId()); if (i.equals(pageNo)) { // 取值 SearchHits hits = response.getHits(); for (SearchHit hit : hits) { Map<String, HighlightField> result = hit .highlightFields(); if (fields != null) { // 用高亮字段替换搜索字段 for (String field : fields) { HighlightField titleField = result.get(field); if (titleField == null) { continue; } Text[] titleTexts = titleField.fragments(); StringBuffer value = new StringBuffer(""); for (Text text : titleTexts) { value.append(text); } hit.getSource().put(field, value.toString()); } } hit.getSource().put("id", hit.getId()); hit.getSource().put("index", hit.getIndex()); hit.getSource().put("type", hit.getType()); lists.add(hit.getSource()); } Pager result = new Pager(hits.getTotalHits(), lists, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } // 查询下一分页的数据 response = client.prepareSearchScroll(response.getScrollId()) .setScroll(TimeValue.timeValueMinutes(8)).execute() .actionGet(); } clearScroll(client, scrollIds); Pager result = new Pager(count, lists, Long.parseLong(pageNo + "")); long startTime6 = System.currentTimeMillis(); System.out.println("ES queryMultiField count[" + (startTime6-startTime5) + "]"); return JSON.toJSONString(result); } catch (Exception e) { e.printStackTrace(); } return null; } /** * */ public void set() { clientHelper = new ClientHelper(); } public String queryDocumentsUseScrollMultiFields(EsQueryModel queryModel) { String clusterName = queryModel.getClusterName(); String index = queryModel.getIndex(); String type = queryModel.getType(); Integer pageNo = queryModel.getPageNo(); Integer size = queryModel.getSize(); List<Map<String, Object>> rangeLists = queryModel.getRangeFields(); Map<String,Object> sortMaps = queryModel.getSortFields(); List<String> fields = queryModel.getFields(); String queryCondition = queryModel.getQueryCondition(); List<String> queryFields = queryModel.getQueryFields(); Client client = clientHelper.getClient(clusterName); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index) .setTypes(type).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); if (pageNo == null || pageNo <= 0) { pageNo = 1; } if (size == null || size <= 0) { size = 20; } try { List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); /** 下面这一段是构造bool嵌套,就是构造一个在满足精确查找的条件下,再去进行多字段的或者关系的全文检索 **/ // 构造全文或关系的查询 if (queryCondition != null && !"".equals(queryCondition)) { String fieldString = Arrays.toString(queryFields.toArray()); searchRequestBuilder.setQuery(QueryBuilders.multiMatchQuery( queryCondition, fieldString)); } // 构造范围查询参数 QueryBuilder qb = null; if (rangeLists != null && rangeLists.size() > 0) { for (Map<String, Object> map : rangeLists) { if (map != null && (!map.isEmpty())) { if (map.get("field") != null) { if (map.get("from") != null && map.get("to") != null) { qb = QueryBuilders .rangeQuery( StringUtils.trim(map.get( "field").toString())) .from(StringUtils.trim(map.get("from") .toString())) .to(StringUtils.trim(map.get("to") .toString())); } else if (map.get("from") == null && map.get("to") != null) { qb = QueryBuilders.rangeQuery( StringUtils.trim(map.get("field") .toString())).to( StringUtils.trim(map.get("to") .toString())); } else if (map.get("from") != null && map.get("to") == null) { qb = QueryBuilders.rangeQuery( StringUtils.trim(map.get("field") .toString())).from( StringUtils.trim(map.get("from") .toString())); } } } } searchRequestBuilder.setPostFilter(qb); } // 构造排序参数 SortBuilder sortBuilder = null; if (sortMaps != null && sortMaps.size() > 0) { for (Object key : sortMaps.keySet()) { sortBuilder = SortBuilders.fieldSort((String) key).order( StringUtils.trim(sortMaps.get(key).toString()) .equals("ASC") ? SortOrder.ASC : SortOrder.DESC); } searchRequestBuilder.addSort(sortBuilder); } searchRequestBuilder.setExplain(true); // 构造高亮字段 if (fields != null && fields.size() > 0) { for (String field : fields) { searchRequestBuilder.addHighlightedField(field); } searchRequestBuilder.setHighlighterEncoder("UTF-8") .setHighlighterPreTags("<span style=\"color:red\">") .setHighlighterPostTags("</span>"); } // 查询(该查询已返回第一分片的数据) SearchResponse response = searchRequestBuilder.setSize(size) .setScroll(TimeValue.timeValueMinutes(1)).execute() .actionGet(); // 取总计数 long count = response.getHits().getTotalHits(); for (Integer i = 1; i <= pageNo; i++) { if (i.equals(pageNo)) { // 取值 SearchHits hits = response.getHits(); for (SearchHit hit : hits) { Map<String, HighlightField> result = hit .highlightFields(); if (fields != null) { // 用高亮字段替换搜索字段 for (String field : fields) { HighlightField titleField = result.get(field); if (titleField == null) { continue; } Text[] titleTexts = titleField.fragments(); StringBuffer value = new StringBuffer(""); for (Text text : titleTexts) { value.append(text); } hit.getSource().put(field, value.toString()); } } hit.getSource().put("id", hit.getId()); hit.getSource().put("index", hit.getIndex()); hit.getSource().put("type", hit.getType()); lists.add(hit.getSource()); } Pager result = new Pager(hits.getTotalHits(), lists, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } // 查询下一分页的数据 response = client.prepareSearchScroll(response.getScrollId()) .setScroll(TimeValue.timeValueMinutes(8)).execute() .actionGet(); } Pager result = new Pager(count, lists, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 清除scroll,缓解ES内存占用 * * @param client * @param scrollIdList * @return boolean */ private boolean clearScroll(Client client, List<String> scrollIdList) { ClearScrollRequestBuilder clearScrollRequestBuilder = client .prepareClearScroll(); clearScrollRequestBuilder.setScrollIds(scrollIdList); ClearScrollResponse response = clearScrollRequestBuilder.get(); return response.isSucceeded(); } @Override public boolean batchInsert(String clusterName, String index, String type, List<Map<String, Object>> contents) { Client client = clientHelper.getClient(clusterName); boolean result = false; BulkRequestBuilder request = client.prepareBulk(); if(CollectionUtils.isEmpty(contents)) { throw new RuntimeException("批量插入的内容不能为空!"); } for(Map<String,Object> map : contents){ if (map.get("id") == null || map.get("id").toString().trim().length() <= 0) { logger.error("id字段不能为空!"); throw new RuntimeException("id字段不能为空!"); } IndexRequestBuilder indexRequestBuilder = client.prepareIndex(index, type, map.get("id").toString()); indexRequestBuilder.setSource(map); request.add(indexRequestBuilder); } BulkResponse rs = request.execute().actionGet(); if(rs.hasFailures()){ result = false; }else{ result = true; } return result; } @Override public String queryStatExcellent(EsQueryModel model) { String clusterName = model.getClusterName(); String index = model.getIndex(); String type = model.getType(); Integer pageNo = model.getPageNo(); Integer size = model.getSize(); Map<String, Object> conditions = model.getQueryConditions(); Map<String, Object> must = model.getMust(); Map<String, Object> should = model.getShould(); List<Map<String,Object>> mustNot = model.getMustNot(); Map<String,List<Map<String,Object>>> rangeMap = model.getRangeLists(); List<String> notNullFields = model.getNotNullFields(); List<String> nullFields = model.getNullFields(); Integer nearBy = model.getNearBy(); List<GeoPointModel> geoPoints = model.getGeoPoints(); Map<String, Object> sortMaps = model.getSortFields(); List<String> fields = model.getFields(); String countField = model.getCountField(); Client client = clientHelper.getClient(clusterName); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index) .setTypes(type).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); if (pageNo == null || pageNo <= 0) pageNo = 1; if (size == null || size < 0) size = 20; List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); try { BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); if(null!= conditions && conditions.size()>0) { Iterator<String> keys = conditions.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); if (conditions.get(key) != null && StringUtils.isNotBlank(conditions.get(key) .toString())) { MatchQueryBuilder condition = QueryBuilders.matchQuery(key, conditions.get(key)); boolQuery.must(condition); } } } if(null!= must &&must.size()>0) { Iterator<String> keys = must.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); if (must.get(key) != null && StringUtils.isNotBlank(must.get(key) .toString())) { MatchQueryBuilder condition = QueryBuilders.matchQuery(key, must.get(key)); condition.minimumShouldMatch("100%"); boolQuery.must(condition); } } } if(null!= should && should.size()>0) { Iterator<String> keys = should.keySet().iterator(); BoolQueryBuilder shouldQuery = QueryBuilders.boolQuery(); boolean appendShould = false; while (keys.hasNext()) { String key = keys.next(); if (should.get(key) != null && StringUtils.isNotBlank(should.get(key) .toString())) { MatchQueryBuilder shouldCondition = QueryBuilders.matchQuery(key, should.get(key)); shouldQuery.should(shouldCondition); appendShould = true; } } if(appendShould) { boolQuery.must(shouldQuery); } } //排除的部分 if(null!= mustNot && mustNot.size()>0) { for(Map<String,Object> mp : mustNot) { Iterator<String> keys = mp.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); Object obj = mp.get(key); if(null!=obj) { MatchQueryBuilder mt = QueryBuilders.matchQuery(key, obj); boolQuery.mustNot(mt); } } } } // 构造范围查询参数 if(rangeMap != null && rangeMap.size()>0 ) { Iterator<String> keys = rangeMap.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); List<Map<String,Object>> ls = rangeMap.get(key); if(ls!=null&&ls.size()>0) { //即只包含一个begin end的情形 if(ls.size()==1) { RangeQueryBuilder range = QueryBuilders.rangeQuery(key); Map<String,Object> mp = ls.get(0); if(mp.isEmpty()) { continue; } if(mp.containsKey("begin")) { range.from(mp.get("begin")); } if(mp.containsKey("end")) { range.to(mp.get("end")); } range.includeLower(true); range.includeUpper(true); boolQuery.must(range); }else { //包含多个起止值时 BoolQueryBuilder multiValue = QueryBuilders.boolQuery(); for( Map<String,Object> mp : ls) { if(mp.isEmpty()) { continue; } RangeQueryBuilder range = QueryBuilders.rangeQuery(key); if(mp.containsKey("begin")) { range.from(mp.get("begin")); } if(mp.containsKey("end")) { range.to(mp.get("end")); } range.includeLower(true); range.includeUpper(true); multiValue.should(range); } boolQuery.must(multiValue); } } } } if(null!= notNullFields && notNullFields.size()>0) { for(String field : notNullFields) { //不为空查询 ExistsQueryBuilder exists= QueryBuilders.existsQuery(field); boolQuery.must(exists); } } if(null!=nullFields&&nullFields.size()>0) { for(String field : nullFields) { ExistsQueryBuilder exists= QueryBuilders.existsQuery(field); boolQuery.mustNot(exists); } } //查找附近 if(null!=nearBy&&geoPoints!=null&&geoPoints.size()==1) { //地理坐标字段统一为location GeoDistanceQueryBuilder distance = QueryBuilders.geoDistanceQuery("location").distance(Double.valueOf(nearBy), DistanceUnit.KILOMETERS); GeoPointModel gp =geoPoints.get(0); distance.lat(gp.getLat()); distance.lon(gp.getLng()); boolQuery.must(distance); }else if(null==nearBy&&geoPoints!=null&&geoPoints.size()>1) { //画圈找房 Double topLeftLat = geoPoints.get(0).getLat(); Double topLeftlng = geoPoints.get(0).getLng(); Double btmRightLat = geoPoints.get(1).getLat(); Double btmRightlng = geoPoints.get(1).getLng(); //设置左上角和右下角坐标 GeoBoundingBoxQueryBuilder geoBound = QueryBuilders.geoBoundingBoxQuery("location").topLeft(topLeftLat,topLeftlng) .bottomRight(btmRightLat, btmRightlng) ; boolQuery.must(geoBound); }else if(null!=nearBy&&geoPoints!=null&&geoPoints.size()>1) { GeoDistanceQueryBuilder distance = QueryBuilders.geoDistanceQuery("location").distance(Double.valueOf(nearBy), DistanceUnit.KILOMETERS); GeoPointModel gp =geoPoints.get(0); distance.lat(gp.getLat()); distance.lon(gp.getLng()); boolQuery.must(distance); //画圈找房 Double topLeftLat = geoPoints.get(1).getLat(); Double topLeftlng = geoPoints.get(1).getLng(); Double btmRightLat = geoPoints.get(2).getLat(); Double btmRightlng = geoPoints.get(2).getLng(); //设置左上角和右下角坐标 GeoBoundingBoxQueryBuilder geoBound = QueryBuilders.geoBoundingBoxQuery("location").topLeft(topLeftLat,topLeftlng) .bottomRight(btmRightLat, btmRightlng) ; boolQuery.must(geoBound); } searchRequestBuilder.setQuery(boolQuery); // 构造排序参数 SortBuilder sortBuilder = null; if (sortMaps != null && sortMaps.size() > 0) { for (Object key : sortMaps.keySet()) { sortBuilder = SortBuilders.fieldSort((String) key) .order(StringUtils.trim(sortMaps.get(key).toString()).equalsIgnoreCase("ASC") ? SortOrder.ASC : SortOrder.DESC); searchRequestBuilder.addSort(sortBuilder); } } searchRequestBuilder.setExplain(false); if(null!=fields&&fields.size()>0) { for(String field : fields) { searchRequestBuilder.addField(field); } } //聚合查询 TermsBuilder aggregation = null; if(StringUtils.isNotBlank(countField)) { //聚合的时候,将size设置为0,这样才能够返回全部,不然只返回top10 aggregation= AggregationBuilders.terms(countField).field(countField).size(0); searchRequestBuilder.addAggregation(aggregation); } // System.out.println(searchRequestBuilder); if(pageNo*size>10000) { // 查询(该查询已返回第一分片的数据) SearchResponse response = searchRequestBuilder.setSize(size) .setScroll(TimeValue.timeValueMinutes(1)).execute() .actionGet(); // 取总计数 long count = response.getHits().getTotalHits(); //scrollId用来清除 List<String> scrollIds = new ArrayList<String>(); for (Integer i = 1; i <= pageNo; i++) { scrollIds.add(response.getScrollId()); if (i.equals(pageNo) ) { SearchHits hits = response.getHits(); if(null==fields||fields.size()==0) { for (SearchHit hit : hits) { hit.getSource().put("id", hit.getId()); hit.getSource().put("index", hit.getIndex()); hit.getSource().put("type", hit.getType()); lists.add(hit.getSource()); } }else { for (SearchHit hit : hits) { Map<String,Object> hitResult = new HashMap<String, Object>(); Map<String, SearchHitField>mp = hit.getFields(); if(mp!=null&&!mp.isEmpty()) { Iterator<String> keyItor = mp.keySet().iterator(); while(keyItor.hasNext()) { String key = keyItor.next(); Object value = (Object)mp.get(key).getValue(); hitResult.put(key, value); } } lists.add(hitResult); } } clearScroll(client, scrollIds); Pager result = new Pager(count, lists, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } // 查询下一分页的数据 response = client.prepareSearchScroll(response.getScrollId()) .setScroll(TimeValue.timeValueMinutes(5)).execute() .actionGet(); } }else { int from = (pageNo-1)*size; SearchResponse response = searchRequestBuilder.setFrom(from).setSize(size).get(); //如果是聚合查询,只返回聚合查询部分即可 if(aggregation!=null) { List<Aggregation> aggregations =response.getAggregations().asList(); List<Map<String,Object>> rs = new ArrayList<Map<String,Object>>(); for(Aggregation ag : aggregations) { StringTerms st = (StringTerms)ag; List<Bucket> buckets = st.getBuckets(); for(Bucket bucket : buckets) { Map<String,Object> map = new HashMap<String, Object>(); map.put("key", bucket.getKeyAsString()); map.put("count", bucket.getDocCount()); rs.add(map); } } Pager result = new Pager(null, rs, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } // 取总计数 long count = response.getHits().getTotalHits(); // 取值 SearchHits hits = response.getHits(); if(null==fields||fields.size()==0) { for (SearchHit hit : hits) { hit.getSource().put("id", hit.getId()); hit.getSource().put("index", hit.getIndex()); hit.getSource().put("type", hit.getType()); lists.add(hit.getSource()); } }else { for (SearchHit hit : hits) { Map<String,Object> hitResult = new HashMap<String, Object>(); Map<String, SearchHitField>mp = hit.getFields(); if(mp!=null&&!mp.isEmpty()) { Iterator<String> keyItor = mp.keySet().iterator(); while(keyItor.hasNext()) { String key = keyItor.next(); Object value = (Object)mp.get(key).getValue(); hitResult.put(key, value); } } lists.add(hitResult); } } Pager result = new Pager(count, lists, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } } catch (Exception e) { e.printStackTrace(); } return null; } @Override public String queryProperty(EsQueryModel model) { String clusterName = model.getClusterName(); String index = model.getIndex(); String type = model.getType(); Integer pageNo = model.getPageNo(); Integer size = model.getSize(); Map<String, Object> sortMaps = model.getSortFields(); List<PropertyQueryModel> ls = model.getProperty(); Client client = clientHelper.getClient(clusterName); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index) .setTypes(type).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); if (pageNo == null || pageNo <= 0) pageNo = 1; if (size == null || size < 0) size = 20; BoolQueryBuilder booleanQuery = QueryBuilders.boolQuery(); if(!CollectionUtils.isEmpty(ls)) { for(PropertyQueryModel propertyQueryModel : ls) { boolean appendCondition = false; BoolQueryBuilder condition = QueryBuilders.boolQuery(); Map<String,Object> mp = propertyQueryModel.getQueryCondition(); if(mp!=null&&mp.size()>0) { Iterator<String> keys = mp.keySet().iterator(); while(keys.hasNext()) { String key = keys.next(); Object value = mp.get(key); MatchQueryBuilder match = QueryBuilders.matchQuery(key, value); condition.must(match); appendCondition = true; } } Map<String,List<Map<String,Object>>> rangeMap = propertyQueryModel.getRangeQuerys(); // 构造范围查询参数 if(rangeMap != null && rangeMap.size()>0 ) { Iterator<String> keys = rangeMap.keySet().iterator(); while (keys.hasNext()) { String key = keys.next(); List<Map<String,Object>> rangeLs = rangeMap.get(key); if(ls!=null&&ls.size()>0) { //即只包含一个begin end的情形 if(ls.size()==1) { RangeQueryBuilder range = QueryBuilders.rangeQuery(key); Map<String,Object> rangMp = rangeLs.get(0); if(rangMp.isEmpty()) { continue; } if(rangMp.containsKey("begin")) { range.from(rangMp.get("begin")); } if(rangMp.containsKey("end")) { range.to(rangMp.get("end")); } condition.must(range); appendCondition = true; }else { //包含多个起止值时 BoolQueryBuilder multiValue = QueryBuilders.boolQuery(); for( Map<String,Object> rangMp : rangeLs) { if(rangMp.isEmpty()) { continue; } RangeQueryBuilder range = QueryBuilders.rangeQuery(key); if(rangMp.containsKey("begin")) { range.from(rangMp.get("begin")); } if(rangMp.containsKey("end")) { range.to(rangMp.get("end")); } multiValue.should(range); } condition.must(multiValue); appendCondition = true; } } } } if(appendCondition) { booleanQuery.should(condition); } } } // 构造排序参数 SortBuilder sortBuilder = null; if (sortMaps != null && sortMaps.size() > 0) { for (Object key : sortMaps.keySet()) { sortBuilder = SortBuilders.fieldSort((String) key) .order(StringUtils.trim(sortMaps.get(key).toString()).equalsIgnoreCase("ASC") ? SortOrder.ASC : SortOrder.DESC); searchRequestBuilder.addSort(sortBuilder); } } List<Map<String, Object>> lists = new ArrayList<Map<String, Object>>(); try { if(pageNo*size>10000) { // 查询(该查询已返回第一分片的数据) SearchResponse response = searchRequestBuilder.setSize(size) .setScroll(TimeValue.timeValueMinutes(1)).execute() .actionGet(); // 取总计数 long count = response.getHits().getTotalHits(); //scrollId用来清除 List<String> scrollIds = new ArrayList<String>(); for (Integer i = 1; i <= pageNo; i++) { scrollIds.add(response.getScrollId()); if (i.equals(pageNo)) { SearchHits hits = response.getHits(); for (SearchHit hit : hits) { Map<String,Object> hitResult = new HashMap<String, Object>(); Map<String, SearchHitField>mp = hit.getFields(); if(mp!=null&&!mp.isEmpty()) { Iterator<String> keyItor = mp.keySet().iterator(); while(keyItor.hasNext()) { String key = keyItor.next(); Object value = (Object)mp.get(key).getValue(); hitResult.put(key, value); } } lists.add(hitResult); } clearScroll(client, scrollIds); Pager result = new Pager(count, lists, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } // 查询下一分页的数据 response = client.prepareSearchScroll(response.getScrollId()) .setScroll(TimeValue.timeValueMinutes(5)).execute() .actionGet(); } }else { // 查询(该查询已返回第一分片的数据) int from = (pageNo-1)*size; SearchResponse response = searchRequestBuilder.setFrom(from).setSize(size).setQuery(booleanQuery).get(); // 取总计数 long count = response.getHits().getTotalHits(); // 取值 SearchHits hits = response.getHits(); for (SearchHit hit : hits) { hit.getSource().put("id", hit.getId()); hit.getSource().put("index", hit.getIndex()); hit.getSource().put("type", hit.getType()); lists.add(hit.getSource()); } Pager result = new Pager(count, lists, Long.parseLong(pageNo + "")); return JSON.toJSONString(result); } } catch (Exception e) { e.printStackTrace(); } return null; } @Override public String batchUpdate(String clusterName, String index, String type, List<Map<String, Object>> contents) { Client client = clientHelper.getClient(clusterName); Map<String,Object> message = new HashMap<String, Object>(); BulkRequestBuilder request = client.prepareBulk(); for(Map<String,Object> map : contents){ if (map.get("id") == null || map.get("id").toString().trim().length() <= 0) { message.put("errorMsg", "map中ID不能为空"); message.put("success", false); return JSON.toJSONString(message); } UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate(index, type, map.get("id").toString()); updateRequestBuilder.setDoc(map); request.add(updateRequestBuilder); } BulkResponse rs = request.execute().actionGet(); if(rs.hasFailures()){ message.put("errorMsg", rs.buildFailureMessage()); message.put("success", false); }else{ message.put("success", true); } return JSON.toJSONString(message); } @Override public String findRecordById(String clusterName, String index, String type, String id) { Client client = clientHelper.getClient(clusterName); SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index) .setTypes(type).setSearchType(SearchType.DFS_QUERY_THEN_FETCH); QueryBuilder queryBuilder = QueryBuilders.idsQuery().ids(id); String result = null; try { SearchResponse response = searchRequestBuilder.setQuery(queryBuilder).get(); // 取值 SearchHits hits = response.getHits(); for (SearchHit hit : hits) { hit.getSource().put("id", hit.getId()); hit.getSource().put("index", hit.getIndex()); hit.getSource().put("type", hit.getType()); result = JSON.toJSONString(hit.getSource()); } } catch (Exception e) { Map<String,Object> rs = new HashMap<String,Object>(); result = JSON.toJSONString(rs); } return result; } public boolean isIndexExists(String clusterName, String indexName) { Client client = clientHelper.getClient(clusterName); IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName); IndicesExistsResponse inExistsResponse = client.admin().indices().exists(inExistsRequest).actionGet(); return inExistsResponse.isExists(); } public boolean isIndexTypeExists(String clusterName, String indexName, String indexTypeName) { Client client = clientHelper.getClient(clusterName); TypesExistsRequest typesExistsRequest = new TypesExistsRequest(new String[]{indexName}, indexTypeName); TypesExistsResponse response = client.admin().indices().typesExists(typesExistsRequest).actionGet(); return response.isExists(); } public boolean createIndex(String clusterName, String indexName) { Client client = clientHelper.getClient(clusterName); CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName); CreateIndexResponse createIndexResponse = client.admin().indices().create(createIndexRequest).actionGet(); return createIndexResponse.isAcknowledged(); } public boolean createMapping(String clusterName, String indexName, String indexType, String mappingJson) { Client client = clientHelper.getClient(clusterName); PutMappingResponse putMappingResponse = client.admin().indices().preparePutMapping(indexName) .setType(indexType).setSource(mappingJson) .execute().actionGet(); return putMappingResponse.isAcknowledged(); } public boolean deleteIndex(String clusterName, String indexName) { Client client = clientHelper.getClient(clusterName); DeleteIndexResponse dResponse = client.admin().indices().prepareDelete(indexName) .execute().actionGet(); return dResponse.isAcknowledged(); } public void setClientHelper(ClientHelper clientHelper) { this.clientHelper = clientHelper; } }
dm_customer.txt
{ "dm_customer": { "properties": { "oldorgid": { "type": "double" }, "nTransaCustHisHAvgViews": { "type": "double" }, "pernobusicnt": { "type": "double" }, "addCustsDealsRate": { "type": "double" }, "shopcnt": { "type": "double" }, "nTransaCustPeriod": { "type": "double" }, "startdate": { "type": "string" }, "type": { "type": "string", "index": "not_analyzed" }, "addCusts7daysLook": { "type": "double" }, "crossAreaLookGroupsCnt": { "type": "double" }, "newCustomersSeeMore": { "type": "double" }, "addCusts": { "type": "double" }, "efCusts": { "type": "double" }, "perbusicnt": { "type": "double" }, "id": { "type": "string" }, "dimdateid": { "type": "string", "index": "not_analyzed" }, "newRentCustomerGroup": { "type": "double" }, "newRentCustomersWithViews": { "type": "double" }, "crossAreaOneWithMoreGroup": { "type": "double" }, "nTransaCustHisHViews": { "type": "double" }, "shopcnt1": { "type": "double" }, "crossAreaOneWithMoreLooks": { "type": "double" }, "shopcnt2": { "type": "double" }, "batchtime": { "type": "string", "index": "not_analyzed" }, "dimorgallcnt": { "type": "double" }, "npidfullpath": { "type": "string" }, "batchuser": { "type": "string" }, "enddate": { "type": "string" }, "nTransaCustAvgPeriod": { "type": "double" }, "crossRegionLookGroups": { "type": "double" }, "nidfullpath": { "type": "string" }, "areacnt2": { "type": "double" }, "areacnt1": { "type": "double" }, "dimorgid": { "type": "double" }, "dimorgName": { "type": "string" }, "mid": { "type": "string" }, "crossAreaOneWithMoreRate": { "type": "double" }, "nTransaCustHisVAvgViews": { "type": "double" }, "buyCusts": { "type": "double" }, "crossZoneOfAreaLookGroups": { "type": "double" }, "newSalesPurchasesGroup": { "type": "double" }, "crossAreaLookGroups": { "type": "double" }, "salesWithViews": { "type": "double" }, "secLookRate": { "type": "double" }, "perBusi": { "type": "double" }, "secLookCusts": { "type": "double" }, "addCustsDeals": { "type": "double" }, "crossAreaLookRate": { "type": "double" }, "actperbusi2": { "type": "double" }, "idfullpath": { "type": "string" }, "dimorgcnt": { "type": "double" }, "crossZoneOfAreaLookRate": { "type": "double" }, "lookGroups": { "type": "double" }, "pidfullpath": { "type": "string" }, "crossAreaOneWithMore": { "type": "double" }, "dictcnt1": { "type": "double" }, "dictcnt2": { "type": "double" }, "processed": { "type": "string", "index": "not_analyzed" }, "status": { "type": "string", "index": "not_analyzed" }, "crossZoneOfAreaLooks": { "type": "double" }, "areaSLookCnt": { "type": "double" }, "areaSLookGroups": { "type": "double" }, "repeatLookGroups": { "type": "double" } , "repeatLookRate": { "type": "double" } } } }
划船不用桨、杨帆不等风、一生全靠浪