ElasticSearchClient.java

package com.zbiti.framework.elasticsearch.utils;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.elasticsearch.action.admin.cluster.node.info.NodeInfo;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoRequest;
import org.elasticsearch.action.admin.cluster.node.info.NodesInfoResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.node.Node;
import org.elasticsearch.node.NodeBuilder; /**
* 创建索引的client
*
* @author cookey
*/
public class ElasticSearchClient {
private static Logger logger = Logger.getLogger(ElasticSearchClient.class);
private static Client searchClient = null;
private static List<String> clusterList = null; private ElasticSearchClient() {
} /**
* 创建client实例
*
* @return client
* */
public static synchronized Client getInstance() {
if (searchClient == null) {
open();
}
return searchClient;
} /*
* 创建搜索客户�?tcp连接搜索服务�?创建索引 创建mapping
*/
private static void open() {
try {
/**
* 如果100秒没有连接上搜索服务器,则超�?
* */
Settings settings = ImmutableSettings
.settingsBuilder()
// .put(this.searchClientConfigureMap)
.put("client.transport.ping_timeout", "100s")
.put("client.transport.sniff", "true")
.put("cluster.name",PropertyManager.getContextProperty("cluster.name"))
// .put("client.transport.ignore_cluster_name", "true")
.build();
/* 创建搜索客户�?*/
searchClient = new TransportClient(settings);
if (CollectionUtils.isEmpty(clusterList)) {
String cluster = PropertyManager.getContextProperty("search.clusterList");
if (cluster != null) {
clusterList = Arrays.asList(cluster.split(","));
}
}
for (String item : clusterList) {
String address = item.split(":")[0];
int port = Integer.parseInt(item.split(":")[1]);
/* 通过tcp连接搜索服务器,如果连接不上,有�?��可能是服务器端与客户端的jar包版本不匹配 */
searchClient = ((TransportClient) searchClient).addTransportAddress(new InetSocketTransportAddress(
address, port));
}
} catch (Exception e) {
logger.error(e.getMessage());
e.printStackTrace();
}
} /**
* 创建node对象
*
* @return client
* */
public static Client createClient() {
NodeBuilder builder = NodeBuilder.nodeBuilder();
String clusterName = PropertyManager.getContextProperty("cluster.name");
builder.clusterName(clusterName); Boolean isClient = Boolean.parseBoolean(PropertyManager
.getContextProperty("node.client"));
builder.client(isClient); Node node = builder.node();
/*
* Node node = nodeBuilder().clusterName("yourclustername").node();
* Client client = node.client();
*/
return node.client();
} /**
* 测试ES可用连接数方�?同时也也可以用以校验ES是否可以连接�?
*/
public static boolean testConnection(Client client) {
try {
/**
* 获得节点信息
* */
NodesInfoResponse response = client.admin().cluster()
.nodesInfo(new NodesInfoRequest().timeout("30"))
.actionGet();
// 获得集群名称
String cluNameString = response.getClusterNameAsString();
System.out.println(cluNameString);
// 获得节点名称
NodeInfo[] indexNameString = response.getNodes();
System.out.println(indexNameString[0].getNode().getName());
Map<String, NodeInfo> nodesMap = response.getNodesMap();
// 打印节点信息
for (Map.Entry<String, NodeInfo> entry : nodesMap.entrySet()) {
System.out.println(entry.getKey() + ":"
+ entry.getValue().getServiceAttributes());
}
if (nodesMap != null) {
return true;
}
/**
* 获得Mapping
* */
ClusterState cs = client.admin().cluster().prepareState()
.setFilterIndices("standard_test").execute().actionGet()
.getState(); IndexMetaData imd = cs.getMetaData().index("standard_test");
// type的名�?
MappingMetaData mdd = imd.mapping("standard_type_test");
System.out.println(mdd.sourceAsMap()); } catch (Exception e) {
e.printStackTrace();
System.out.println("无法连接到Elasticsearch");
}
return false;
} /**
* 索引库是否存�?
*
* @param client
* 客户�?
* @param index
* 索引库名
* @return 存在则返回true,不存在则返回false
*/
public static Boolean indexExist(Client client, String index) {
IndicesExistsRequest request = new IndicesExistsRequestBuilder(client
.admin().indices(), index).request();
IndicesExistsResponse response = client.admin().indices()
.exists(request).actionGet();
return response.isExists();
} }

ElasticSearchDao.java

package com.zbiti.framework.elasticsearch.utils;

import java.util.List;

