Elasticsearchクエリー構成(Elasticsearch+springboot)


このコードの機能はesの中のデータをクエリーしてページングして展示して、使用するjarバージョンは2.4です
おおよその流れ
maven依存の追加
        
            org.elasticsearch
            elasticsearch
            2.4.0
        

propertiesプロパティファイル構成
spring.elasticsearch.host=192.168.101.123
spring.elasticsearch.port=9300

Springのプロファイル、TransportClientオブジェクトの初期化
package com.unioncast.ssp.front.config;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

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

/**
 * ElasticsearchConfig
 *
 * @author zhangzhe
 * @date 2017/2/21 16:33
 */
@Configuration
@PropertySource(value = "classpath:/elasticsearch.properties")
public class ElasticsearchConfig {

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

    private static final Logger LOG = LogManager.getLogger(ElasticsearchConfig.class);

    @Bean
    public TransportClient elasticsearchClient(){	// spring  es        
        TransportClient transportClient = null;

        Settings settings = Settings.settingsBuilder()
                .put("cluster.name", "bigData-cluster").build();
        try {
            transportClient = TransportClient
                    .builder()
                    .settings(settings)
                    .build()
                    .addTransportAddress(
                            new InetSocketTransportAddress(InetAddress.getByName(host), port)
                    );
        } catch (UnknownHostException e) {
            LOG.error("  elasticsearch     ");
        }
        LOG.info("  elasticsearch     ");

        return transportClient;

    }

}

データのクエリー操作の実装クラス
package com.unioncast.ssp.front.service.ssp.elasticsearchData;

import com.unioncast.common.page.Pagination;
import com.unioncast.common.restClient.RestResponse;
import com.unioncast.common.ssp.model.SspAdvertiser;
import com.unioncast.common.ssp.model.SspCreative;
import com.unioncast.ssp.front.service.ssp.SspAdvertiserService;
import com.unioncast.ssp.front.service.ssp.SspCreativeService;
import com.unioncast.ssp.front.service.ssp.SspOrderService;
import com.unioncast.ssp.front.service.ssp.SspPlanService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.UnknownHostException;
import java.util.*;

/**
 * @author zhangzhe
 * @date 2017/2/21 10:25
 */


@Component
public class ElasticsearchADReportImpl implements ElasticsearchADReport {

    private static final Logger LOG = LogManager.getLogger(ElasticsearchADReportImpl.class);

    private static final String INDEX = "hehe";		//es      
    private static final String TYPE = "kafka_type";	//es     
    private static final Integer PAGESIZE = 10;		//        
    private static final String ESC = "ip";		//       
    @Resource
    TransportClient transportClient;		//  es    

    @Resource
    SspAdvertiserService sspAdvertiserService;

    @Resource
    SspOrderService sspOrderService;

    @Resource
    SspPlanService sspPlanService;

    @Resource
    SspCreativeService sspCreativeService;

    //es  
    public Pagination> esADReport(Long accountId, Long advertiserId, Long orderId, Long planId, Long creativeId,
                                                      Integer currentPageNo, String startTime, String endTime)
            throws UnknownHostException {

        List> list = new ArrayList<>();
        Integer totalCount = 0;

        QueryBuilder query = QueryBuilders.boolQuery();

        //  ID	   matchPhraseQuery      ,    matchQuery   ,matchPhrasePrefixQuery        
        if (accountId != null) {
            query = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchPhraseQuery("accountId", accountId));		

        }

        //   ID
        if (advertiserId != null) {
            query = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchPhraseQuery("advertiserId", advertiserId));

        }

        //  ID
        if (orderId != null) {
            query = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchPhraseQuery("orderId", orderId));

        }

        //  ID
        if (planId != null) {
            query = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchPhraseQuery("planId", planId));

        }

        //  ID
        if (creativeId != null) {
            query = QueryBuilders.boolQuery()
                    .must(QueryBuilders.matchPhraseQuery("creativeId", creativeId));

        }

        //  
