spring cloud集成es

package com.jd.bds.stream.statistics.config;

import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

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

@Configuration
public class EsBuilder {
    private Logger logger = LoggerFactory.getLogger(EsBuilder.class);

    @Value("${elasticsearch.host}")
    private String host;

    @Value("${elasticsearch.port}")
    private int port;

    @Value("${elasticsearch.cluster.name}")
    private String clusterName;
    /**
     *
     * @author
     * @return
     * @throws UnknownHostException
     */
    @Bean
    public Client build() throws UnknownHostException {
        Settings settings = Settings.settingsBuilder().put("cluster.name", clusterName).put("client.transport.sniff", false).build();
        TransportClient client = TransportClient.builder().settings(settings).build();
        client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(host), port));
        logger.info("es init info clusterName:{} host :{} port :{}",clusterName,host,port);
        return client;
    }
}

 
  


    org.elasticsearch
    elasticsearch
    ${es.version}

2.1.2
  BoolQueryBuilder queryBuilder = BoolQueryUtils.getQueryBuilder();
    if (url != null) {
        queryBuilder.must(QueryBuilders.wildcardQuery("page_url", "*" + url + "*"));
    }
    if (clstagId != null) {
        queryBuilder.must(QueryBuilders.termQuery("", clstagId));
    }
    TermsBuilder tb = AggregationBuilders.terms("")
            .field("").size(1000).executionHint("map");
    TermsBuilder idBuilder = AggregationBuilders.terms("").field("")
            .size(500).order(Terms.Order.aggregation("",false)).executionHint("");
    tb.subAggregation(idBuilder.subAggregation(AggregationBuilders.sum("").field("")));
    SearchRequestBuilder builder = this.client.prepareSearch(UseBehaviorEnum.CLICK_EVENT.getIndex())
            .setTypes(UseBehaviorEnum.CLICK_EVENT.getType())
            .setQuery(queryBuilder)
            .addAggregation(tb);
    logger.info("requset is:{}", builder.toString());
    SearchResponse response = builder.get();
    Terms terms = response.getAggregations().get("");
    List buckets = terms.getBuckets();
    List result = new ArrayList();
    Iterator iterator = buckets.iterator();
    while (iterator.hasNext()) {
        Terms.Bucket bucket = iterator.next();
        Terms idTerms = (Terms) bucket.getAggregations().asMap().get("");
        Iterator idIterator = idTerms.getBuckets().iterator();
        /**封装对象*/
        while (idIterator.hasNext()) {
            ClickEventBean clickEventBean = new ClickEventBean();
            clickEventBean.setUrl(bucket.getKeyAsString());
            Terms.Bucket idBucket = idIterator.next();
            Map idMap = idBucket.getAggregations().asMap();
            clickEventBean.setClstaId(idBucket.getKeyAsString());
            clickEventBean.setClstagCount(((InternalSum) idMap.get("")).value());
            result.add(clickEventBean);
        }

    }
    return result;
}
 public List getData(BoolQueryBuilder queryBuilder,WebEnum useBehaviorEnum, String order){
        SearchRequestBuilder request = this.client.prepareSearch(useBehaviorEnum.getIndex())
                .setTypes(useBehaviorEnum.getType())
                .setFrom(0).setSize(9000)
                .setQuery(queryBuilder);
        if(StringUtils.isNotBlank(order)){
            request.addSort("pv", SortOrder.DESC);
        }

        logger.info("request is : {}",request);
        SearchResponse response = request.get();
        List result = new ArrayList();
        response.getHits().forEach(e->result.add(HumpConverter.formatHumpName(e.getSource())));
        return result;
    }


 
 

你可能感兴趣的:(spring cloud集成es)