import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.search.SearchHits; import com.zbiti.framework.base.entity.BaseEntity;
/**
* ElasticSearch操作接口
*
* @author LICHANGWU
*
*/
public interface ElasticSearchDao { // -------------------------创建索引-----------------------------
/**
* 根据对象实例的json串创建索引(单条索引).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param indexId
* 建立索引的唯一标识ID
* @param jsonStr
* 索引对象实例的对应json串
* @author LICHANGWU
*/
public IndexResponse createIndexByJson(String indexName, String indexType,
String indexId, String jsonStr); /**
* 根据对象集合批量索引(多条批量)
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param surplusGoodsEntitys
* 对象集合
*/
public void createIndexsByBeans(String indexName, String indexType,
List<BaseEntity> entry); // ------------------------------------查询------------------------------------------------- /**
* 根据索引唯一标识ID查找对象().
*
* @param indexName
* 索引组名
*
* @param indexType
* 索引类型
* @param queryName
* 查询字段名
* @param queryValue
* 查询字段值
* @param PageInfo
* 分页情况
*
* @author LICHANGWU
*/
public List<Object> searchById(String indexName, String indexType,
String queryName, String queryValue, PageInfo pageInfo); /**
* 根据传入的值进行模糊查询(分页查询).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
*
* @param queryMap
* key 查询条件 value 字段域
* @param pageInfo
* 分页信息
* @return
*/
/**
* 根据传入的值进行模糊查询(分页查询).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
*
* @param queryMap
* key 查询条件 value 字段域
* @param pageInfo
* 分页信息
* @return
* @throws Exception
*/
public List search(String indexName, String indexType,
List<QueryBuilder> bqbl,List<FilterBuilder> fbList,String[] highLighterFields, PageInfo pageInfo, Class cls,List<Order> orders)
throws Exception; /**
* 根据传入的值进行模糊查询(分组查询个数).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
*
* @param queryMap
* key 查询条件 value 字段域
* @param pageInfo
* 分页信息
*
* @param groupFiled
* 分页字段
* @return
* @throws Exception
*/
public List searchCount(String indexName, String indexType,
List<QueryBuilder> bqbl, List<FilterBuilder> fbList,PageInfo pageInfo,String groupFiled); public SearchHits searchByOnlyValue(String indexName, String indexType,
String queryValue, PageInfo pageInfo) ; // -----------------------删除---(暂且完结)---------------------- /**
* 根据指定属性键值对删除索引(查询删除).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param name
* 删除依据的字段属性名
* @param value
* 删除依据的字段属性值
*/
public DeleteByQueryResponse deleteIndexByNameAndValue(String indexName,
String indexType, String name, String value); /**
* 根据索引的Id删除索引(单条删除).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param Id
* 指定索引文档Id
*/
public DeleteResponse deleteIndexById(String indexName, String indexType,
String Id); /**
* 根据索引的Id集合删除索引(批量删除).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param Ids
* 指定索引文档Id集合
*/
public void deleteIndexByIds(String indexName, String indexType,
List<String> Ids); /**
* 删除确定索引组和索引类型下所有数据(危险,慎用).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
*/
public void deleteAllIndex(String indexName, String indxType); // --------------------------更新索引------------------------ /**
* 根据ID更新索引(单条更新).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param obj
* 更新数据
* @param Id
* 更新条件Id
*
*/
public void updateIndex(String indexName, String indexType,
BaseEntity entry, String Id); }

ElasticSearchDaoImpl.java

