目标1:完成solr环境安装、中文分析器和业务域的配置

目标2:会使用Spring Data Solr完成增删改查操作

目标3:完成批量数据导入功能

目标4:完成按关键字搜索功能

1.业务接口层

1 public interface ItemSearchService {
2     /**
3      * 搜索
4      * @param 
5      * @return
6      */
7     public Map<String,Object> search(Map searchMap);
8 }
View Code

2.服务实现层

applicationContext-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:solr="http://www.springframework.org/schema/data/solr"
    xsi:schemaLocation="http://www.springframework.org/schema/data/solr 
          http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- solr服务器地址 -->
    <solr:solr-server id="solrServer" url="http://192.168.188.128:8080/solr" />
    <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg ref="solrServer" />
    </bean>
</beans>

 

在src/main/resources/spring 下applicationContext-service.xml   : dubbo端口20884

 1 @Service(timeout = 3000)
 2 public class ItemSearchServiceImpl2 implements ItemSearchService {
 3 
 4     @Autowired
 5     private SolrTemplate solrTemplate;
 6 
 7     @Override
 8     public Map<String, Object> search(Map searchMap) {
 9 
10         //获取查询条件
11         String keywords = String.valueOf(searchMap.get("keywords"));
12         Integer pageNo = Integer.parseInt(String.valueOf(searchMap.get("pageNo")));
13         //获取每页查询条数
14         Integer pageSize = Integer.parseInt(String.valueOf(searchMap.get("pageSize")));
15 
16 
17         Map<String,Object> resultMap=new HashMap<>();
18         //封装查询对象
19         Query query=new SimpleQuery();
20         //添加查询条件
21         Criteria criteria=new Criteria("item_keywords").is(searchMap.get("keywords"));
22         //将查询的条件放入查询的对象
23         query.addCriteria(criteria);
24         if (pageNo == null || pageNo <= 0){
25             pageNo=1;
26         }
27         //当前页开始是第几条
28         Integer start = (pageNo-1)*pageSize;
29         //设置第几条开始
30         query.setOffset(start);
31         //每页查询多少条数据
32         query.setRows(pageSize);
33         //去solr查询并返回结果
34         ScoredPage<Item> page = solrTemplate.queryForPage(query, Item.class);
35         resultMap.put("rows",page.getContent());
36         return resultMap;
37     }
38 }
View Code

3.控制层

 1 @RestController
 2 @RequestMapping("/itemsearch")
 3 public class ItemSearchController {
 4     @Reference
 5     private ItemSearchService itemSearchService;
 6     
 7     @RequestMapping("/search")
 8     public Map<String, Object> search(@RequestBody Map searchMap ){
 9         return  itemSearchService.search(searchMap);
10     }    
11 }
View Code

 

posted on 2019-11-21 22:00  protect24  阅读(402)  评论(0编辑  收藏  举报