package com.zhiweidata.bjfzxWebapp.util;

import java.util.List;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;

/**
 * 
 * @TODO( es 的查询工具类)
 * @name ESQueryUtil
 * @author 陈炜涛
 * @time 2017年7月13日下午3:08:28
 */
public class ESQueryUtil {
	public static final String ES_QUERY_AND = "must";

	public static final String ES_QUERY_OR = "should";

	public static final String ES_QUERY_NOT = "not";

	/**
	 * 
	 * @TODO( 获取高亮设置)
	 * @author 陈炜涛
	 * @param fields
	 * @return
	 * @time 2017年7月13日下午3:44:39
	 * @return HighlightBuilder
	 */
	public synchronized static HighlightBuilder getHighlightBuilder(String... fields) {
		HighlightBuilder hiBuilder = new HighlightBuilder();

		hiBuilder.preTags("<font style='font-weight:bold;font-size:15px' color='#ff330D'>");
		hiBuilder.postTags("</font>");
		for (String field : fields) {
			hiBuilder.field(field, 300);
		}

		return hiBuilder;
	}

	/**
	 * 
	 * @TODO( 字段不拆封,多字段 同关键词)
	 * @author 陈炜涛
	 * @param boolQueryBuilder
	 * @param esQuery
	 *            是must 还是should
	 * @param word
	 * @param fields
	 * @return
	 * @time 2017年7月13日下午3:38:14
	 * @return BoolQueryBuilder
	 */
	public synchronized static BoolQueryBuilder matchPhraseQueryFields(BoolQueryBuilder boolQueryBuilder, String esQuery,
			String word, String... fields) {
		if (ES_QUERY_AND.equals(esQuery)) {
			for (String field : fields) {
				boolQueryBuilder.must(QueryBuilders.matchPhraseQuery(field, word));
			}
		} else if (ES_QUERY_OR.equals(esQuery)) {
			for (String field : fields) {
				boolQueryBuilder.should(QueryBuilders.matchPhraseQuery(field, word));
			}
		} else if (ES_QUERY_NOT.equals(esQuery)) {
			for (String field : fields) {
				boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery(field, word));
			}
		}
		return boolQueryBuilder;
	}

	/**
	 * 
	 * @TODO( 字段不拆封,多字段 同关键词)
	 * @author 陈炜涛
	 * @param boolQueryBuilder
	 * @param esQuery
	 *            是must 还是should
	 * @param word
	 * @param fields
	 * @return
	 * @time 2017年7月13日下午3:38:14
	 * @return BoolQueryBuilder
	 */
	public synchronized static BoolQueryBuilder matchPhraseQueryFields(BoolQueryBuilder boolQueryBuilder, String esQuery,
			String word, List<String> fields) {
		if (ES_QUERY_AND.equals(esQuery)) {
			for (String field : fields) {
				boolQueryBuilder.must(QueryBuilders.matchPhraseQuery(field, word));
			}
		} else if (ES_QUERY_OR.equals(esQuery)) {
			for (String field : fields) {
				boolQueryBuilder.should(QueryBuilders.matchPhraseQuery(field, word));
			}
		} else if (ES_QUERY_NOT.equals(esQuery)) {
			for (String field : fields) {
				boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery(field, word));
			}
		}
		return boolQueryBuilder;
	}

	/**
	 * 
	 * @TODO( 字段不拆封,单字段 多关键词)
	 * @author 陈炜涛
	 * @param boolQueryBuilder
	 * @param field
	 * @param words
	 * @return
	 * @time 2017年7月13日下午3:39:43
	 * @return BoolQueryBuilder
	 */
	public synchronized static BoolQueryBuilder matchPhraseQueryWords(BoolQueryBuilder boolQueryBuilder, String esQuery,
			String field, List<String> words) {
		if (ES_QUERY_AND.equals(esQuery)) {
			for (String word : words) {
				boolQueryBuilder.must(QueryBuilders.matchPhraseQuery(field, word));
			}
		} else if (ES_QUERY_OR.equals(esQuery)) {
			for (String word : words) {
				boolQueryBuilder.should(QueryBuilders.matchPhraseQuery(field, word));
			}
		} else if (ES_QUERY_NOT.equals(esQuery)) {
			for (String word : words) {
				boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery(field, word));
			}
		}
		return boolQueryBuilder;
	}
	
	
	/**
	 * 
	 * @TODO( 字段不拆封,单字段 多关键词)
	 * @author 陈炜涛
	 * @param boolQueryBuilder
	 * @param field
	 * @param words
	 * @return
	 * @time 2017年7月13日下午3:39:43
	 * @return BoolQueryBuilder
	 */
	public synchronized static BoolQueryBuilder matchPhraseQueryWords(BoolQueryBuilder boolQueryBuilder, String esQuery,
			String field, String... words) {
		if (ES_QUERY_AND.equals(esQuery)) {
			for (String word : words) {
				boolQueryBuilder.must(QueryBuilders.matchPhraseQuery(field, word));
			}
		} else if (ES_QUERY_OR.equals(esQuery)) {
			for (String word : words) {
				boolQueryBuilder.should(QueryBuilders.matchPhraseQuery(field, word));
			}
		} else if (ES_QUERY_NOT.equals(esQuery)) {
			for (String word : words) {
				boolQueryBuilder.mustNot(QueryBuilders.matchPhraseQuery(field, word));
			}
		}
		return boolQueryBuilder;
	}
	
	
}
