java solr功能代码
哈哈
阅读:554
2021-03-31 21:34:17
评论:0
package com.wlsq.search.center.util;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
public class SolrUtil {
private static HttpSolrServer instance = null;
private static String address = "http://120.76.221.164:8888/solr";
static {
address = PropertiesUtil.getValue();
System.out.println("address:" + address);
}
public static HttpSolrServer getInstance() {
if (instance == null) {
synchronized (HttpSolrServer.class) {
if (instance == null)
instance = new HttpSolrServer(address);
instance.setConnectionTimeout(5000);
instance.setSoTimeout(5000);
instance.setMaxTotalConnections(100);
instance.setDefaultMaxConnectionsPerHost(100);
instance.setAllowCompression(false);
instance.setMaxRetries(1);
instance.setFollowRedirects(true);
}
}
return instance;
}
public static QueryResponse getQueryResponseByCondition(int start,
int rows, String queryParams, String[] sortField, boolean[] sortFlag)
throws Exception {
HttpSolrServer server = getInstance();
SolrQuery sQuery = new SolrQuery();
if ((queryParams != null) && (!queryParams.equals(""))) {
sQuery.setQuery(queryParams);
}
if (rows != 0) {
sQuery.setStart(Integer.valueOf(start));
sQuery.setRows(Integer.valueOf(rows));
}
// sQuery.setHighlight(true); // �����������
// sQuery.addHighlightField("item_sell_point");// �����ֶ�
// // sQuery.addHighlightField("title");// �����ֶ�
// sQuery.setHighlightSimplePre("<font color='red'>");//��ǣ������ؼ���ǰ
// sQuery.setHighlightSimplePost("</font>");//��
if ((sortField != null) && (sortField.length > 0) && (sortFlag != null)
&& (sortFlag.length > 0)) {
for (int i = 0; i < sortField.length; i++) {
if (sortFlag[i] != true)
sQuery.addSortField(sortField[i], SolrQuery.ORDER.desc);
else {
sQuery.addSortField(sortField[i], SolrQuery.ORDER.asc);
}
}
}
QueryResponse response = server.query(sQuery);
return response;
}
public static QueryResponse getQueryResponse(int start, int row,
String queryStr, String filterStr, String defaultStr, String descStr) {
QueryResponse response = null;
try {
HttpSolrServer server = getInstance();
SolrQuery query = new SolrQuery();
// 下面设置solr查询参数
if (StringUtil.isNotBlank(queryStr)) {
query.set("q", queryStr);// 参数q 查询所有特定条件
}
if (StringUtil.isNotBlank(filterStr)) {
query.addFilterQuery(filterStr);// 参数fq, 给query增加过滤查询条件
}
if (StringUtil.isNotBlank(defaultStr)) {
query.set("df", defaultStr); // 参数df,给query设置默认搜索域
}
if (StringUtil.isNotBlank(descStr)) {
query.setSort(descStr, SolrQuery.ORDER.desc);// 参数sort,设置返回结果的排序规则
}
// 设置分页参数
query.setStart(start);
query.setRows(row);// 每一页多少值
// 获取查询结果
response = server.query(query);
} catch (Exception e) {
return response;
}
return response;
}
}
功能代码:
public Map<String, Object> queryUserInfo(int start, int row,
String queryStr, String filterStr, String defaultStr, String descStr) {
// TODO Auto-generated method stub
Map resultMap = new HashMap();
QueryResponse response = SolrUtil.getQueryResponse(start, row, queryStr, filterStr, defaultStr, descStr);
long total = response.getResults().getNumFound();
List users = response.getBeans(User.class);
resultMap.put("total", total);
resultMap.put("data", users);
return resultMap;
}
测试代码:
public static void queryTestProduct(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "consumer.xml" });
context.start();
UserCenterService userCenterService = (UserCenterService) context.getBean("userCenterService");
Map<String,Object> map= userCenterService.queryProductInfo(0, 10, "product_name:梨子", "", "product_keywords", "");
System.out.println("======================"+map);
}
public static void queryTestUser(){
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"consumer.xml"});
context.start();
UserCenterService userCenterService = (UserCenterService)context.getBean("userCenterService");
Map<String,Object> map = userCenterService.queryUserInfo(0, 10, "id:6", "", "user_keywords", "");
System.out.println("======================"+map);
}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。