package com.zbiti.framework.elasticsearch.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import net.sf.json.JSONObject; import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
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.facet.FacetBuilders;
import org.elasticsearch.search.facet.terms.TermsFacet;
import org.elasticsearch.search.facet.terms.TermsFacetBuilder;
import org.elasticsearch.search.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Service; import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zbiti.framework.base.entity.BaseEntity; /**
* ElasticSearch操作接口实现
*
* @author LICHANGWU
*
*/
@Service
public class ElasticSearchDaoImpl implements ElasticSearchDao {
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create(); // -------------------------创建索引----------------------------- /**
* 根据对象实例的json串创建索引.
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param indexId
* 建立索引的唯一标识ID
* @param jsonStr
* 索引对象实例的对应json串
* @author LICHANGWU
*/
public IndexResponse createIndexByJson(String indexName, String indexType,
String indexId, String jsonStr) {
Client client = ElasticSearchClient.getInstance();
IndexResponse response = client.prepareIndex(indexName, indexType,
indexId).setSource(jsonStr).execute().actionGet();
System.out.println(response.getId());
return response; } /**
* 根据对象集合批量索引(多条批量)
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param surplusGoodsEntitys
* 对象集合
*/
public void createIndexsByBeans(String indexName, String indexType,
List<BaseEntity> entry) {
Client client = ElasticSearchClient.getInstance();
BulkRequestBuilder builder = client.prepareBulk();
for (BaseEntity g : entry) {
String jsonStr = gson.toJson(g);
String id = g.getId();
builder.add(client.prepareIndex(indexName, indexType, id)
.setSource(jsonStr).request());
}
builder.execute().actionGet(); } // ------------------------------------查询------------------------------------------------- /**
* 根据索引唯一标识ID查找对象.
*
* @param indexName
* 索引组名
*
* @param indexType
* 索引类型
* @param queryName
* 查询字段名
* @param queryValue
* 查询字段值
* @param PageInfo
* 分页情况
*
* @author LICHANGWU
*/
public List<Object> searchById(String indexName, String indexType,
String queryName, String queryValue, PageInfo pageInfo) {
Client client = ElasticSearchClient.getInstance(); return null;
} /**
* 根据传入的值进行模糊查询(分页查询).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
*
* @param queryMap
* key 查询条件 value 字段域
* @param pageInfo
* 分页信息
* @return
* @throws Exception
*/
public List search(String indexName, String indexType,
List<QueryBuilder> bqbl, List<FilterBuilder> fbList,
String[] highLighterFields, PageInfo pageInfo, Class cls,
List<Order> orders) throws Exception {
Client client = ElasticSearchClient.getInstance();
int pageIndex = 0;
int pageSize = 0;
int from = 0;
BoolQueryBuilder boolQueryBuilderTotal = QueryBuilders.boolQuery();
if (bqbl != null) {
for (QueryBuilder bqb : bqbl) {
boolQueryBuilderTotal.must(bqb);
}
} SearchRequestBuilder searchRequestBuilder = client.prepareSearch(
indexName).setTypes(indexType);
if (fbList != null) {
for (FilterBuilder fb : fbList) {
searchRequestBuilder.setFilter(fb);
}
}
searchRequestBuilder.setQuery(boolQueryBuilderTotal).setExplain(true);
if (pageInfo != null) {
pageIndex = pageInfo.getCurrPage();
pageSize = pageInfo.getPageSize();
from = (pageIndex - 1) * pageSize;
searchRequestBuilder.setFrom(from).setSize(pageSize);
} if (orders != null && orders.size() > 0) {
/* 如果需要排序 */
for (Order order : orders) {
if (!"".equals(order.getField()) && null != order.getField()) {
org.elasticsearch.search.sort.SortOrder sortOrder = "desc"
.equals(order.getSort()) ? SortOrder.DESC
: SortOrder.ASC;
searchRequestBuilder = searchRequestBuilder.addSort(order
.getField(), sortOrder);
}
} }
if (highLighterFields != null && highLighterFields.length > 0) {
String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
if(!"genernalReport".equals(methodName)){ //非导出excel表格时,添加样式
// 设置高亮显示
for (String field : highLighterFields) {
searchRequestBuilder.addHighlightedField(field);
}
searchRequestBuilder.setHighlighterPreTags("<span style=\"color:red;\">");
searchRequestBuilder.setHighlighterPostTags("</span>");
}
}
SearchResponse searchResponse = searchRequestBuilder.execute()
.actionGet(); SearchHits hits = searchResponse.getHits(); SearchHit[] hts = hits.getHits();
List list = convert(hts, cls);
if (pageInfo != null) {
pageInfo.setTotal((int) hits.getTotalHits());
pageInfo.setResult(list);
}
return list;
} /**
* 根据传入的值进行模糊查询(分组查询个数).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
*
* @param queryMap
* key 查询条件 value 字段域
* @param pageInfo
* 分页信息
*
* @param groupFiled
* 分页字段
* @return
* @throws Exception
*/
public List searchCount(String indexName, String indexType,
List<QueryBuilder> bqbl, List<FilterBuilder> fbList,PageInfo pageInfo,String groupFiled){
Client client = ElasticSearchClient.getInstance();
int pageIndex = 0;
int pageSize = 0;
int from = 0;
BoolQueryBuilder boolQueryBuilderTotal = QueryBuilders.boolQuery();
if (bqbl != null) {
for (QueryBuilder bqb : bqbl) {
boolQueryBuilderTotal.must(bqb);
}
} SearchRequestBuilder searchRequestBuilder = client.prepareSearch(
indexName).setTypes(indexType);
if (fbList != null) {
for (FilterBuilder fb : fbList) {
searchRequestBuilder.setFilter(fb);
}
} searchRequestBuilder.setQuery(boolQueryBuilderTotal).setExplain(true);
if (pageInfo != null) {
pageIndex = pageInfo.getCurrPage();
pageSize = pageInfo.getPageSize();
from = (pageIndex - 1) * pageSize;
searchRequestBuilder.setFrom(from).setSize(pageSize);
}
TermsFacetBuilder facetBuilder = FacetBuilders.termsFacet(groupFiled).field(groupFiled).size(Integer.MAX_VALUE);
SearchResponse searchResponse = searchRequestBuilder.addFacet(facetBuilder).execute()
.actionGet(); List<Map> groupList = new ArrayList<Map>();
List<Map> resultList = new ArrayList<Map>();
TermsFacet tf = (TermsFacet)searchResponse.getFacets().facetsAsMap().get(groupFiled);
for (TermsFacet.Entry entry : tf) {
Map<String,Object> modelMap = new HashMap<String,Object>();
modelMap.put(groupFiled, entry.getTerm().toString());
modelMap.put("count", entry.getCount());
groupList.add(modelMap);
}
for(int i=0; i<10; i++){
int index = from + i;
if(index >= groupList.size()){
break;
}
resultList.add(groupList.get(index));
}
if (pageInfo != null) {
pageInfo.setTotal(groupList.size());
pageInfo.setResult(resultList);
}
return resultList;
} /**
* 根据传入的值进行模糊查询(分页查询).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
*
* @param queryMap
* key 查询条件 value 字段域
* @param pageInfo
* 分页信息
* @return
* @throws Exception
*/
public SearchHits searchByOnlyValue(String indexName, String indexType,
String queryValue, PageInfo pageInfo) {
Client client = ElasticSearchClient.getInstance();
int pageIndex = pageInfo.getCurrPage();
int pageSize = pageInfo.getPageSize();
int from = (pageIndex - 1) * pageSize;
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
QueryBuilder qu = QueryBuilders.fuzzyQuery("deviceName", queryValue);
boolQueryBuilder.should(qu);
SearchRequestBuilder searchRequestBuilder = client.prepareSearch(
indexName).setTypes(indexType).setQuery(boolQueryBuilder)
.setFrom(from).setSize(pageSize).addHighlightedField(
"deviceName").setHighlighterEncoder("UTF-8")
.setHighlighterPreTags("<font class='font-highlight'>")
.setHighlighterPostTags("</font>").setExplain(true);
SearchResponse searchResponse = searchRequestBuilder.execute()
.actionGet();
SearchHits hits = searchResponse.getHits(); return hits;
} /**
* 转化成实例对象的集合
*
* @param hts
* @param cls
* @return
* @throws Exception
*/
private List convert(SearchHit[] hts, Class cls) throws Exception {
List list = new ArrayList();
List<Field> fields = new ArrayList<Field>();
getAllFields(cls, fields); for (SearchHit sh : hts) {
Object obj = cls.newInstance();
// 获取对应的高亮域
Map<String, HighlightField> result = sh.highlightFields();
for (Field f : fields) {
Object value = "";
if (result != null && result.size() > 0) {
// 从设定的高亮域中取得指定域
HighlightField titleField = result.get(f.getName());
if (titleField != null) {
// 取得定义的高亮标签
Text[] titleTexts = titleField.fragments();
// 为title串值增加自定义的高亮标签
for (Text text : titleTexts) {
value = String.valueOf(value) + text;
}
}
}
if ("".equals(value) || null == value) {
value = sh.getSource().get(f.getName());
} Class<?> type = f.getType();
Method m = cls.getMethod("set".concat(
f.getName().substring(0, 1).toUpperCase()).concat(
f.getName().substring(1)), new Class[] { type }); if (m != null) {
if (value != null && !"".equals(value)) {
if (type == String.class) {
m.invoke(obj,
new Object[] { String.valueOf(value) });
} else if (type == Integer.class || type == int.class) {
m.invoke(obj, new Object[] { new Integer(String
.valueOf(value)) });
} else if (type == Double.class || type == double.class) {
m.invoke(obj, new Object[] { new Double(String
.valueOf(value)) });
} else if (type == Date.class) {
Map timeMap = (Map) value;
Long time = (Long) timeMap.get("time");
m.invoke(obj, new Object[] { new Date(time) });
} else if (type == Boolean.class
|| type == boolean.class) {
m.invoke(obj, new Object[] { new Boolean(String
.valueOf(value)) });
} else if (type == Long.class || type == long.class) {
m.invoke(obj, new Object[] { new Long(String
.valueOf(value)) });
} else {
throw new Exception("暂时无法赋值到" + type.getName()
+ "类型的属性中");
}
}
}
}
list.add(obj);
}
return list;
} /**
* 递归获取类所有属性包含父类属性
*
* @param cls
* @param list
*/
private void getAllFields(Class cls, List<Field> list) {
Field[] f = cls.getDeclaredFields();
List<Field> l = Arrays.asList(f);
list.addAll(l); Class superc = cls.getSuperclass();
if (superc != null) {
getAllFields(superc, list);
}
} // -----------------------删除---(暂且完结)----------------------
/**
* 根据指定属性键值对删除索引(查询删除).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param name
* 删除依据的字段属性名
* @param value
* 删除依据的字段属性值
*/
public DeleteByQueryResponse deleteIndexByNameAndValue(String indexName,
String indexType, String name, String value) {
Client client = ElasticSearchClient.getInstance();
QueryBuilder queryBuiler = QueryBuilders.fieldQuery(name, value);
DeleteByQueryResponse response = client.prepareDeleteByQuery(indexName)
.setQuery(queryBuiler).execute().actionGet();
return response;
} /**
* 根据索引的Id删除索引.
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param Id
* 指定索引文档Id
*/
public DeleteResponse deleteIndexById(String indexName, String indexType,
String Id) {
Client client = ElasticSearchClient.getInstance();
DeleteResponse response = client
.prepareDelete(indexName, indexType, Id).execute().actionGet();
return response; } /**
* 根据索引的Id集合删除索引(批量删除).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param Ids
* 指定索引文档Id集合
*/
public void deleteIndexByIds(String indexName, String indexType,
List<String> Ids) {
Client client = ElasticSearchClient.getInstance();
BulkRequestBuilder builder = client.prepareBulk();
for (String id : Ids) {
builder.add(client.prepareDelete(indexName, indexType, id)
.request());
}
builder.execute().actionGet();
} /**
* 删除确定索引组和索引类型下所有数据(危险,慎用).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
*/
public void deleteAllIndex(String indexName, String indxType) {
Client client = ElasticSearchClient.getInstance();
client.prepareDeleteByQuery(indexName).setTypes(indxType).setQuery(
QueryBuilders.matchAllQuery()).execute().actionGet(); } /**
* 根据ID更新索引(单条更新).
*
* @param indexName
* 索引组名
* @param indexType
* 索引类型
* @param obj
* 更新数据
* @param Id
* 更新条件Id
*
*/
public void updateIndex(String indexName, String indexType,
BaseEntity entry, String Id) {
Client client = ElasticSearchClient.getInstance();
client.prepareDelete(indexName, indexType, Id).execute().actionGet();
JSONObject jsonstr = JSONObject.fromObject(entry);
IndexResponse response = client.prepareIndex(indexName, indexType, Id)
.setSource(jsonstr.toString()).execute().actionGet();
System.out.println(response.getId()); } public static void main(String[] args) {
ElasticSearchDaoImpl ed = new ElasticSearchDaoImpl();
// ed.deleteAllIndex("supdem_index","supdem_index_type");
} }

