码迷,mamicode.com
首页 > 编程语言 > 详细

elasticsearch代码片段,及工具类SearchEsUtil.java

时间:2018-07-20 01:19:47      阅读:364      评论:0      收藏:0      [点我收藏+]

标签:浏览次数   prope   gen   条件   ||   cse   获取   multi   timeout   

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

标签:浏览次数   prope   gen   条件   ||   cse   获取   multi   timeout   

原文地址:https://www.cnblogs.com/xyhero/p/9339200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!