/*        SearchResponse rsTotal = transportClient.prepareSearch(INDEX).setTypes(TYPE)
                .setSearchType(SearchType.DFS_QUERY_AND_FETCH)
                .setQuery(query)
                .addSort(ESC, SortOrder.DESC)
                .setExplain(true).execute().actionGet();
        if (rsTotal != null && rsTotal.getHits().getHits().length != 0) {
            totalCount = (int) rsTotal.getHits().getTotalHits();
//            System.out.println("totalHits: "+totalHits);
//            totalCount=rsTotal.getHits().getHits().length;
//            System.out.println("totalCount: "+totalCount);
        }*/


        //  	   .setSearchType           ,            
        int pageIndex = (currentPageNo - 1) * PAGESIZE;
        SearchResponse rs = transportClient.prepareSearch(INDEX).setTypes(TYPE)	
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)		//      ,            ,              
                .setQuery(query)				//    
                .addSort(ESC, SortOrder.DESC)	//  ,DESC   
                .setFrom(pageIndex).setSize(PAGESIZE)		//     ( 0  )     
                .setExplain(true).execute().actionGet();


        //       
        if (rs != null && rs.getHits().getHits().length != 0) {
            totalCount = (int) rs.getHits().getTotalHits();		//            ,           
            SearchHit[] hits = rs.getHits().getHits();			//    es      ,           10 
            //List page = page(currentPageNo, PAGESIZE, hits);
            for (SearchHit hit : hits) {
                Map source = hit.getSource();	//             map  ,       string   json  

                //  id     
                Map map = new HashMap();	//     es     id,      name     

                Long sspAdvertiserId = (Long) source.get("sspAdvertiserId");
                if (sspAdvertiserId != null) {
                    String sspAdvertiserName = findSspAdvertiserName(sspAdvertiserId);
                    map.put("sspAdvertiserName", sspAdvertiserName);
                }

                Long sspOrderId = (Long) source.get("sspOrderId");
                if (sspOrderId != null) {
                    String sspOrderName = findSspOrderName(sspOrderId);
                    map.put("sspOrderName", sspOrderName);
                }

                Long sspPlanId = (Long) source.get("sspPlanId");
                if (sspPlanId != null) {
                    String sspPlanName = findSspPlanName(sspPlanId);
                    map.put("sspPlanName", sspPlanName);
                }

                Long sspCreativeId = (Long) source.get("sspCreativeId");
                if (sspCreativeId != null) {
                    String sspCreativeName = findSspCreativeName(sspCreativeId);
                    map.put("sspCreativeName", sspCreativeName);
                }
                source.putAll(map);

                list.add(source);
                //System.out.println(hit.getSourceAsString());
            }
        }

        return new Pagination>(totalCount, PAGESIZE, currentPageNo, list.toArray(new Map[]{}));	//     

    }


    private String findSspAdvertiserName(Long advertiserId) {
        SspAdvertiser sspAdvertiser = new SspAdvertiser();
        sspAdvertiser.setId(advertiserId);
        RestResponse restResponse = sspAdvertiserService.find(sspAdvertiser);
        ArrayList result = (ArrayList) restResponse.getResult();
        LinkedHashMap map = (LinkedHashMap) result.get(0);
        String name = (String) map.get("name");
        return name;
    }

    private String findSspOrderName(Long orderId) {
        String name = null;
        try {
            RestResponse restResponse = sspOrderService.find(orderId);
            ArrayList result = (ArrayList) restResponse.getResult();
            LinkedHashMap map = (LinkedHashMap) result.get(0);
            name = (String) map.get("name");
        } catch (Exception e) {
            LOG.error("      ");
        }
        return name;
    }

    private String findSspPlanName(Long planId) {
        String name = null;
        try {
            RestResponse restResponse = sspPlanService.find(planId);
            LinkedHashMap map = (LinkedHashMap) restResponse.getResult();
            name = (String) map.get("name");
        } catch (Exception e) {
            LOG.error("      ");
        }
        return name;
    }

    private String findSspCreativeName(Long creativeId) {
        SspCreative creative = new SspCreative();
        creative.setId(creativeId);
        RestResponse restResponse = sspCreativeService.find(creative);
        ArrayList result = (ArrayList) restResponse.getResult();
        LinkedHashMap map = (LinkedHashMap) result.get(0);
        String name = (String) map.get("creativeName");
        return name;
    }


/*    public List page(int pageNo, int pageSize, SearchHit[] hits) {
        List result = new ArrayList<>();
        if (hits != null && hits.length > 0) {
            int allCount = hits.length;
            int pageCount = (allCount + pageSize - 1) / pageSize;
            if (pageNo >= pageCount) {
                pageNo = pageCount;
            }
            if (pageNo < 1) {
                pageNo = 1;
            }
            int start = (pageNo - 1) * pageSize;
            int end = pageNo * pageSize;
            if (end >= allCount) {
                end = allCount;
            }
            for (int i = start; i < end; i++) {
                result.add(hits[i]);
            }
        }
        return (result != null && result.size() > 0) ? result : null;
    }*/

}

添付:
クエリーのタイプ
1、Query and fetchはインデックスのすべてのスライスにクエリー要求を発行し、各スライスが戻るときに要素ドキュメントを(document)計算されたランキング情報とともに返されます.この検索方式が一番速いです.次のいくつかの検索方式に比べて、この検索方法はshardに行って1回だけクエリーする必要があります.しかし、各shardが返す結果の数の和は、ユーザーが要求するsizeのn倍かもしれません.2、query then fetch(デフォルトの検索方法)検索時に検索方法が指定されていない場合は、この検索方法を使用します.この検索方法は、2つのステップに分かれています.最初のステップは、すべてのshardに要求し、各スライスはソートとランキングに関する情報のみを返します(ドキュメントdocumentは含まれません).次に、各スライスで返されたスコアに従って並べ替え、ランキングし、前のsizeドキュメントを取得します.次に2ステップ目を行い、関連するshardからdocumentを取ります.このようにして返されるdocumentは,ユーザが要求するsizeと等しい.3、DFS query and fetchこの方式は第1の方式より1つの初期化配布(initial scatter)ステップが多く、このステップがあれば、検索採点と順位をより正確に制御できるという.4、DFS query then fetchは第2の方式より1つの初期化配布(initial scatter)ステップが多い.