Order.java

package com.zbiti.framework.elasticsearch.utils;

public class Order {

    private String field;

    private String sort;

    public Order(){}

    public Order(String field,String sort){
this.field = field;
this.sort = sort;
} public String getField() {
return field;
} public void setField(String field) {
this.field = field;
} public String getSort() {
return sort;
} public void setSort(String sort) {
this.sort = sort;
} }

OrderInfo.java

package com.zbiti.framework.elasticsearch.utils;

public class OrderInfo {

    private String field;
private boolean sort; public String getField() {
return field;
} public void setField(String field) {
this.field = field;
} public boolean isSort() {
return sort;
} public void setSort(boolean sort) {
this.sort = sort;
}
}

PageInfo.java

package com.zbiti.framework.elasticsearch.utils;

import java.util.List;

/**
* ElasticSearch查询分页信息
*
* @author LICHANGWU
*
*/
public class PageInfo { private int pageSize;
private int total;
private int totalPage;
private int currPage; private List result; public PageInfo(){ } public PageInfo(int currPage,int pageSize){
this.currPage= currPage;
this.pageSize = pageSize;
} public int getPageSize() {
return pageSize;
} public void setPageSize(int pageSize) {
this.pageSize = pageSize;
} public int getTotal() {
return total;
} public void setTotal(int total) {
this.total = total;
this.totalPage = this.total%this.pageSize == 0?this.total/this.pageSize : this.total/this.pageSize + 1;
} public int getTotalPage() {
return totalPage;
} public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
} public int getCurrPage() {
return currPage;
} public void setCurrPage(int currPage) {
this.currPage = currPage;
} public List getResult() {
return result;
} public void setResult(List result) {
this.result = result;
}
}

