开发人员如何快速定制化实现一个基于Solr的搜索引擎( 四 )


搜索Solr:
public void searchSolr(){
SolrQuery query=new SolrQuery();
// 设置查询条件
query.setQuery("手机");
// 分页查询
query.setStart(0);
query.setRows(10);
// 设置查询的默认域
query.set("df","tb_item_keywords");
// 设置高亮显示
query.setHighlight(true);
query.addHighlightField("tb_item_title");
query.setHighlightSimplePre("<span style='color:red;'>");
query.setHighlightSimplePost("</span>");
// 开始查询
try{
QueryResponse queryResponse=solrClient.query(query);
SolrDocumentList results=queryResponse.getResults();
【开发人员如何快速定制化实现一个基于Solr的搜索引擎】// 获取高亮
Map<String,Map<String,List<String>>> highlighting=queryResponse.getHighlighting();
for(SolrDocument result:results){
List<String> strings=highlighting.get(result.get("id")).get(result.get("tb_item_title"))
if(strings!=null&&strings.size()>0){
String title=strings.get(0);
System.out.println(title);
}
}
}catch(SolrServerException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}

  • 创建SearchServiceImpl实现SearchService接口
package com.oxford.myshop.service.search.provider.api.impl;
@Service(version="${services.versions.search.v1}")
public class SearchServiceImpl implements SearchService{
@Autowired
private SolrClient solrClient;
@Override
public List<TbItemResult> search(String query,int page,int rows){
List<TbItemResult> searchResults=Lists.newArrayList();
SolrQuery query=new SolrQuery();
// 设置查询条件
query.setQuery("手机");
// 分页查询
query.setStart((page-1)*rows);
query.setRows(rows);
// 设置查询的默认域
query.set("df","tb_item_keywords");
// 设置高亮显示
query.setHighlight(true);
query.addHighlightField("tb_item_title");
query.setHighlightSimplePre("<span style='color:red;'>");
query.setHighlightSimplePost("</span>");
// 开始查询
try{
QueryResponse queryResponse=solrClient.query(query);
SolrDocumentList results=queryResponse.getResults();
// 获取高亮
Map<String,Map<String,List<String>>> highlighting=queryResponse.getHighlighting();
for(SolrDocument solrDocument:solrDocuments){
TbItemResult result=new TbResult();
result.setId(Long.parseLong(String.valueOf(solrDocument.get("id"))));
result.setTbItemCid(Long.parseLong(String.valueOf(solrDocument.get("tb_item_cid"))));
result.setTbItemCname((String)solrDocument.get("tb_item_cname"));
result.setTbItemTitle((String)solrDocument.get("tb_item_title"));
result.setTbItemSellPoint((String)solrDocument.get("tb_item_sell_point"));
result.setTbItemDesc((String)solrDocument.get("tb_item_desc"));
String tbItemTitle="";
List<String> list=highlighting.get(result.get("id")).get(result.get("tb_item_title"))
if(list!=null&&lsit.size()>0){
String title=list.get(0);
}else{
tbItemTitle=(String)solrDocument.get("tb_item_title");
}
result.setTbItemTitle(tbItemTitle);
searchResults.add(result);
}
}catch(SolrServerException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return searchResults;
}
}
创建搜索服务消费者
  • 创建搜索服务消费者myshop-service-search-consumer对Solr数据库中的数据进行检索
  • MyShopServiceSearchConsumerApplication
package com.oxford.myshop.service.search.consumer;
@EnableHystrix
@EnableHystrixDashboard
@SpringBootApplication(scanBasePackages="com.oxford.myshop",exclude=DataSourceAutoConfiguration.class)
public class MyShopServiceSearchConsumerApplication{
public static void main(String[] args){
SpringApplication.run(MyShopServiceSearchConsumerApplication.class,args);
Main.main(args);
}
}
  • SearchController
package com.oxford.myshop.service.search.consumer.controller;
@RestController
public class SearchController{
@Reference(version="${services.versions.search.v1}")
private SearchService searchService;
@RequestMapping(value=https://www.isolves.com/it/cxkf/bk/2021-04-02/"search/{query}/{page}/{rows}",method=RequestMethod.GET)
public List<TbItemResult> search(
@PathVariable(required=true) String query,


推荐阅读