Hadoop -- ES -- CURD

1.获取ES连接

package com.ciic.history.common;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.transport.TransportAddress;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class DaoUtilL {

    private static TransportClient clientL = null;

    private DaoUtilL(){};

    public static TransportClient getClientL() {
        try {
            if (null != clientL) {
                return clientL;
            }
            Settings settings = Settings.settingsBuilder().put("cluster.name", "search1")
                    .put("transport.tcp.compress", true).build();
            InetSocketTransportAddress address1 = new InetSocketTransportAddress(InetAddress.getByName("192.168.43.249"), 9300);
            InetSocketTransportAddress address2 = new InetSocketTransportAddress(InetAddress.getByName("192.168.43.250"), 9300);
            InetSocketTransportAddress address3 = new InetSocketTransportAddress(InetAddress.getByName("192.168.43.251"), 9300);
            InetSocketTransportAddress address4 = new InetSocketTransportAddress(InetAddress.getByName("192.168.43.252"), 9300);
            TransportAddress[] addressArr = {address1, address2, address3, address4};
            clientL = TransportClient.builder().settings(settings).build().addTransportAddresses(addressArr);
            return clientL;
        } catch (Exception e) {
            return null;
        }
    }
}

 

2.CURD操作

    @Override
    public JsonEntity queryCustomerPreview(int page, int rows) {
        TransportClient clientL = DaoUtilL.getClientL();
        SearchRequestBuilder builder =  clientL.prepareSearch();
        builder.setIndices("esinner_limecustomerpreview_index").
                setTypes("xian").
                addSort("imscustomername", SortOrder.ASC).setFrom((page-1)*rows).setSize(rows);

        SearchResponse response = builder.get();
        SearchHit[] searchHits = response.getHits().getHits();
        ArrayList arrayList = new ArrayList();
        for(int i = 0; i < searchHits.length; i++) {
            arrayList.add(searchHits[i].getSource());
        }
        //查询总记录数total
        SearchResponse totalResponse = clientL.prepareSearch()
                .setIndices("esinner_limecustomerpreview_index")
                .setSearchType(SearchType.COUNT).setSize(0).get();
        long length = response.getHits().totalHits();

        JsonEntity entity = new JsonEntity();
        entity.setTotal(length);
        entity.setRows(arrayList);
        return entity;
    }

-- -- -- -- -- -- 

    @Override
    public JsonEntity customerPreviewSearch(EsinnerLimeCustomerPreviewIndex customerPreview, int page, int rows) {
        TransportClient clientL = DaoUtilL.getClientL();
        SearchRequestBuilder searchRequestBuilder =  clientL.prepareSearch();
        searchRequestBuilder.setIndices("esinner_limecustomerpreview_index").
                setTypes("xian").setSearchType(SearchType.DEFAULT).
                setFrom((page-1)*rows).setSize(rows);
        
        BoolQueryBuilder builder =  QueryBuilders.boolQuery();

        boolean flag=true;
        if(StringUtils.isNotBlank(customerPreview.getImscustomername())){
            flag=false;
            builder.must(QueryBuilders.termQuery("imscustomername",customerPreview.getImscustomername()));
        }
        if(StringUtils.isNotBlank(customerPreview.getImscustomercode())){
            flag=false;
            builder.must(QueryBuilders.matchQuery("imscustomercode",customerPreview.getImscustomercode()));
        }
        if(flag==true){
            searchRequestBuilder.addSort("imscustomercode", SortOrder.ASC);
        }
        searchRequestBuilder.setQuery(builder);

        SearchResponse response = searchRequestBuilder.get();
        SearchHit[] searchHits = response.getHits().getHits();
        ArrayList arrayList = new ArrayList();
        for(int i = 0; i < searchHits.length; i++) {
            arrayList.add(searchHits[i].getSource());
        }

        SearchResponse totalResponse = clientL.prepareSearch()
                .setIndices("esinner_limecustomerpreview_index")
                .setSearchType(SearchType.COUNT).setSize(0).get();
        long length = response.getHits().totalHits();
        JsonEntity entity = new JsonEntity();

        entity.setTotal(length);
        entity.setRows(arrayList);

        return entity;
    }

-- -- -- -- -- -- 

3.返回数据

啦啦啦

posted @ 2017-08-03 15:12  limeOracle  阅读(332)  评论(0编辑  收藏  举报