搜索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接口
@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
@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
@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,
推荐阅读
- 理解HTTPS及配置Django+HTTPS开发环境
- 自制蜜桔干,如何自制桂花茶
- 软件测试开发实战|接口自动化测试框架开发
- 浅谈在Linux中如何将脚本做成系统服务开机自启动
- 我们将如何在火星上生活 火星在地球
- 如何在主流浏览器上正常使用只兼容IE的“上古网站”?
- 八种实用的免费游戏开发软件工具
- 如何用谷歌关键词规划师挖掘有效关键词?
- 云呼叫中心的核心功能有哪些?企业如何应用?
- 如何快速去水印批量搬运短视频,教你玩转自媒体短视频