SearchEsUtil.java

package com.zbiti.framework.elasticsearch.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set; import org.elasticsearch.index.query.BoolFilterBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; public class SearchEsUtil { /**
* 多个查询条件在一个域中查询 :或关系
* @param query
* @return
*/
public static QueryBuilder appendMulToOneOrRelation(String[] query,String queryArea){
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
for(String que : query){
QueryBuilder qu =QueryBuilders.fieldQuery(queryArea,"\""+que+"\"").analyzer(null).analyzeWildcard(false);
boolQueryBuilder.should(qu);
}
return boolQueryBuilder;
} /**
* 一个查询条件在多个域中查询 :或关系
* @param query
* @return
*/
public static QueryBuilder appendOneToMulOrRelation(String query,String[] queryArea,boolean fz){
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
if(!fz){
query = "\""+query+"\"";
for(String que : queryArea){
QueryBuilder qu =QueryBuilders.fieldQuery(que,query).analyzer(null).analyzeWildcard(false);
boolQueryBuilder.should(qu);
}
return boolQueryBuilder;
}else{
MultiMatchQueryBuilder mulqueryBuilder = QueryBuilders.multiMatchQuery(query, queryArea);
BoolQueryBuilder builder = boolQueryBuilder.should(mulqueryBuilder);
return builder;
}
} /**
* 一个查询条件在一个域中查询:且关系
* @param query
* @param queryArea
* @return
*/
public static QueryBuilder appendOneToOneAndRelation(String query,String queryArea){
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
QueryBuilder qu =QueryBuilders.fieldQuery(queryArea,"\""+query+"\"").analyzer(null).analyzeWildcard(false);
boolQueryBuilder.must(qu);
return boolQueryBuilder;
} /**
* 时间过滤
* @param start
* @param end
* @param queryArea
* @return
*/
public static FilterBuilder appendTimeRelation(String start,String end,String queryArea){
BoolFilterBuilder qu = FilterBuilders.boolFilter().must(FilterBuilders.rangeFilter(queryArea).from(start).to(end));
return qu;
} /**
* 组合查询条件分组转化
* @param mulQuery
* @return
*/
public static Map<String,String[]> mulQueryToMap(String mulQuery){
Map<String,String[]> mulMap = new HashMap<String,String[]>();
if(null != mulQuery && !"".equals(mulQuery)){
String[] querys = mulQuery.split(",");
Set<String> keyS = new HashSet<String>();
List<String> vals = new ArrayList<String>();
for(String query:querys){
if(null != query && !"".equals(query)){
String[] s = query.split("_");
keyS.add(s[0]);
vals.add(query);
}
}
for(String key:keyS){
List<String> tol = new ArrayList<String>();
for(String val : vals){
if(val.contains(key)){
tol.add(val.split("_")[1]);
}
}
mulMap.put(key,tol.toArray(new String[tol.size()]));
}
}
return mulMap;
}
}

