|
@Autowired private SolrServer solrServer;//导入solrServer对象
//创建查询对象
SolrQuery solrQuery = new SolrQuery();
//设置查询条件
//solrQuery.set("q","");
solrQuery.setQuery(queryString);
//设置分页
solrQuery.setStart((int) ((page - 1) * pageSize));
solrQuery.setRows(pageSize.intValue());
//高亮设置
solrQuery.setHighlight(true);
//设置高亮显示的域
solrQuery.addHighlightField("item_title");
//高亮显示的前缀
solrQuery.setHighlightSimplePre("<span style='color:red'>");
//高亮显示的后缀
solrQuery.setHighlightSimplePost("</span>");
//设置默认搜区域
solrQuery.set("df", "item_keywords");
//根据查询条件查询索引库
QueryResponse response = solrServer.query(solrQuery);
//从response中取查询结果
SolrDocumentList list = response.getResults();
//查询结果list
List<SolrItem> itemList = new ArrayList<>();
//遍历查询结果
for (SolrDocument solrDocument : list) {
//创建一商品对象
SolrItem item = new SolrItem();
item.setId(Long.parseLong((String)solrDocument.get("id")));
//取高亮显示
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
List<String> hList = highlighting.get(solrDocument.get("id")).get("item_title");
String> //判断是否有高亮信息
if (hList != null && hList.size() > 0) {
title = hList.get(0);
} else {
title = (String) solrDocument.get("item_title");
}
item.setTitle(title);
item.setSell_point((String) solrDocument.get("item_sell_point"));
item.setPrice((long) solrDocument.get("item_price"));
item.setImage((String) solrDocument.get("item_image"));
//添加到商品列表
itemList.add(item);
} |
|
|