在项目中使用Solr

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         version="2.5">

  <!-- 加载spring容器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:spring/applicationContext*.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

2个配置文件(在resources下的spring文件夹下)

applicationContext-service.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:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
 
    <dubbo:protocol name="dubbo" port="20884"></dubbo:protocol>
    <dubbo:application name="pinyougou-search-service"/>  
    <dubbo:registry address="zookeeper://192.168.200.128:2181"/>
    <dubbo:annotation package="cn.wangju.core.service" />
</beans>

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.200.128:8080/solr" />
    <!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
    <bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
        <constructor-arg ref="solrServer" />
    </bean>
</beans>

业务逻辑层的实现类

package cn.wangju.core.service;
/**
 * @author wangju
 * @date 2019/11/21 19:20
 */
@Service
public class SearchItemServiceImpl implements SearchItemService{
    @Autowired
    private SolrTemplate solrTemplate;

    @Override
    public Map<String, Object> searchItem(Map searchMap) {
        // 获取查询的条件
        String keywords = (String)searchMap.get("keywords");
        //当前页
        Integer pageNo = (Integer) searchMap.get("pageNo");
        //每页查询多少条
        Integer pageSize = (Integer) searchMap.get("pageSize");

        // 封装查询对象
        Query query = new SimpleQuery();
        Criteria criteria = new Criteria("item_keywords").is(keywords);
        //将查询的条件放入查询的对象
        query.addCriteria(criteria);
        if (pageNo!=null || pageNo<0){
            pageNo = 1;
        }
        pageNo = (pageNo-1)*pageSize;
        // 设置第几条开始
        query.setOffset(pageNo);
        // 每页查询多少条数据
        query.setRows(pageSize);
        // 去solr 查询并返回结果
        ScoredPage<Item> items = solrTemplate.queryForPage(query, Item.class);
        Map<String,Object> map = new HashMap<>();
        map.put("rows",items.getContent());
        map.put("totalPages",items.getTotalPages());
        map.put("total",items.getTotalElements());
        return map;
    }
}

  

猜你喜欢

转载自www.cnblogs.com/wangju/p/11909033.html