发布搜索服务供其它工程调用。
Dao
要定义返回的POJO:1
2
3
4
5
6
7
8public class SearchResult {
private List<Item> itemList;
private long totalItemCount;
private long totalPageCount;
private long currentPageNumber;
private long currentRowCount;
}
并实现Dao:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51@Repository
public class SearchDaoImpl implements SearchDao {
@Autowired
private SolrServer solrServer;
@Override
public SearchResult search(SolrQuery query) throws Exception {
// return result
SearchResult resultList = new SearchResult();
// solr search
QueryResponse response = solrServer.query(query);
SolrDocumentList solrDocumentList = response.getResults();
// highlight
Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
//get item list
List<Item> itemList = new ArrayList<>();
for (SolrDocument solrDocument : solrDocumentList) {
Item item = new Item();
item.setId((String) solrDocument.get("id"));
//get highlight
List<String> list = highlighting.get(solrDocument.get("id")).get("item_title");
String title = "";
if(list != null && list.size()>0){
title = list.get(0);
}else{
title = (String) solrDocument.get("item_title");
}
item.setTitle(title);
item.setImage((String) solrDocument.get("item_image"));
item.setPrice((long) solrDocument.get("item_price"));
item.setSell_point((String) solrDocument.get("item_sell_point"));
item.setCategory_name((String) solrDocument.get("item_category_name"));
itemList.add(item);
}
resultList.setItemList(itemList);
// get total item number
resultList.setTotalItemCount(solrDocumentList.getNumFound());
return resultList;
}
}
Service
1 | @Service |
Controller
1 | @Controller |