Java代码
- @Service
- @Qualifier("searchService")
- public class SearchService {
- @Value("${solr.url}")
- private String searchurl;
- @Value("${solr.q.fields}")
- private String qfields;
- @Value("${solr.s.fields}")
- private String sfields;
- @Value("${solr.h.fields}")
- private String hfields;
- /**
- * 分页查询Position
- * @param key 关键字
- * @param page 分页
- * @param pagesize 分页大小
- * @return 分页信息
- */
- public PageInfo<Position> query(String key, int page, int pagesize) {
- PageInfo<Position> ps = new PageInfo<Position>(page, pagesize);
- SolrServer server = new HttpSolrServer(searchurl);
- SolrQuery params = new SolrQuery();
- params.setQuery(setQueryFields(key, qfields));
- params.setStart(ps.getStart());
- params.setRows(ps.getPagesize());
- boolean ish = setHighlight(params,hfields);
- setSortFields(params, sfields);
- SolrDocumentList list = null;
- //第一个Map的键是文档的ID,第二个Map的键是高亮显示的字段名
- Map<String,Map<String,List<String>>> high= null;
- try {
- QueryResponse response = server.query(params);
- list = response.getResults();
- if(ish){
- high = response.getHighlighting();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- if(ish){
- }
- ps.setItems(mappingVo(list,high,ish));
- ps.setTotal((int) list.getNumFound());
- return ps;
- }
- /**
- * 设置高亮字段
- * @param params SolrQuery对象
- * @param hfields2 高亮字段,以逗号分隔
- * @return 是否需要高亮
- */
- private boolean setHighlight(SolrQuery params, String hfields2) {
- if(StringUtils.hasText(hfields2)){
- String[] arr = hfields2.split(",");
- for (String s : arr) {
- params.addHighlightField(s);
- }
- params.setHighlight(true);
- params.setHighlightSimplePre("<font color='red'><em>");
- params.setHighlightSimplePost("</em></font>");
- return true;
- }
- return false;
- }
- /**
- * 将查询结果返回映射为Position对象列表
- * @param list 返回的document列表
- * @param high 高亮信息
- * @param ish 是否需要高亮
- * @return Position对象列表
- */
- private List<Position> mappingVo(SolrDocumentList list,Map<String,Map<String,List<String>>> high,boolean ish) {
- List<Position> poses = new ArrayList<Position>();
- for (SolrDocument doc : list) {
- Position p = new Position();
- p.setPid(doc.getFieldValue("CP_ID").toString());
- if(ish && high.get(p.getPid()).containsKey("COMPANY_NAME")){
- p.setCompany(high.get(p.getPid()).get(("COMPANY_NAME")).toString());
- }else{
- p.setCompany(doc.getFieldValue("COMPANY_NAME").toString());
- }
- p.setStamp((Date)doc.getFieldValue("ADD_AT"));
- poses.add(p);
- }
- return poses;
- }
- /**
- * 设置排序字段
- * @param params SolrQuery对象
- * @param sfields2 排序字段,都是按降序排
- */
- private void setSortFields(SolrQuery params, String sfields2) {
- String[] arr = sfields2.split(",");
- for (String s : arr) {
- params.addSortField(s, ORDER.desc);
- }
- }
- /**
- * 定义查询字段
- * @param key 关键字
- * @param qfields2 以,分隔的字段列表,以或条件
- * @return
- */
- private String setQueryFields(String key, String qfields2) {
- StringBuilder sb = new StringBuilder();
- String[] arr = qfields2.split(",");
- for (String s : arr) {
- sb.append(s).append(":").append(key).append(" OR ");
- }
- String q = sb.toString();
- return q.substring(0, q.length() - 4);
- }
- }
@Service@Qualifier("searchService")public class SearchService { @Value("${solr.url}") private String searchurl; @Value("${solr.q.fields}") private String qfields; @Value("${solr.s.fields}") private String sfields; @Value("${solr.h.fields}") private String hfields; /** * 分页查询Position * @param key 关键字 * @param page 分页 * @param pagesize 分页大小 * @return 分页信息 */ public PageInfoquery(String key, int page, int pagesize) { PageInfo ps = new PageInfo (page, pagesize); SolrServer server = new HttpSolrServer(searchurl); SolrQuery params = new SolrQuery(); params.setQuery(setQueryFields(key, qfields)); params.setStart(ps.getStart()); params.setRows(ps.getPagesize()); boolean ish = setHighlight(params,hfields); setSortFields(params, sfields); SolrDocumentList list = null; //第一个Map的键是文档的ID,第二个Map的键是高亮显示的字段名 Map >> high= null; try { QueryResponse response = server.query(params); list = response.getResults(); if(ish){ high = response.getHighlighting(); } } catch (Exception e) { e.printStackTrace(); } if(ish){ } ps.setItems(mappingVo(list,high,ish)); ps.setTotal((int) list.getNumFound()); return ps; } /** * 设置高亮字段 * @param params SolrQuery对象 * @param hfields2 高亮字段,以逗号分隔 * @return 是否需要高亮 */ private boolean setHighlight(SolrQuery params, String hfields2) { if(StringUtils.hasText(hfields2)){ String[] arr = hfields2.split(","); for (String s : arr) { params.addHighlightField(s); } params.setHighlight(true); params.setHighlightSimplePre(" "); params.setHighlightSimplePost(""); return true; } return false; } /** * 将查询结果返回映射为Position对象列表 * @param list 返回的document列表 * @param high 高亮信息 * @param ish 是否需要高亮 * @return Position对象列表 */ private List mappingVo(SolrDocumentList list,Map >> high,boolean ish) { List poses = new ArrayList (); for (SolrDocument doc : list) { Position p = new Position(); p.setPid(doc.getFieldValue("CP_ID").toString()); if(ish && high.get(p.getPid()).containsKey("COMPANY_NAME")){ p.setCompany(high.get(p.getPid()).get(("COMPANY_NAME")).toString()); }else{ p.setCompany(doc.getFieldValue("COMPANY_NAME").toString()); } p.setStamp((Date)doc.getFieldValue("ADD_AT")); poses.add(p); } return poses; } /** * 设置排序字段 * @param params SolrQuery对象 * @param sfields2 排序字段,都是按降序排 */ private void setSortFields(SolrQuery params, String sfields2) { String[] arr = sfields2.split(","); for (String s : arr) { params.addSortField(s, ORDER.desc); } } /** * 定义查询字段 * @param key 关键字 * @param qfields2 以,分隔的字段列表,以或条件 * @return */ private String setQueryFields(String key, String qfields2) { StringBuilder sb = new StringBuilder(); String[] arr = qfields2.split(","); for (String s : arr) { sb.append(s).append(":").append(key).append(" OR "); } String q = sb.toString(); return q.substring(0, q.length() - 4); }}