packagecn.maitian.maimai.search.service.impl;importjava.net.InetAddress;importjava.util.ArrayList;importjava.util.Arrays;importjava.util.HashMap;importjava.util.Iterator;importjava.util.List;importjava.util.Map;importorg.apache.commons.lang3.StringUtils;importorg.apache.commons.logging.Log;importorg.apache.commons.logging.LogFactory;importorg.elasticsearch.action.admin.indices.alias.exists.AliasesExistRequestBuilder;importorg.elasticsearch.action.admin.indices.alias.exists.AliasesExistResponse;importorg.elasticsearch.action.admin.indices.create.CreateIndexRequest;importorg.elasticsearch.action.admin.indices.create.CreateIndexResponse;importorg.elasticsearch.action.admin.indices.delete.DeleteIndexResponse;importorg.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;importorg.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;importorg.elasticsearch.action.admin.indices.exists.types.TypesExistsRequest;importorg.elasticsearch.action.admin.indices.exists.types.TypesExistsResponse;importorg.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;importorg.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;importorg.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;importorg.elasticsearch.action.bulk.BulkRequestBuilder;importorg.elasticsearch.action.bulk.BulkResponse;importorg.elasticsearch.action.delete.DeleteResponse;importorg.elasticsearch.action.deletebyquery.DeleteByQueryAction;importorg.elasticsearch.action.deletebyquery.DeleteByQueryRequestBuilder;importorg.elasticsearch.action.deletebyquery.DeleteByQueryResponse;importorg.elasticsearch.action.index.IndexRequestBuilder;importorg.elasticsearch.action.index.IndexResponse;importorg.elasticsearch.action.search.ClearScrollRequestBuilder;importorg.elasticsearch.action.search.ClearScrollResponse;importorg.elasticsearch.action.search.SearchRequestBuilder;importorg.elasticsearch.action.search.SearchResponse;importorg.elasticsearch.action.search.SearchType;importorg.elasticsearch.action.update.UpdateRequestBuilder;importorg.elasticsearch.action.update.UpdateResponse;importorg.elasticsearch.client.Client;importorg.elasticsearch.client.transport.TransportClient;importorg.elasticsearch.common.text.Text;importorg.elasticsearch.common.transport.InetSocketTransportAddress;importorg.elasticsearch.common.unit.DistanceUnit;importorg.elasticsearch.common.unit.TimeValue;importorg.elasticsearch.common.xcontent.XContentBuilder;importorg.elasticsearch.common.xcontent.XContentFactory;importorg.elasticsearch.index.query.AndQueryBuilder;importorg.elasticsearch.index.query.BoolQueryBuilder;importorg.elasticsearch.index.query.ExistsQueryBuilder;importorg.elasticsearch.index.query.GeoBoundingBoxQueryBuilder;importorg.elasticsearch.index.query.GeoDistanceQueryBuilder;importorg.elasticsearch.index.query.MatchQueryBuilder;importorg.elasticsearch.index.query.OrQueryBuilder;importorg.elasticsearch.index.query.QueryBuilder;importorg.elasticsearch.index.query.QueryBuilders;importorg.elasticsearch.index.query.RangeQueryBuilder;importorg.elasticsearch.search.SearchHit;importorg.elasticsearch.search.SearchHitField;importorg.elasticsearch.search.SearchHits;importorg.elasticsearch.search.aggregations.Aggregation;importorg.elasticsearch.search.aggregations.AggregationBuilders;importorg.elasticsearch.search.aggregations.bucket.terms.StringTerms;importorg.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket;importorg.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;importorg.elasticsearch.search.highlight.HighlightField;importorg.elasticsearch.search.sort.SortBuilder;importorg.elasticsearch.search.sort.SortBuilders;importorg.elasticsearch.search.sort.SortOrder;importorg.springframework.util.CollectionUtils;importcom.alibaba.fastjson.JSON;importcn.maitian.maimai.search.common.ClientHelper;importcn.maitian.maimai.search.common.EsQueryModel;importcn.maitian.maimai.search.common.GeoPointModel;importcn.maitian.maimai.search.common.Pager;importcn.maitian.maimai.search.common.PropertyQueryModel;importcn.maitian.maimai.search.service.SearchService;/***
*@authorLXINXIN
* @company MAITIAN
*@version1.0*/
public class EsSearchServiceImpl implementsSearchService {/*** log日志*/
private Log logger = LogFactory.getLog(EsSearchServiceImpl.class);privateClientHelper clientHelper;public booleanupdateRecord(String clusterName, String index, String type,
String id, MapnewContent) {
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;
}returnresult;
}public booleaninsertRecord(String clusterName, String index, String type,
String id, Mapcontent) {
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;returnresult;
}public booleandeleteRecordById(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;returnsuccess;
}public booleandeleteRecordsByConditions(String clusterName, String index,
String type, Mapconditions) {long num = -1;
Client client=clientHelper.getClient(clusterName);
DeleteByQueryRequestBuilder deleteQueryBuilder= newDeleteByQueryRequestBuilder(
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;returnsuccess;
}publicString queryDocumentsUseScroll(EsQueryModel model) {
String clusterName=model.getClusterName();
String index=model.getIndex();
String type=model.getType();
Integer pageNo=model.getPageNo();
Integer size=model.getSize();
Map queryMaps =model.getQueryConditions();
List> rangeLists =model.getRangeFields();
Map sortMaps =model.getSortFields();
List 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> lists = new ArrayList>();/**下面这一段是构造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 (Mapmap : 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("")
.setHighlighterPostTags("");
}//查询(该查询已返回第一分片的数据)
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 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= newStringBuffer();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= newPager(hits.getTotalHits(), lists,
Long.parseLong(pageNo+ ""));returnJSON.toJSONString(result);
}//查询下一分页的数据
response =client.prepareSearchScroll(response.getScrollId())
.setScroll(TimeValue.timeValueMinutes(8)).execute()
.actionGet();
}
Pager result= new Pager(count, lists, Long.parseLong(pageNo + ""));returnJSON.toJSONString(result);
}catch(Exception e) {
e.printStackTrace();
}return null;
}publicString 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 must =model.getMust();
Map should =model.getShould();
List> rangeLists =model.getRangeFields();
Map sortMaps =model.getSortFields();
List 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> lists = new ArrayList>();try{
OrQueryBuilder or= null;
AndQueryBuilder and= null;//构建or查询
if (should != null && !should.isEmpty()) {
Iterator 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 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 (Mapmap : 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("")
.setHighlighterPostTags("");
}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 scrollIds = new ArrayList();for (Integer i = 1; i <= pageNo; i++) {
scrollIds.add(response.getScrollId());if(i.equals(pageNo)) {//取值
SearchHits hits =response.getHits();for(SearchHit hit : hits) {
Map 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= newPager(hits.getTotalHits(), lists,
Long.parseLong(pageNo+ ""));returnJSON.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) + "]");returnJSON.toJSONString(result);
}catch(Exception e) {
e.printStackTrace();
}return null;
}/****/
public voidset() {
clientHelper= newClientHelper();
}publicString queryDocumentsUseScrollMultiFields(EsQueryModel queryModel) {
String clusterName=queryModel.getClusterName();
String index=queryModel.getIndex();
String type=queryModel.getType();
Integer pageNo=queryModel.getPageNo();
Integer size=queryModel.getSize();
List> rangeLists =queryModel.getRangeFields();
Map sortMaps =queryModel.getSortFields();
List fields =queryModel.getFields();
String queryCondition=queryModel.getQueryCondition();
List 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> lists = new ArrayList>();/**下面这一段是构造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 (Mapmap : 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("")
.setHighlighterPostTags("");
}//查询(该查询已返回第一分片的数据)
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 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 + ""));returnJSON.toJSONString(result);
}//查询下一分页的数据
response =client.prepareSearchScroll(response.getScrollId())
.setScroll(TimeValue.timeValueMinutes(8)).execute()
.actionGet();
}
Pager result= new Pager(count, lists, Long.parseLong(pageNo + ""));returnJSON.toJSONString(result);
}catch(Exception e) {
e.printStackTrace();
}return null;
}/*** 清除scroll,缓解ES内存占用
*
*@paramclient
*@paramscrollIdList
*@returnboolean*/
private boolean clearScroll(Client client, ListscrollIdList) {
ClearScrollRequestBuilder clearScrollRequestBuilder=client
.prepareClearScroll();
clearScrollRequestBuilder.setScrollIds(scrollIdList);
ClearScrollResponse response=clearScrollRequestBuilder.get();returnresponse.isSucceeded();
}
@Overridepublic booleanbatchInsert(String clusterName, String index, String type,
List>contents) {
Client client=clientHelper.getClient(clusterName);boolean result = false;
BulkRequestBuilder request=client.prepareBulk();if(CollectionUtils.isEmpty(contents)) {throw new RuntimeException("批量插入的内容不能为空!");
}for(Mapmap : 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;
}returnresult;
}
@OverridepublicString queryStatExcellent(EsQueryModel model) {
String clusterName=model.getClusterName();
String index=model.getIndex();
String type=model.getType();
Integer pageNo=model.getPageNo();
Integer size=model.getSize();
Map conditions =model.getQueryConditions();
Map must =model.getMust();
Map should =model.getShould();
List> mustNot =model.getMustNot();
Map>> rangeMap =model.getRangeLists();
List notNullFields =model.getNotNullFields();
List nullFields =model.getNullFields();
Integer nearBy=model.getNearBy();
List geoPoints =model.getGeoPoints();
Map sortMaps =model.getSortFields();
List 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> lists = new ArrayList>();try{
BoolQueryBuilder boolQuery=QueryBuilders.boolQuery();if(null!= conditions && conditions.size()>0) {
Iterator 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 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 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(Mapmp : mustNot) {
Iterator 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 keys =rangeMap.keySet().iterator();while(keys.hasNext()) {
String key=keys.next();
List> ls =rangeMap.get(key);if(ls!=null&&ls.size()>0) {//即只包含一个begin end的情形
if(ls.size()==1) {
RangeQueryBuilder range=QueryBuilders.rangeQuery(key);
Map 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( Mapmp : 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 scrollIds = new ArrayList();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 hitResult = new HashMap();
Mapmp =hit.getFields();if(mp!=null&&!mp.isEmpty()) {
Iterator 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 + ""));returnJSON.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 aggregations =response.getAggregations().asList();
List> rs = new ArrayList>();for(Aggregation ag : aggregations) {
StringTerms st=(StringTerms)ag;
List buckets =st.getBuckets();for(Bucket bucket : buckets) {
Map map = new HashMap();
map.put("key", bucket.getKeyAsString());
map.put("count", bucket.getDocCount());
rs.add(map);
}
}
Pager result= new Pager(null, rs, Long.parseLong(pageNo + ""));returnJSON.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 hitResult = new HashMap();
Mapmp =hit.getFields();if(mp!=null&&!mp.isEmpty()) {
Iterator 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 + ""));returnJSON.toJSONString(result);
}
}catch(Exception e) {
e.printStackTrace();
}return null;
}
@OverridepublicString queryProperty(EsQueryModel model) {
String clusterName=model.getClusterName();
String index=model.getIndex();
String type=model.getType();
Integer pageNo=model.getPageNo();
Integer size=model.getSize();
Map sortMaps =model.getSortFields();
List 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 mp =propertyQueryModel.getQueryCondition();if(mp!=null&&mp.size()>0) {
Iterator 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>> rangeMap =propertyQueryModel.getRangeQuerys();//构造范围查询参数
if(rangeMap != null && rangeMap.size()>0) {
Iterator keys =rangeMap.keySet().iterator();while(keys.hasNext()) {
String key=keys.next();
List> rangeLs =rangeMap.get(key);if(ls!=null&&ls.size()>0) {//即只包含一个begin end的情形
if(ls.size()==1) {
RangeQueryBuilder range=QueryBuilders.rangeQuery(key);
Map 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( MaprangMp : 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> lists = new ArrayList>();try{if(pageNo*size>10000) {//查询(该查询已返回第一分片的数据)
SearchResponse response =searchRequestBuilder.setSize(size)
.setScroll(TimeValue.timeValueMinutes(1)).execute()
.actionGet();//取总计数
long count =response.getHits().getTotalHits();//scrollId用来清除
List scrollIds = new ArrayList();for (Integer i = 1; i <= pageNo; i++) {
scrollIds.add(response.getScrollId());if(i.equals(pageNo)) {
SearchHits hits=response.getHits();for(SearchHit hit : hits) {
Map hitResult = new HashMap();
Mapmp =hit.getFields();if(mp!=null&&!mp.isEmpty()) {
Iterator 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 + ""));returnJSON.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 + ""));returnJSON.toJSONString(result);
}
}catch(Exception e) {
e.printStackTrace();
}return null;
}
@Overridepublic String batchUpdate(String clusterName, String index, String type, List>contents) {
Client client=clientHelper.getClient(clusterName);
Map message = new HashMap();
BulkRequestBuilder request=client.prepareBulk();for(Mapmap : contents){if (map.get("id") == null || map.get("id").toString().trim().length() <= 0) {
message.put("errorMsg", "map中ID不能为空");
message.put("success", false);returnJSON.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);
}returnJSON.toJSONString(message);
}
@OverridepublicString 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 rs = new HashMap();
result=JSON.toJSONString(rs);
}returnresult;
}public booleanisIndexExists(String clusterName, String indexName) {
Client client=clientHelper.getClient(clusterName);
IndicesExistsRequest inExistsRequest= newIndicesExistsRequest(indexName);
IndicesExistsResponse inExistsResponse=client.admin().indices().exists(inExistsRequest).actionGet();returninExistsResponse.isExists();
}public booleanisIndexTypeExists(String clusterName, String indexName, String indexTypeName) {
Client client=clientHelper.getClient(clusterName);
TypesExistsRequest typesExistsRequest= new TypesExistsRequest(newString[]{indexName}, indexTypeName);
TypesExistsResponse response=client.admin().indices().typesExists(typesExistsRequest).actionGet();returnresponse.isExists();
}public booleancreateIndex(String clusterName, String indexName) {
Client client=clientHelper.getClient(clusterName);
CreateIndexRequest createIndexRequest= newCreateIndexRequest(indexName);
CreateIndexResponse createIndexResponse=client.admin().indices().create(createIndexRequest).actionGet();returncreateIndexResponse.isAcknowledged();
}public booleancreateMapping(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();returnputMappingResponse.isAcknowledged();
}public booleandeleteIndex(String clusterName, String indexName) {
Client client=clientHelper.getClient(clusterName);
DeleteIndexResponse dResponse=client.admin().indices().prepareDelete(indexName)
.execute().actionGet();returndResponse.isAcknowledged();
}public voidsetClientHelper(ClientHelper clientHelper) {this.clientHelper =clientHelper;
}
}