数据添加到solr索引库后前台如何搜索

主要结构:

 

查询 Dao:

package com.taotao.search.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.taotao.search.dao.SearchDao;
import com.taotao.search.pojo.Item;
import com.taotao.search.pojo.SearchResult;

@Repository
public class SearchDaoImpl implements SearchDao{

    @Autowired
    SolrServer solrServer;
    
    @Override
    public SearchResult search(SolrQuery query) throws Exception {
        //返回值对象
        SearchResult result = new SearchResult();
        //根据查询条件查询索引库
        QueryResponse response = solrServer.query(query);
        //取查询结果
        SolrDocumentList documentList = response.getResults();
        //取查询结果总数量
        result.setRecordCount(documentList.getNumFound());
        //商品列表
        List<Item> itemList = new ArrayList<Item>();
        //取高亮显示
        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();
        //取商品列表
        for (SolrDocument document : documentList) {
            Item item = new Item();
            item.setId((String) document.get("id"));
            //取高亮显示结果
            List<String> list = highlighting.get(document.get("id")).get("item_title");
            String title = "";
            if (list!=null && list.size()>0) {
                title = list.get(0);
            }else{
                title = (String) document.get("item_title");
            }
            item.setTitle(title);
            item.setImage((String) document.get("item_image"));
            item.setPrice((long) document.get("item_price"));
            item.setCategory_name((String)document.get("item_category_name"));
            //添加到商品列表
            itemList.add(item);
        }
        result.setItemList(itemList);
        return result;
    }

}

其中的 注入的solrServer是在 配置文件中配的:

增加   applicationContext-solr.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    
    
    <bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
        <constructor-arg name="baseURL" value="${SOLR_SERVER_URL}"></constructor-arg>
    </bean>
</beans>

resource.properties

#solrServer的url
SOLR_SERVER_URL=http://192.168.29.102:8080/solr

 

service层:

package com.taotao.search.service.impl;

import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.taotao.search.dao.SearchDao;
import com.taotao.search.pojo.SearchResult;
import com.taotao.search.service.SearchService;

@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(page);
        
        return searchResult;
    }
}

 

Controller层:

package com.taotao.search.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.search.pojo.SearchResult;
import com.taotao.search.service.SearchService;

@Controller
public class SearchController {

    @Autowired
    private SearchService searchService;
    
    @RequestMapping("/query")
    @ResponseBody
    public TaotaoResult search(@RequestParam("q")String queryString,
            @RequestParam(defaultValue="1")Integer page,
            @RequestParam(defaultValue="60")Integer rows){
        if (StringUtils.isEmpty(queryString)) {
            return TaotaoResult.build(400, "查询条件不能为空");
        }
        SearchResult result = null;
        try {
            //解决参数乱码
            queryString = new String(queryString.getBytes("ISO8859-1"), "utf-8");
            result = searchService.search(queryString, page, rows);
        } catch (Exception e) {
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
        return  TaotaoResult.ok(result);
    }
}

 

结果:

 

posted @ 2018-10-16 09:47  戈博折刀  阅读(479)  评论(0编辑  收藏  举报