Solr4.6,用builder模式构建查询参数对象

    Solr搜索的条件有8个左右,界面式搜索条件如下:

Solr4.6,用builder模式构建查询参数对象

    8个参数,封装成一个参数对象,属性有点多。是提供构造器方式设置属性还是提供java bean的方式好呢?

    如果提供构造器方式,那么你不得不提供多个构造器以满足必选参数和多个可选参数的组合。

    如果是java bean模式,虽然构造一个请求参数bean十分容易,但是如果少了一个必选属性呢?比如少了关键字。

    这个时候最合适的是设计模式里面的构造器模式,既可以满足必选属性一个都不少,同时满足可选属性自助式设置。

    我为solr查询功能封装的查询参数对象如下,应用builder模式:

@SuppressWarnings("serial")
public class SearchRequest implements Serializable{
	//compulsory fields!必须传入的字段
	private String 				keyWord;		//查询关键字
	private SolrCoreNameEnum 	coreNameEnum;	//选择的核
	private List<String> 		searchFieldList;//需要查询的字段列表
	private List<String> 		returnFieldList;//返回的字段列表
	private Class<?>			returnClass;	//返回的对象类型
 	
	//optional fields,可选择字段
	private int 				pageIdx;		//查询的页数
	private int 				pageSize;		//页大小
	private List<SortClause>	sortClauseList;	//排序字段,可以多级排序
	private String[] 			filterQueryArr;	//查询过滤器
	private static final int 	DEFAULT_PAGE_SIZE = 20;
	
	
	/**
	 * builder模式使用!
	 * @author cdlvsheng
	 *
	 */
	public static class Builder {
		//compulsory fields!必须传入的字段
		private String 				keyWord;		//查询关键字
		private SolrCoreNameEnum 	coreNameEnum;	//选择的核
		private List<String> 		searchFieldList;//需要查询的字段列表
		private List<String> 		returnFieldList;//返回的字段列表
		private Class<?>			returnClass;	//返回的对象类型
	 	
		//optional fields,可选择字段
		private int 				pageIdx;		//查询的页数
		private int 				pageSize;		//页大小
		private List<SortClause>	sortClauseList;	//排序字段,可以多级排序
		private String[] 			filterQueryArr;	//查询过滤器
		
		public Builder(SolrCoreNameEnum coreNameEnum, String keyWord, List<String> searchFieldList, List<String> returnFieldList, Class<?> returnClass) {
			this.coreNameEnum 		= coreNameEnum;
			this.keyWord 			= keyWord;
			this.searchFieldList 	= searchFieldList;
			this.returnFieldList 	= returnFieldList;
			this.returnClass 		= returnClass;
			
			this.pageIdx 				= 0 ;
			this.pageSize 				= DEFAULT_PAGE_SIZE;
		}
		
		public Builder pageIdx(int val) {
			this.pageIdx = val;
			return this;
		}
		
		public Builder pageSize(int val) {
			this.pageSize = val;
			return this;
		}
		
		public Builder sortClauseList(List<SortClause> sortClauseList) {
			this.sortClauseList = sortClauseList;
			return this;
		}
		
		public Builder filterQueryArr(String[] filterQueryArr) {
			this.filterQueryArr = filterQueryArr;
			return this;
		}
		
		public SearchRequest build() {
			return new SearchRequest(this);
		}
	}
	
	private SearchRequest(Builder builder) {
		this.coreNameEnum 		= builder.coreNameEnum;
		this.keyWord 			= builder.keyWord;
		this.searchFieldList 	= builder.searchFieldList;
		this.returnFieldList 	= builder.returnFieldList;
		this.returnClass 		= builder.returnClass;
		
		this.pageIdx 			= builder.pageIdx ;
		this.pageSize 			= builder.pageSize;
		this.sortClauseList		= builder.sortClauseList;
		this.filterQueryArr		= builder.filterQueryArr;
	}
	
}
    客户端调用如下:

SearchRequest sr = new SearchRequest.Builder(SolrCoreNameEnum.WaiterCore, keyWord, list, returnFieldList, WaiterTest.class).pageSize(100).build();

    

Solr4.6,用builder模式构建查询参数对象

上一篇:安卓自定义标题栏


下一篇:ubuntu下查找含有某关键字内容的文件