一、存储内容到索引库
1、mapper
2、service
3、controller
4、可以看到如下结果
二、搜索服务的发布
1、dao
@Repository
public class SearchDaoImpl implements SearchDao {
@Autowired
private SolrServer solrServer;
@Override
public SearchResult search(SolrQuery query) throws Exception {
// 执行查询
QueryResponse response = solrServer.query(query);
//取查询结果
SolrDocumentList solrDocumentList = response.getResults();
List<SearchItem> itemList = new ArrayList<SearchItem>();
for(SolrDocument solrDocument : solrDocumentList) {
//创建一个SearchItem对象
SearchItem item = new SearchItem();
item.setCategory_name((String) solrDocument.get("item_category_name"));
item.setId((String) solrDocument.get("id"));
item.setImage((String) solrDocument.get("item_image"));
item.setPrice((Long) solrDocument.get("item_price"));
item.setSell_point((String) solrDocument.get("item_sell_point"));
//取高亮显示
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
String itemTitle = "";
if(list != null && list.size() > 0) {
//取得高亮后的结果
itemTitle = list.get(0);
}else {
itemTitle = (String) solrDocument.get("item_title");
}
item.setTitle(itemTitle);
//添加到列表
itemList.add(item);
}
//创建一个SearchResult对象
SearchResult result = new SearchResult();
result.setItemList(itemList);
//查询结果总数量
result.setRecordCount(solrDocumentList.getNumFound());
return result;
}
}
2、service
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private SearchDao searchDao;
@Override
public SearchResult search(String queryString, int page, int rows) throws Exception {
//创建查询条件
SolrQuery query = new SolrQuery();
//设置查询条件
query.setQuery(queryString);
//设置分页条件
query.setStart((page-1)*rows);
query.setRows(rows);
//设置默认搜索域
query.set("df", "item_title");
//设置高亮
query.setHighlight(true);
query.addHighlightField("item_title");
query.setHighlightSimplePre("<font class=\"skcolor_ljg\">");
query.setHighlightSimplePost("</font>");
//执行查询
SearchResult searchResult = searchDao.search(query);
//计算总页数
Long recordCount = searchResult.getRecordCount();
int pageCount = (int) (recordCount / rows);
if((pageCount % rows) > 0) {
pageCount++;
}
searchResult.setPageCount(pageCount);
searchResult.setCurPage(page);
return searchResult;
}
}
3、controller
@Controller
public class SearchController {
@Autowired
private SearchService searchService;
/**
* 根据关键字进行查询
*
* @param keyword
* @param page
* @param rows
* @return
*/
@RequestMapping("/q")
@ResponseBody
public TaotaoResult search(@RequestParam(defaultValue="")String keyword,
@RequestParam(defaultValue="1")Integer page,
@RequestParam(defaultValue="30")Integer rows) {
try {
//poat乱码在过滤器中解决了,此处会有get乱码
//转换字符集
keyword = new String(keyword.getBytes("iso8859-1"), "utf-8");
SearchResult searchResult = searchService.search(keyword, page, rows);
return TaotaoResult.ok(searchResult);
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
}
}
}
三、在potral中实现搜索
1、service
2、controller
3、结果