SurplusGoodsEntity.java

package com.zbiti.framework.elasticsearch.utils;

import java.io.Serializable;
import java.util.Date; /**
* 闲置资源实体类.
*
* @author LICHANGWU
*
*/
public class SurplusGoodsEntity implements Serializable { /**
* serialVersionUID.
*/
private static final long serialVersionUID = -5266853903541167616L;
/**
* 唯一标识ID
*/
private String id; /**
* 正式库资源ID.
*/
private int deviceId;
/**
* 资源编号.
*/
private String deviceNo;
/**
* 专业.
*/
private String specialty;
/**
* 网元.
*/
private String netType;
/**
* 设备型号.
*/
private int modelId;
/**
* 资产目录.
*/
private int assId;
/**
* 设备名称.
*/
private String deviceName;
/**
* 干线级别.
*/
private String trunkLine;
/**
* 公司编码.
*/
private String companyCode;
/**
* 部门编码.
*/
private String departmentCode;
/**
* 维护人.
*/
private String maintenceMan;
/**
* 存放位置.
*/ private String place;
/**
* 原值.
*/
private double originalValue;
/**
* 净值.
*/
private double netValue;
/**
* 数据来源.
*/
private String dataSource;
/**
* 开始使用时间.
*/
private Date useDate;
/**
* 开始闲置时间.
*/
private Date beginDate;
/**
* 资源卡片编号.
*/
private String cardNo;
/**
* 资产属性.
*/
private String assetProperty;
/**
* 计量单位.
*/
private String unit;
/**
* 录入方式.
*/
private String inputWay;
/**
* 闲置原因.
*/
private String leaveuseWhy;
/**
* 使用年限.
*/
private String liftSpan;
/**
* 库存.
*/
private int ctockNum;
/**
* 是否可用.
*/
private String disable;
/**
* 调拨次数.
*/
private String allocateCount;
/**
* 是否上架.
*/
private String isShelves;
/**
* 浏览次数.
*/
private int browseCount;
/**
* 点击量.
*/
private int scanTotal;
/**
* 排序.
*/
private int sort; /**
* 获取唯一标识ID.
*/
public String getId() {
return id;
} /**
* 设置唯一标识ID.
*/
public void setId(String id) {
this.id = id;
} /**
* 获取正式库资源ID.
*/
public int getDeviceId() {
return deviceId;
} /**
* 设置正式库资源ID.
*/
public void setDeviceId(int deviceId) {
this.deviceId = deviceId;
} /**
* 获取资源编号.
*/
public String getDeviceNo() {
return deviceNo;
} /**
*设置 资源编号.
*/
public void setDeviceNo(String deviceNo) {
this.deviceNo = deviceNo;
} /**
* 获取专业.
*/
public String getSpecialty() {
return specialty;
} /**
* 设置专业.
*/
public void setSpecialty(String specialty) {
this.specialty = specialty;
} /**
* 获取网元.
*/
public String getNetType() {
return netType;
} /**
*设置 网元.
*/
public void setNetType(String netType) {
this.netType = netType;
} /**
* 获取设备型号.
*/
public int getModelId() {
return modelId;
} /**
* 设置设备型号.
*/
public void setModelId(int modelId) {
this.modelId = modelId;
} /**
* 获取资产目录.
*/
public int getAssId() {
return assId;
} /**
* 设置资产目录.
*/
public void setAssId(int assId) {
this.assId = assId;
} /**
* 获取设备名称.
*/
public String getDeviceName() {
return deviceName;
} /**
* 设置设备名称.
*/
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
} /**
* 获取干线级别.
*/
public String getTrunkLine() {
return trunkLine;
} /**
* 设置干线级别.
*/
public void setTrunkLine(String trunkLine) {
this.trunkLine = trunkLine;
} /**
* 获取公司编码.
*/
public String getCompanyCode() {
return companyCode;
} /**
* 设置公司编码.
*/
public void setCompanyCode(String companyCode) {
this.companyCode = companyCode;
} /**
* 获取部门编码.
*/
public String getDepartmentCode() {
return departmentCode;
} /**
* 设置部门编码.
*/
public void setDepartmentCode(String departmentCode) {
this.departmentCode = departmentCode;
} /**
* 获取维护人.
*/
public String getMaintenceMan() {
return maintenceMan;
} /**
* 设置维护人.
*/
public void setMaintenceMan(String maintenceMan) {
this.maintenceMan = maintenceMan;
} /**
* 获取存放位置.
*/
public String getPlace() {
return place;
} /**
* 设置存放位置.
*/
public void setPlace(String place) {
this.place = place;
} /**
* 获取原值.
*/
public double getOriginalValue() {
return originalValue;
} /**
*设置 原值.
*/
public void setOriginalValue(double originalValue) {
this.originalValue = originalValue;
} /**
* 获取净值.
*/
public double getNetValue() {
return netValue;
} /**
* 设置净值.
*/
public void setNetValue(double netValue) {
this.netValue = netValue;
} /**
* 获取数据来源.
*/
public String getDataSource() {
return dataSource;
} /**
* 设置数据来源.
*/
public void setDataSource(String dataSource) {
this.dataSource = dataSource;
} /**
* 获取开始使用时间.
*/
public Date getUseDate() {
return useDate;
} /**
* 设置开始使用时间.
*/
public void setUseDate(Date useDate) {
this.useDate = useDate;
} /**
* 获取开始闲置时间.
*/
public Date getBeginDate() {
return beginDate;
} /**
* 设置开始闲置时间.
*/
public void setBeginDate(Date beginDate) {
this.beginDate = beginDate;
} /**
* 获取资源卡片编号.
*/
public String getCardNo() {
return cardNo;
} /**
*设置 资源卡片编号.
*/
public void setCardNo(String cardNo) {
this.cardNo = cardNo;
} /**
* 获取资产属性.
*/
public String getAssetProperty() {
return assetProperty;
} /**
* 设置资产属性.
*/
public void setAssetProperty(String assetProperty) {
this.assetProperty = assetProperty;
} /**
* 获取计量单位.
*/
public String getUnit() {
return unit;
} /**
* 设置计量单位.
*/
public void setUnit(String unit) {
this.unit = unit;
} /**
* 获取录入方式.
*/
public String getInputWay() {
return inputWay;
} /**
* 设置录入方式.
*/
public void setInputWay(String inputWay) {
this.inputWay = inputWay;
} /**
* 获取闲置原因.
*/
public String getLeaveuseWhy() {
return leaveuseWhy;
} /**
* 设置闲置原因.
*/
public void setLeaveuseWhy(String leaveuseWhy) {
this.leaveuseWhy = leaveuseWhy;
} /**
* 获取使用年限.
*/
public String getLiftSpan() {
return liftSpan;
} /**
* 设置使用年限.
*/
public void setLiftSpan(String liftSpan) {
this.liftSpan = liftSpan;
} /**
* 获取库存.
*/
public int getCtockNum() {
return ctockNum;
} /**
* 设置库存.
*/
public void setCtockNum(int ctockNum) {
this.ctockNum = ctockNum;
} /**
* 获取是否可用.
*/
public String getDisable() {
return disable;
} /**
* 设置是否可用.
*/
public void setDisable(String disable) {
this.disable = disable;
} /**
* 获取调拨次数.
*/
public String getAllocateCount() {
return allocateCount;
} /**
* 设置调拨次数.
*/
public void setAllocateCount(String allocateCount) {
this.allocateCount = allocateCount;
} /**
* 获取是否上架.
*/
public String getIsShelves() {
return isShelves;
} /**
* 设置是否上架.
*/ public void setIsShelves(String isShelves) {
this.isShelves = isShelves;
} /**
*获取 浏览次数.
*/
public int getBrowseCount() {
return browseCount;
} /**
* 设置浏览次数.
*/
public void setBrowseCount(int browseCount) {
this.browseCount = browseCount;
} /**
* 获取点击量.
*/
public int getScanTotal() {
return scanTotal;
} /**
* 设置点击量.
*/
public void setScanTotal(int scanTotal) {
this.scanTotal = scanTotal;
} /**
* 获取排序.
*/
public int getSort() {
return sort;
} /**
* 设置排序.
*/
public void setSort(int sort) {
this.sort = sort;
} }

