Java Solr HTTPClient一般请求流程代码编写分析

访客 阅读:247 2021-03-31 13:46:59 评论:0
import com.alibaba.fastjson.JSONObject; 
import com.digipower.common.page.PageData; 
 
/** 
 * 查询的策略模式 
 * @author zzg 
 * 
 */ 
public interface SearchStrategy<T> { 
 
	PageData<T> search(JSONObject json); 
} 
/** 
 * 文件级  solr 查询 
 * @author zzg 
 * 
 */ 
public class SolrStrategy extends AbstractController implements SearchStrategy<Map> { 
	 
	public static final Logger logger = LoggerFactory.getLogger(SolrStrategy.class); 
	 
	private static final String solrCoreName ="fileInfo"; 
	// slor 查询字段 
	private String[] fields; 
	// slor 查询结果是否高亮(默认false) 
	private boolean highlight = false; 
	 
	public String[] getFields() { 
		return fields; 
	} 
 
	public void setFields(String[] fields) { 
		this.fields = fields; 
	} 
 
	public boolean isHighlight() { 
		return highlight; 
	} 
 
	public void setHighlight(boolean highlight) { 
		this.highlight = highlight; 
	} 
	 
	public SolrStrategy(String[] fields, boolean highlight) { 
		super(); 
		this.fields = fields; 
		this.highlight = highlight; 
	} 
 
	@Override 
	public PageData<Map> search(JSONObject json) { 
		// 分页请求参数 
		Map<String, Object> parame = JSONObject.toJavaObject(json, Map.class); 
		PageParam rb = super.initPageBounds(parame); 
		int start = (rb.getPageNo()-1); 
		int rows = rb.getLimit(); 
		// 过滤字段 
		String[] fq = this.getFq(json); 
		 
		HttpSolrClient solrClient = this.getCommonsHttpSolrServer(solrCoreName); 
		SolrQuery query = this.getCommonSolrQuery(fq, start, rows); 
		 
		return this.getCommonPageData(solrClient, query); 
		return null; 
	} 
	 
	public String[] getFq(JSONObject json){ 
		// 移除分页请求参数 
		json.remove(WebAppConstants.PAGE); 
		json.remove(WebAppConstants.LIMIT); 
		json.remove("type"); 
		 
		List<String> list = new ArrayList<String>(); 
		for(Object map: json.entrySet()){ 
			String key = String.valueOf(((Map.Entry)map).getKey()); 
			String value = String.valueOf(((Map.Entry)map).getValue()); 
			if(!StringUtils.isEmpty(value)){ 
				StringBuilder builder = new StringBuilder(); 
				builder.append("text").append(":").append(value); 
				list.add(builder.toString()); 
			} 
			 
		} 
		 
		  //初始化需要得到的数组 
	    String[] array = new String[list.size()]; 
 
	    //使用for循环得到数组 
	    for(int i = 0; i < list.size();i++){ 
	        array[i] = list.get(i); 
	    } 
 
		return array; 
	} 
	 
	/** 
	 * 构建solr 客户端 
	 * @param solrCoreName 
	 * @return 
	 */ 
	protected HttpSolrClient getCommonsHttpSolrServer(String solrCoreName){ 
		String solruri = ApplicationPropertiesHolder.getProperty("request.solr.uri", ""); 
		solruri = solruri + solrCoreName + "/"; 
		CloseableHttpClient httpClient = SpringContextUtil.getBean(CloseableHttpClient.class); 
		HttpSolrClient server = new HttpSolrClient(solruri,httpClient); 
		return server; 
	} 
	 
	/** 
	 * 构建solr 查询条件 
	 * @param paramter 
	 * @param start 
	 * @param rows 
	 * @return 
	 */ 
	protected SolrQuery getCommonSolrQuery(String[] fq,Integer start, Integer rows){ 
		SolrQuery query = new SolrQuery(); 
		 
		// 1、常用查询 q 
		query.setQuery("*:*"); 
		// 2、过滤查询fq 
		query.setFilterQueries(fq); 
		// 3、设置查询字段 
		if(this.fields != null && this.fields.length > 0){ 
			query.setFields(this.fields);			 
		} 
		// 4、设置查询分页 
		query.setStart(start); 
		query.setRows(rows); 
		// 5、设置是否高亮 
		query.setHighlight(this.highlight); 
	 
		return query; 
	} 
	 
	protected PageData<Map> getCommonPageData(HttpSolrClient client, SolrQuery query){ 
		PageData<Map> result =new PageData<Map>(); 
		List<Map> list = new ArrayList<Map>(); 
		try { 
			SolrDocumentList solrDocuments = client.query(query).getResults(); 
			for(SolrDocument solrDocument : solrDocuments){ 
				Map<String,Object> map = new HashMap<String,Object>(); 
				for(int i= 0; i < this.fields.length; i++){ 
					String key = this.fields[i]; 
					Object object = solrDocument.get(key); 
					if(ObjectUtils.isEmpty(object)){ 
						map.put(key, StringUtils.EMPTY); 
					} else { 
						map.put(key, object); 
					} 
				} 
				list.add(map); 
			} 
			long total = solrDocuments.getNumFound(); 
			 
			result.setData(list); 
			result.setPageNum(query.getStart()); 
			result.setPageSize(query.getRows()); 
			result.setTotalCount(total); 
			 
		} catch (Exception e) { 
			// TODO Auto-generated catch block 
			logger.error(e.getMessage()); 
			return null; 
		}  
		return result; 
	} 
	 
	 
	 
	 
	 
	 
 
}

 

声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号