Java项目实战 - solr发布搜索服务

发布搜索服务供其它工程调用。

Dao

要定义返回的POJO:

1
2
3
4
5
6
7
8
public 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
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
@Service
public class ServiceSearchImpl implements SearchService {

@Autowired
private SearchDao searchDao;

@Override
public SearchResult search(String query, int page, int rows) throws Exception {
SolrQuery solrQuery = new SolrQuery();

solrQuery.setQuery(query);

solrQuery.setStart((page -1) * rows);
solrQuery.setRows(rows);

solrQuery.set("df", "item_keywords");

solrQuery.setHighlight(true);
solrQuery.addHighlightField("item_title");
solrQuery.setHighlightSimplePre("<em style=\"color:red\">");
solrQuery.setHighlightSimplePost("</em>");

SearchResult result = searchDao.search(solrQuery);

long totalItemCount = result.getTotalItemCount();
long totalPageCount = totalItemCount / rows;
if (totalItemCount % rows > 0){
totalPageCount++;
}

result.setTotalPageCount(totalPageCount);
result.setCurrentPageNumber(page);
result.setCurrentRowCount(rows);
return result;
}

}

Controller

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
@Controller
public class SearchServiceController {

@Autowired
private SearchService searchService;

@RequestMapping(value="/query", method=RequestMethod.GET)
@ResponseBody
public TaotaoResult search(@RequestParam("q")String query,
@RequestParam(defaultValue="1")Integer page,
@RequestParam(defaultValue="20")Integer rows){

//query is null
if(StringUtils.isBlank(query))
return TaotaoResult.build(400, "Should contain query string");

SearchResult searchResult = null;
try {
query = new String(query.getBytes("iso8859-1"), "utf-8");
searchResult = searchService.search(query, page, rows);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return TaotaoResult.ok(searchResult);
}

}

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!