開篇
參考博客《數據導入》將數據庫中的數據根據需求導入到solr後,就可以使用這些被導入的數據源進行搜索了。
正文
實例實現順序從前臺開始。頁面上有一個搜索框和搜索按鈕,輸入內容單擊搜索按鈕請求後臺方法開始通過solr實現搜索,按鈕調用的js方法。.html爲定義的攔截的請求。
function search(a) {
var b = "http://localhost:8082/search.html?q="+
+ encodeURIComponent(document.getElementById(a).value);
return window.location.href = b;
}
以上的請求將會調用Controller中的search方法。
Controller中的search方法實現如下:
public class SearchController {
@Autowired
private SearchService searchService;
@RequestMapping("/search")
public String search(@RequestParam("q")String keyword,
@RequestParam(defaultValue="1")Integer page,
@RequestParam(defaultValue="60")Integer rows, Model model) {
//get亂碼處理
try {
keyword = new String(keyword.getBytes("iso8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
keyword = "";
e.printStackTrace();
}
SearchResult searchResult = searchService.search(keyword, page, rows);
//參數傳遞 給頁面
model.addAttribute("query", keyword);
model.addAttribute("totalPages", searchResult.getPageCount());
model.addAttribute("itemList", searchResult.getItemList());
model.addAttribute("page", searchResult.getCurPage());
//返回邏輯視圖
return "search";
}
}
調用service返回數據成功後將跳轉到search視圖。
Service層的實現
@Service
public class SearchServiceImpl implements SearchService {
@Autowired
private SearchDao searchDao;
@Override
public SearchResult search(String keyword, int page, int rows) {
//創建查詢條件
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 (recordCount % rows > 0) {
pageCount++;
}
searchResult.setPageCount(pageCount);
searchResult.setCurPage(page);
return searchResult; }
}
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<>();
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 result = new SearchResult();
result.setItemList(itemList);
//查詢結果總數量
result.setRecordCount(solrDocumentList.getNumFound());
return result;
}
}
小結
WithOutSolrj:
這種方式是採用http直接訪問的方法,所有的搜索條件會拼接成一個URL串,like that:q=title:手機&fl=id,titl,price&start=0&rows=10&sort=price &…&...&…,會採用httpclient向solr發起請求,返回的結果爲xml文件,我們需要對結果進行解析。
WithSolrj
面向對象的思想使用setter設置查詢條件,最終由solrj拼接url然後向solr服務發起請求。solrj返回的結果是兩種方式都是對象類型的,不需要我們解析。一種爲solrj自帶的SolrDocumentList,另外一種可以我們自定義一個pojo類,使用solrj提供的@Field註解。