/** 
 * @Title: MediaESDAOImpl.java 
 * @Package com.zhiweidata.bjfzxWebapp.esDao.impl 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月7日 下午6:59:45 
 * @version V1.0 
 */ /**
 * 
 */
package com.zhiweidata.bjfzxWebapp.esDao.impl;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder.Type;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;

import com.zhiweidata.bjfzxWebapp.conf.EsConfig;
import com.zhiweidata.bjfzxWebapp.entity.MediaAndWechatEvent;
import com.zhiweidata.bjfzxWebapp.esDao.ESClient;
import com.zhiweidata.bjfzxWebapp.esDao.EsFatherDAO;
import com.zhiweidata.bjfzxWebapp.esDao.MediaESDAO;
import com.zhiweidata.bjfzxWebapp.util.TimeUtil;

/** 
 * @ClassName: MediaESDAOImpl 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author xuyimeng 
 * @date 2018年2月7日 下午6:59:45  
 */
public class MediaESDAOImpl extends EsFatherDAO  implements MediaESDAO{
	
	
	@Override
	public List<SearchHit> findByTime(String start, String end,String... keys) throws IOException{
		
//		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
		Date startTime = TimeUtil.parseTime(start, "yyyy-MM-dd HH:mm:ss");
        Date endTime = TimeUtil.parseTime(start, "yyyy-MM-dd HH:mm:ss");
        
        List<String> indexs = ESClient.eSIndexes.getIndexes(
                EsConfig.es_meida_indexName, startTime, endTime);
        
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(new RangeQueryBuilder("time").gt(start).lte(end));
		
        SearchRequestBuilder searchRequestBuilder = transportClient
                .prepareSearch(indexs.toArray(new String[indexs.size()]));// 指定查询库
        //        SearchRequestBuilder searchRequestBuilder = transportClient
//                .prepareSearch(EsConfig.es_meida_indexName);// 指定查询库
        
        searchRequestBuilder.setTypes(EsConfig.es_media_type);
        
        
        for (String key : keys)
		{
			MultiMatchQueryBuilder mmqb = new MultiMatchQueryBuilder(key, "title","content");
			mmqb.type(Type.PHRASE);
			mmqb.slop(0);
			boolQueryBuilder.must(mmqb);
		}
        SortBuilder sortBuilder = SortBuilders.fieldSort("time")
                .order(SortOrder.DESC);
        searchRequestBuilder.setQuery(boolQueryBuilder);
//        searchRequestBuilder.setPostFilter(QueryBuilders.rangeQuery("time")
//                .from(start).to(end));
        
        searchRequestBuilder.addSort(sortBuilder);
		return findSearchHit(searchRequestBuilder);
	}

	@Override
	public List<MediaAndWechatEvent> findByTimeBean(String start, String end,String groupName,String keyStr, String... keys) throws IOException {
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
		Date startTime = TimeUtil.parseTime(start, "yyyy-MM-dd HH:mm:ss");
        Date endTime = TimeUtil.parseTime(start, "yyyy-MM-dd HH:mm:ss");
        
        List<String> indexs = ESClient.eSIndexes.getIndexes(
                EsConfig.es_meida_indexName, startTime, endTime);
        
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(new RangeQueryBuilder("time").gt(start).lte(end));
		
        SearchRequestBuilder searchRequestBuilder = transportClient
                .prepareSearch(indexs.toArray(new String[indexs.size()]));// 指定查询库
        //        SearchRequestBuilder searchRequestBuilder = transportClient
//                .prepareSearch(EsConfig.es_meida_indexName);// 指定查询库
        
        searchRequestBuilder.setTypes(EsConfig.es_media_type);
        
        
        for (String key : keys)
		{
			MultiMatchQueryBuilder mmqb = new MultiMatchQueryBuilder(key, "title","content");
			mmqb.type(Type.PHRASE);
			mmqb.slop(0);
			boolQueryBuilder.must(mmqb);
		}
        SortBuilder sortBuilder = SortBuilders.fieldSort("time")
                .order(SortOrder.DESC);
		searchSourceBuilder.query(boolQueryBuilder)
							.sort(sortBuilder);
		return findSearchMedia(searchRequestBuilder, groupName, keyStr);
	}
}