PropertyManager.java

package com.zbiti.framework.elasticsearch.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties; /**
* 资源文件管理
*
* @author LICHANGWU
*
*/
public class PropertyManager {
private static Map<String, String> ctxPropertiesMap = null;
static {
try { String path = PropertyManager.class.getResource("/").getPath(); File dic = new File(path);
ctxPropertiesMap = new HashMap<String, String>();
for (File file : dic.listFiles()) {
if (file.getName().endsWith(".properties")) {
Properties prop = new Properties();
prop.load(new FileInputStream(file));
Enumeration<Object> keys = prop.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement().toString();
String value = prop.getProperty(key);
value = new String(value.getBytes("ISO-8859-1"),"UTF-8");
ctxPropertiesMap.put(key, value);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} /**
* 获取配置属
*
* @param name
* @return String
*/
public static String getContextProperty(String name) {
return (String) ctxPropertiesMap.get(name);
}
}

elasticsearch代码片段,及工具类SearchEsUtil.java的更多相关文章

  1. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  2. Java日期工具类,Java时间工具类,Java时间格式化

    Java日期工具类,Java时间工具类,Java时间格式化 >>>>>>>>>>>>>>>>>&g ...

  3. Java字符串转16 进制工具类Hex.java

    Java字符串转16 进制工具类Hex.java 学习了:https://blog.csdn.net/jia635/article/details/56678086 package com.strin ...

  4. Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie

    Java Cookie工具类,Java CookieUtils 工具类,Java如何增加Cookie >>>>>>>>>>>>& ...

  5. UrlUtils工具类,Java URL工具类,Java URL链接工具类

    UrlUtils工具类,Java URL工具类,Java URL链接工具类 >>>>>>>>>>>>>>>&g ...

  6. [Google Guava] 2.3-强大的集合工具类:java.util.Collections中未包含的集合工具

    原文链接 译文链接 译者:沈义扬,校对:丁一 尚未完成: Queues, Tables工具类 任何对JDK集合框架有经验的程序员都熟悉和喜欢java.util.Collections包含的工具方法.G ...

  7. java基础课程笔记 static 主函数 静态工具类 classpath java文档注释 静态代码块 对象初始化过程 设计模式 继承 子父类中的函数 继承中的构造函数 对象转型 多态 封装 抽象类 final 接口 包 jar包

    Static那些事儿 Static关键字 被static修饰的变量成为静态变量(类变量) 作用:是一个修饰符,用于修饰成员(成员变量,成员方法) 1.被static修饰后的成员变量只有一份 2.当成员 ...

  8. Android开发之SDCardUtils工具类。java工具详细代码,附源代码。判断SD卡是否挂载等功能

    package com.xiaobing.zhbj.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputSt ...

  9. 正则表达式验证工具类RegexUtils.java

    Java 表单注册常用正则表达式验证工具类,常用正则表达式大集合. 1. 电话号码 2. 邮编 3. QQ 4. E-mail 5. 手机号码 6. URL 7. 是否为数字 8. 是否为中文 9. ...

随机推荐

  1. Linux下利用json-c从一个json数组中提取每一个元素中的部分字段组成一个新json数组

    先把代码贴上来,有时间整理一下 首先说一下要实现的功能: 假定现在有一个json格式的字符串,而且他是一个josn中的数组,比如: [ { "id": "NEW20170 ...

  2. Oracle Profile 的简单说明

    1. 查看已经有的oracle 的profile 首先profile的解释 我理解为 是一个 简略的配置文件, 跟linux的 bash文件的配置信息类似 bash_profile . select ...

  3. SQLSERVER 查看操作系统内存

    1. 通过系统试图查看内存信息 SELECT total_physical_memory_kb / AS [物理内存(MB)] , available_physical_memory_kb / AS ...

  4. android管理SD卡 mksdcard

    在创建Android模拟器的时候,会创建一个虚拟的sd卡.我们还可以通过mksdcard命令创建sd卡,在运行模拟器的时候,可以选择具体的sd卡. 1.创建sd卡: mksdcard [-l labl ...

  5. DELPHI动态创建窗体

    //第一种方式 procedure TForm1.btn1Click(Sender: TObject); begin With TForm2.Create(Application) do Try Sh ...

  6. JSONP使用及注意事项小结

    什么是JSONP 三句话总结: 概念:JSONP(JSON with Padding)是JSON的一种"使用模式". 目的:用于解决主流浏览器的跨域数据访问的问题. 原理:利用 & ...

  7. 基于Vue-cli 快速搭建项目

    Vue-cli可以快速帮助我们创建一个项目,这是官方给我们提供的脚手架.下面我说一下vue-cli的使用方法. 一.准备工作 在使用vue-cli时,首先需要安装nodejs,npm,其次需全局安装v ...

  8. 别在int与float上栽跟头

    int与float是我们每天编程都用的两种类型,但是我们真的足够了解它们吗.昨天在博客园看到一个比较老的笑话: “昨天晚上下班回家,一民警迎面巡逻而来.突然对我大喊:站住!民警:int 类型占几个字节 ...

  9. 【BZOJ1028】[JSOI2007]麻将(贪心)

    [BZOJ1028][JSOI2007]麻将(贪心) 题面 BZOJ 洛谷 题解 感觉好久没打过麻将了,似乎都快不会打了. 这个数据范围看着就觉得是\(O(n^2m)\). 那么就枚举听哪张牌,然后枚 ...

  10. cf1066F Yet Another 2D Walking (贪心+dijkstra)

    易证我们走的时候只会从某一层的某端点走向另一端点.然后走向下一层的某端点.. 所以建图然后dijkstra就行了 调了一年以后发现dijkstra写错了 #include<bits/stdc++ ...