请求的url:
file:///C:/Users/ADMINI~1.WIN/AppData/Local/Temp/V7(XMWRN]%7BG8~CI%7DBCCR3QC.gifarch/query?q={查询条件}&page={page}&rows={rows}
返回的结果:TaotaoResult包装商品列表。
创建一个sql语句对应的pojo,单独建立一个pojo
用来装显示的内容列表:
[AppleScript] 纯文本查看 复制代码
public class Item {
private String id;
private String title;
private String sell_point;
private long price;
private String image;
private String category_name;
private String item_des;
}
controller层:
[AppleScript] 纯文本查看 复制代码
@Controller
public class SearchController {
@Autowired
private SearchService searchService;
@RequestMapping(value="/query", method=RequestMethod.GET)
@ResponseBody
public TaotaoResult search(@RequestParam("q")String queryString,
@RequestParam(defaultValue="1")Integer page,
@RequestParam(defaultValue="60")Integer rows) {
//查询条件不能为空
if (StringUtils.isBlank(queryString)) {
return TaotaoResult.build(400, "查询条件不能为空");
}
SearchResult searchResult = null;
try {
queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");
searchResult = searchService.search(queryString, page, rows);
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
}
return TaotaoResult.ok(searchResult);
}
}
[AppleScript] 纯文本查看 复制代码
<span style="font-size: 16px">service层:利用solrJ的solrQurery来查询:</span>
前提是要写好如何从索引库读取数据:
下面是服务的接口层编写:
controller:
[AppleScript] 纯文本查看 复制代码
@Controller
public class SearchController {
@Autowired
private SearchService searchService;
@RequestMapping(value="/query", method=RequestMethod.GET)
@ResponseBody
public TaotaoResult search(@RequestParam("q")String queryString,
@RequestParam(defaultValue="1")Integer page,
@RequestParam(defaultValue="60")Integer rows) {
//查询条件不能为空
if (StringUtils.isBlank(queryString)) {
return TaotaoResult.build(400, "查询条件不能为空");
}
SearchResult searchResult = null;
try {
queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");
searchResult = searchService.search(queryString, page, rows);
} catch (Exception e) {
e.printStackTrace();
return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
}
return TaotaoResult.ok(searchResult);
}
}
[AppleScript] 纯文本查看 复制代码
@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_keywords");
//设置高亮显示
query.setHighlight(true);
query.addHighlightField("item_title");
query.setHighlightSimplePre("<em style=\"color:red\">");
query.setHighlightSimplePost("</em>");
//执行查询
SearchResult searchResult = searchDao.search(query);
//根据结果来计算商品总共多少页
long recordCount=searchResult.getRecordCount();
long pageCount=recordCount/rows;
if (recordCount % rows > 0) {
pageCount++;
}
searchResult.setPageCount(pageCount);
searchResult.setCurPage((long) page);
return searchResult;
}
}
客户端通过输入商品来实现搜索功能:
controller层:
@Controller
[AppleScript] 纯文本查看 复制代码
public class SearchController {
@Autowired
private SearchService searchService;
@RequestMapping("/search")
public String search(@RequestParam("q")String queryString, @RequestParam(defaultValue="1")Integer page, Model model) {
if (queryString != null) {
try {
queryString = new String(queryString.getBytes("iso8859-1"), "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
SearchResult searchResult = searchService.search(queryString, page);
//向页面传递参数
model.addAttribute("query", queryString);
//model.addAttribute("totalPages", searchResult.getPageCount());
model.addAttribute("itemList", searchResult.getItemList());
model.addAttribute("page", page);
return "search";
}
}
service层:
[AppleScript] 纯文本查看 复制代码
@Service
public class SearchServiceImpl implements SearchService {
@Value("${SEARCH_BASE_URL}")
private String SEARCH_BASE_URL;
@Override
public SearchResult search(String queryString, int page) {
//这里需要的是连接+参数.这里每页显示的记录条数,可以传递也可以不用传递
// 调用taotao-search的服务
//查询参数
Map<String, String> param = new HashMap<>();
param.put("q", queryString);
param.put("page", page + "");
try {
//调用服务
String json = HttpClientUtil.doGet(SEARCH_BASE_URL, param);
//把字符串转换成java对象
TaotaoResult taotaoResult = TaotaoResult.formatToPojo(json, SearchResult.class);
SearchResult result = (SearchResult) taotaoResult.getData();
return result;
/* if (taotaoResult.getStatus() == 200) {
}*/
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}