/** 
 * @Title: WeiboESDAOImpl.java 
 * @Package com.zhiweidata.bjfzxWebapp.esDao.impl 
 * @Description: TODO(用一句话描述该文件做什么) 
 * @author xuyimeng 
 * @date 2018年2月5日 下午2:34:07 
 * @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.WeiboBusiness;
import com.zhiweidata.bjfzxWebapp.esDao.ESClient;
import com.zhiweidata.bjfzxWebapp.esDao.EsFatherDAO;
import com.zhiweidata.bjfzxWebapp.esDao.WeiboESDAO;
import com.zhiweidata.bjfzxWebapp.util.ESQueryUtil;
import com.zhiweidata.bjfzxWebapp.util.TimeUtil;

/** 
 * @ClassName: WeiboESDAOImpl 
 * @Description: TODO(这里用一句话描述这个类的作用) 
 * @author xuyimeng 
 * @date 2018年2月5日 下午2:34:07  
 */
public class WeiboESDAOImpl extends EsFatherDAO implements WeiboESDAO{
	
	@Override
	public List<SearchHit> findByTime(String start, String end,String... keys) throws IOException{
		Date startTime = TimeUtil.parseTime(start, "yyyy-MM-dd HH:mm");
        Date endTime = TimeUtil.parseTime(end, "yyyy-MM-dd HH:mm");
        List<String> indexs = ESClient.eSIndexes.getIndexes(
                EsConfig.es_weibo_indexName, startTime, endTime);
		
		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
		
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(new RangeQueryBuilder("time").gt(start).lte(end));
		
		for (String key : keys)
		{
			MultiMatchQueryBuilder mmqb = new MultiMatchQueryBuilder(key, "description","text");
			mmqb.type(Type.PHRASE);
			mmqb.slop(0);
			boolQueryBuilder.must(mmqb);
		}
		SortBuilder sortBuilder = SortBuilders.fieldSort("time")
                .order(SortOrder.DESC);
		searchSourceBuilder.query(boolQueryBuilder)
							.sort(sortBuilder)
							.size(500);
		 SearchRequestBuilder searchRequestBuilder = transportClient
	                .prepareSearch(indexs.toArray(new String[indexs.size()]));// 指定查询库
		 searchRequestBuilder.setTypes(EsConfig.es_weibo_type);
		
		return findSearchHit(searchRequestBuilder);
	}

	@Override
	public List<WeiboBusiness> findByTimeBean(String start, String end, String groupName, String keyStr, String... keys)
			throws IOException {
		Date startTime = TimeUtil.parseTime(start, "yyyy-MM-dd HH:mm");
        Date endTime = TimeUtil.parseTime(end, "yyyy-MM-dd HH:mm");
        List<String> indexs = ESClient.eSIndexes.getIndexes(
                EsConfig.es_weibo_indexName, startTime, endTime);
		
//		SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
		
		BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        boolQueryBuilder.must(new RangeQueryBuilder("time").gt(start).lte(end));
      boolQueryBuilder.should(ESQueryUtil.matchPhraseQueryWords(
              QueryBuilders.boolQuery(), "must", "text", keys));
      boolQueryBuilder.should(ESQueryUtil.matchPhraseQueryWords(
              QueryBuilders.boolQuery(), "must", "roottext", keys));
//		for (String key : keys)
//		{
//			MultiMatchQueryBuilder mmqb = new MultiMatchQueryBuilder(key, "roottext","text");
//			mmqb.type(Type.PHRASE);
//			mmqb.slop(0);
//			boolQueryBuilder.must(mmqb);
//		}
		SortBuilder sortBuilder = SortBuilders.fieldSort("time")
                .order(SortOrder.DESC);
//		searchSourceBuilder.query(boolQueryBuilder)
//							.sort(sortBuilder)
//							.size(500);
		
		 SearchRequestBuilder searchRequestBuilder = transportClient
	                .prepareSearch(indexs.toArray(new String[indexs.size()]));// 指定查询库
		 searchRequestBuilder.setTypes(EsConfig.es_weibo_type);
		 searchRequestBuilder.setQuery(boolQueryBuilder);
		 searchRequestBuilder.addSort(sortBuilder);
		 searchRequestBuilder.setPostFilter(QueryBuilders.rangeQuery("time")
	                .from(startTime).to(endTime));
		 
		 System.out.println(searchRequestBuilder);
		 
		return findSearchWeibo(searchRequestBuilder, groupName, keyStr);
	}
}
