Java代码

  1.  
  2. @Service
  3. @Qualifier("searchService")
  4. public class SearchService {
  5. @Value("${solr.url}")
  6. private String searchurl;
  7. @Value("${solr.q.fields}")
  8. private String qfields;
  9. @Value("${solr.s.fields}")
  10. private String sfields;
  11. @Value("${solr.h.fields}")
  12. private String hfields;
  13. /**
  14. * 分页查询Position
  15. * @param key 关键字
  16. * @param page 分页
  17. * @param pagesize 分页大小
  18. * @return 分页信息
  19. */
  20. public PageInfo<Position> query(String key, int page, int pagesize) {
  21. PageInfo<Position> ps = new PageInfo<Position>(page, pagesize);
  22. SolrServer server = new HttpSolrServer(searchurl);
  23. SolrQuery params = new SolrQuery();
  24. params.setQuery(setQueryFields(key, qfields));
  25. params.setStart(ps.getStart());
  26. params.setRows(ps.getPagesize());
  27. boolean ish = setHighlight(params,hfields);
  28. setSortFields(params, sfields);
  29. SolrDocumentList list = null;
  30. //第一个Map的键是文档的ID,第二个Map的键是高亮显示的字段名
  31. Map<String,Map<String,List<String>>> high= null;
  32. try {
  33. QueryResponse response = server.query(params);
  34. list = response.getResults();
  35. if(ish){
  36. high = response.getHighlighting();
  37. }
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. if(ish){
  42.  
  43. }
  44. ps.setItems(mappingVo(list,high,ish));
  45. ps.setTotal((int) list.getNumFound());
  46. return ps;
  47. }
  48. /**
  49. * 设置高亮字段
  50. * @param params SolrQuery对象
  51. * @param hfields2 高亮字段,以逗号分隔
  52. * @return 是否需要高亮
  53. */
  54. private boolean setHighlight(SolrQuery params, String hfields2) {
  55. if(StringUtils.hasText(hfields2)){
  56. String[] arr = hfields2.split(",");
  57. for (String s : arr) {
  58. params.addHighlightField(s);
  59. }
  60. params.setHighlight(true);
  61. params.setHighlightSimplePre("<font color='red'><em>");
  62. params.setHighlightSimplePost("</em></font>");
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * 将查询结果返回映射为Position对象列表
  69. * @param list 返回的document列表
  70. * @param high 高亮信息
  71. * @param ish 是否需要高亮
  72. * @return Position对象列表
  73. */
  74. private List<Position> mappingVo(SolrDocumentList list,Map<String,Map<String,List<String>>> high,boolean ish) {
  75. List<Position> poses = new ArrayList<Position>();
  76. for (SolrDocument doc : list) {
  77. Position p = new Position();
  78. p.setPid(doc.getFieldValue("CP_ID").toString());
  79. if(ish && high.get(p.getPid()).containsKey("COMPANY_NAME")){
  80. p.setCompany(high.get(p.getPid()).get(("COMPANY_NAME")).toString());
  81. }else{
  82. p.setCompany(doc.getFieldValue("COMPANY_NAME").toString());
  83. }
  84. p.setStamp((Date)doc.getFieldValue("ADD_AT"));
  85. poses.add(p);
  86. }
  87. return poses;
  88. }
  89. /**
  90. * 设置排序字段
  91. * @param params SolrQuery对象
  92. * @param sfields2 排序字段,都是按降序排
  93. */
  94. private void setSortFields(SolrQuery params, String sfields2) {
  95. String[] arr = sfields2.split(",");
  96. for (String s : arr) {
  97. params.addSortField(s, ORDER.desc);
  98. }
  99.  
  100. }
  101. /**
  102. * 定义查询字段
  103. * @param key 关键字
  104. * @param qfields2 以,分隔的字段列表,以或条件
  105. * @return
  106. */
  107. private String setQueryFields(String key, String qfields2) {
  108. StringBuilder sb = new StringBuilder();
  109. String[] arr = qfields2.split(",");
  110. for (String s : arr) {
  111. sb.append(s).append(":").append(key).append(" OR ");
  112. }
  113. String q = sb.toString();
  114. return q.substring(0, q.length() - 4);
  115. }
  116. }
@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
query(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); }}