美文网首页
Elasticsearch基本应用(java)API(三)

Elasticsearch基本应用(java)API(三)

作者: yljava | 来源:发表于2017-12-08 11:48 被阅读0次
package com.wiwj.app.search.util;

import com.wiwj.app.common.constant.PropertyConstantsKey;
import com.wiwj.app.common.constant.PropertyTools;
import com.wiwj.app.search.conf.ElasticSearch;
import com.wiwj.app.util.Util;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 
 * <p>Title: ElasticsearchOperUtil.java</p>
 *
 * <p>Package: core.util</p>
 * 
 * <p>Description: ES操作工具类</p>
 * 
 * @author: 
 * 
 * @date: 2017年5月10日 下午2:16:20
 *
 * @version: 1.0
 */
public class ElasticsearchOperUtil {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ElasticsearchOperUtil.class);
    /** ESclient */
    private static Client client = ElasticSearch.getInstance().getESClient();
    
    /**
     * 
     * @Title: deleteByType
     * @Description: 删除某一类型所有数据
     * @author: 
     * @date: 2017年5月26日 下午4:18:21
     * @param index
     * @param type
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean deleteByType(String index,String type){
        //删除es中的视图
        MatchAllQueryBuilder allQueryBuilder = QueryBuilders.matchAllQuery();
        client.prepareDeleteByQuery(index).setQuery(allQueryBuilder).setTypes(type).execute().actionGet();
        return true;
    }
    
    /**
     * 
     * @Title: queryData
     * @Description: 单字段查询
     * @author: 
     * @date: 2017年5月10日 上午10:43:31
     * @param index
     * @param type
     * @param primaryColumn
     * @param primaryColumn_value
     * @return
     */
    public static List<String> queryData(String index,String type,String primaryColumn, String primaryColumn_value){
        try {
            BoolQueryBuilder querySql = QueryBuilders.boolQuery();
            querySql.must(QueryBuilders.termQuery(primaryColumn, primaryColumn_value));
            SearchRequestBuilder searchRequest = client.prepareSearch(index)
                    .setTypes(type).setQuery(querySql).setFrom(0).setSize(10000);
            SearchResponse actionGet = searchRequest.execute().actionGet();
            SearchHits hits = actionGet.getHits();
            String json = "";
            List<String> dataList = new ArrayList<String>();
            for (SearchHit hit : hits) {
                json = hit.getSourceAsString();
                dataList.add(json);
            }
            return dataList;
        } catch (Exception e) {
            return null;
        }
        
    }
    /**
     * 
     * @Title: addData
     * @Description: 添加数据
     * @author: 
     * @date: 2017年5月9日 下午3:05:52
     * @param index
     * @param type
     * @param dataJson
     * @return
     */
    public static boolean addData(String index,String type,String dataJson) throws Exception{
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        IndexRequest request = client.prepareIndex(index, type,"主键ID")//如果不指定主键ID,则es会生成一个随机的主键ID
                .setSource(dataJson).request();
        bulkRequest.add(request);
        BulkResponse bulkResponse = bulkRequest.execute().actionGet();
        if (bulkResponse.hasFailures()) {
            logger.info(bulkResponse.buildFailureMessage());
            return false;
        }
        return true;
    }
    
    /**
     * 
     * @Title: deleteBySingleColumnConditon
     * @Description: 删除 列=value
     * @author: 
     * @date: 2017年5月9日 下午3:01:53
     * @param index
     * @param type
     * @param column
     * @param value
     * @return
     */
    @SuppressWarnings("deprecation")
    public static boolean deleteBySingleColumnConditon(String index,String type,String column,String value) throws Exception{
        QueryBuilder query = QueryBuilders.matchQuery(column, value);
        client.prepareDeleteByQuery(index).setTypes(type).setQuery(query).execute().actionGet();
        return true;
    }
    
    /**
     * <pre>deleteBySingleColumnConditon(根据id=id1,id2删除)   
     * 创建人:
     * 创建时间:2017年7月20日 上午10:51:57    
     * 修改人:
     * 修改时间:2017年7月20日 上午10:51:57    
     * 修改备注: 
     * @param index
     * @param type
     * @param column
     * @param value 多个条件的值集合
     * @return</pre>
     */
    @SuppressWarnings("deprecation")
    public static boolean deleteBySingleColumnConditon(String index,String type,String column,List<String> value){
        try {
            QueryBuilder query = QueryBuilders.termsQuery(column, value);
            client.prepareDeleteByQuery(index).setTypes(type).setQuery(query).execute().actionGet();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @Title: deleteBySingleColumnConditon
     * @Description: 根据多个主键删除
     * @author: 
     * @date: 2017/9/11 18:15
     * @param index
     * @param type
     */
    public static boolean deleteByMultipleColumnConditon(String index,String type,Map<String,List> prama){
        try {
            BoolQueryBuilder querySql = QueryBuilders.boolQuery();
            Set<Map.Entry<String, List>> entries = prama.entrySet();
            for (Map.Entry<String, List> entry : entries) {
                querySql.must(QueryBuilders.termsQuery(entry.getKey(), entry.getValue()));
            }
            client.prepareDeleteByQuery(index).setTypes(type).setQuery(querySql).execute().actionGet();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * 根据map多条件删除
     * @param index
     * @param type
     * @param prama
     * @return
     */
    public static boolean deleteByMultiStringColumnConditon(String index,String type,Map<String,Object> prama){
        try {
            BoolQueryBuilder querySql = QueryBuilders.boolQuery();
            Set<Map.Entry<String, Object>> entries = prama.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                querySql.must(QueryBuilders.termsQuery(entry.getKey(), String.valueOf(entry.getValue())));
            }
            client.prepareDeleteByQuery(index).setTypes(type).setQuery(querySql).execute().actionGet();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    
    /**
     * 
     * @Title: queryAllDataByIndexAndType
     * @Description: 查询所有数据
     * @author: 
     * @date: 2017年5月18日 下午2:31:00
     * @param index
     * @param type
     * @return
     */
    public static List<String> queryAllDataByIndexAndType(String index,String type){
        try {
            BoolQueryBuilder querySql = QueryBuilders.boolQuery();
            SearchRequestBuilder searchRequest = client.prepareSearch(index)
                    .setTypes(type).setQuery(querySql).setFrom(0).setSize(100000);
            SearchResponse actionGet = searchRequest.execute().actionGet();
            SearchHits hits = actionGet.getHits();
            String json = "";
            List<String> dataList = new ArrayList<String>();
            for (SearchHit hit : hits) {
                json = hit.getSourceAsString();
                dataList.add(json);
            }
            return dataList;
        } catch (Exception e) {
            return null;
        }
    }
    
    /**
     * 
     * @Title: deleteData
     * @Description: 根据id删除数据
     * @author: 
     * @date: 2017年6月22日 下午4:44:49
     * @param index
     * @param type
     * @param ids
     */
    @SuppressWarnings("unused")
    public static void deleteDataByids(String index,String type, String ids) throws Exception{
        String[] idArr = ids.split(",");
        for (int i = 0; i < idArr.length; i++) {
            DeleteResponse response = client.prepareDelete(index, type, idArr[i])   
                    .execute()
                    .actionGet();
        }
    }
    
    /**
     * <pre>bulkSyncToEs(批量同步es)   
     * 创建人
     * 创建时间:2017年7月20日 上午10:46:52    
     * 修改人:       
     * 修改时间:2017年7月20日 上午10:46:52    
     * 修改备注: 
     * @param list(包含三个参数,index+type:当前数据对应的es的index+type,json为组装好的json格式数据)</pre>
     */
    public static boolean bulkSyncToEs(List<Map<String,Object>> list) throws Exception{
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        Map<String,Object> bean = null;
        if(null==list || list.size()==0){
            return true;
        }
        for (int i = 0; i < list.size(); i++) {
            bean = list.get(i); 
            String beanJson = Util.null2String(bean.get("json"));
            String index = Util.null2String(bean.get("index"));
            String type = Util.null2String(bean.get("type"));
            IndexRequest request = client.prepareIndex(index, type)
                    .setSource(beanJson).request();
            bulkRequest.add(request);
        }
        BulkResponse bulkResponse = bulkRequest.execute().actionGet();
        if (bulkResponse.hasFailures()) {
            logger.info(bulkResponse.buildFailureMessage());
            return false;
        }
        return true;
    }
    
    /**
     * <pre>bulkSyncToEs(批量同步es)   
     * 创建人:
     * 创建时间:2017年7月20日 上午10:56:05    
     * 修改人      
     * 修改时间:2017年7月20日 上午10:56:05    
     * 修改备注: 
     * @param list 多条json格式数据集合
     * @param index
     * @param type</pre>
     */
    public static boolean bulkSyncToEs(List<String> list, String index, String type) throws  Exception{
        BulkRequestBuilder bulkRequest = client.prepareBulk();
        String beanJson = null;
        if(null==list || list.size()==0){
            return true;
        }
        for (int i = 0; i < list.size(); i++) {
            beanJson = list.get(i); 
            IndexRequest request = client.prepareIndex(index, type)
                    .setSource(beanJson).request();
            bulkRequest.add(request);
        }
        BulkResponse bulkResponse = bulkRequest.execute().actionGet();
        if (bulkResponse.hasFailures()) {
            beanJson = bulkResponse.buildFailureMessage();
            logger.info(beanJson);
            return false;
        }
        return true;
    }

    /**
     * 拼接过滤查询条件
     * @param querySql
     * @param cityid 城市id
     * @param filtertype 类型(1:已售,2:行情,3:小区)
     */
    public static void generateSensitiveFilterQuery(BoolQueryBuilder querySql,Integer cityid,Integer filtertype){
        if(null == cityid){
            return ;
        }
        String key_prefix;
        switch (filtertype){
            case 1:
                key_prefix = PropertyConstantsKey.sensitivefilter_cjrecord_sale_prefix;
                break;
            case 2:
                key_prefix = PropertyConstantsKey.sensitivefilter_market_sale_prefix;
                break;
            case 3:
                key_prefix = PropertyConstantsKey.sensitivefilter_xiaoqu_sale_prefix;
                break;
            case 4:
                key_prefix = PropertyConstantsKey.sensitivefilter_cjrecord_rent_prefix;
                break;
            default:
                key_prefix = null;
                break;
        }
        if(StringUtils.isBlank(key_prefix)){
            return ;
        }
        String sensitive_config = PropertyTools.getProperty(key_prefix+cityid);
        if(StringUtils.isNotBlank(sensitive_config)){
            String [] configArr = sensitive_config.split("-");
            if(configArr[2].equals("0")){
                querySql.must(QueryBuilders.rangeQuery(configArr[0]).lte(configArr[1]));
            }else if(configArr[2].equals("1")){
                querySql.must(QueryBuilders.rangeQuery(configArr[0]).gt(configArr[1]));
            }else if(configArr[2].equals("2")){
                querySql.must(QueryBuilders.rangeQuery(configArr[0]).gte(configArr[1]));
            }else if ((configArr[4].equals("3"))){//房价行情
                BoolQueryBuilder queryhp = new BoolQueryBuilder();
                queryhp.should(QueryBuilders.rangeQuery(configArr[0]).gte(configArr[1]).lte(configArr[2]));
                queryhp.should(QueryBuilders.termQuery(configArr[0],configArr[3]));
                querySql.must(QueryBuilders.filteredQuery(queryhp,null));
            }
        }
    }

    
    public static void main(String[] args) {
        //ElasticsearchOperUtil.deleteDataByids("cjrecordv1", "cjrecord", "AVzOMcFvgSaZyI-JGdBi,AVzOMcFvgSaZyI-JGc15,AVzOMcFvgSaZyI-JGc1t,AVzOMcFvgSaZyI-JGdBj,AVzOMcFvgSaZyI-JGdBh");
        //deleteByType(ElasticConstant.getCityIndexMap().get(20),"whExchangeHouse");
        //deleteByType(ElasticConstant.getCityIndexMap().get(20),"whExchangeWebHouse");
    }

}

相关文章

网友评论

      本文标题:Elasticsearch基本应用(java)API(三)

      本文链接:https://www.haomeiwen.com/subject/kpheixtx.html