主要结构:

查询 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);
}
}

结果:

数据添加到solr索引库后前台如何搜索的更多相关文章

  1. 将数据库的数据导入solr索引库中

    在solr与tomcat整合文章中,我用的索引库是mycore,现在就以这个为例. 首先要准备jar包:solr-dataimporthandler-4.8.1.jar.solr-dataimport ...

  2. 使用solrj操作solr索引库

    (solrj)初次使用solr的开发人员总是很郁闷,不知道如何去操作solr索引库,以为只能用<五分钟solr4.5教程(搭建.运行)>中讲到的用xml文件的形式提交数据到索引库,其实没有 ...

  3. 使用solrj操作solr索引库,solr是lucene服务器

    客户端开发 Solrj 客户端开发 Solrj Solr是搭建好的lucene服务器 当然不可能完全满足一般的业务需求 可能 要针对各种的架构和业务调整 这里就需要用到Solrj了 Solrj是Sol ...

  4. 如何在分布式环境中同步solr索引库和缓存信息

    天气依旧很好,主要是凉快.老习惯,我在北京向各位问好. 搜索无处不在,相信各位每天都免不了与它的亲密接触,那么我想你确实有必要来了解一下它们,就上周在公司实现的一个小需求来给各位分享一下:如何在分布式 ...

  5. 商城06——solr索引库搭建&solr搜索功能实现&图片显示问题解决

    1.   课程计划 1.搜索工程的搭建 2.linux下solr服务的搭建 3.Solrj使用测试 4.把数据库中的数据导入索引库 5.搜索功能的实现 2.   搜索工程搭建 要实现搜索功能,需要搭建 ...

  6. solr索引库的创建

    solr索引库的创建 一.找到你安装的[solrhome]目录(我的是这个) 二.进入该目录 三.选择其中任意一个索引库复制一份到该目录下并更名为要创建的索引库名称 四.进入[myindex]目录下, ...

  7. JAVAEE——宜立方商城09:Activemq整合spring的应用场景、添加商品同步索引库、商品详情页面动态展示与使用缓存

    1. 学习计划 1.Activemq整合spring的应用场景 2.添加商品同步索引库 3.商品详情页面动态展示 4.展示详情页面使用缓存 2. Activemq整合spring 2.1. 使用方法 ...

  8. solr 索引库的维护

    一.配置中文分析器:IK-analyzer,在FieldType中指定中文分析器:1 复制IK-analyzer到你的服务器指定目录中.2 在该目录中,我们需要的东西有:IKAnalyzer的jar包 ...

  9. 维护solr索引库

    一 2)solrcore    一个solr下可以有多个solrcore,每个solrcore就是一个独立的索引库3)solrconfig.xml    lib:配置solr的扩展包的位置,不指定路径 ...

随机推荐

  1. Linux命令应用大词典-第14章 显示登录用户

    14.1 w:详细查询已登录当前计算机的用户 14.2 who:显示已登录当前计算机用户的简单信息 14.3 whoami:显示与当前的有效ID相关联的用户名 14.4 logname:显示当前用户的 ...

  2. 随机森林random forest及python实现

    引言想通过随机森林来获取数据的主要特征 1.理论根据个体学习器的生成方式,目前的集成学习方法大致可分为两大类,即个体学习器之间存在强依赖关系,必须串行生成的序列化方法,以及个体学习器间不存在强依赖关系 ...

  3. C++ 学习笔记之——字符串和字符串流

    1. 字符数组 字符数组,也就是存放字符类型数据的数组,只不过字符数组的结尾必须是 '\0'.C++ 已经提供了一些字符串处理函数,这些函数被封装在头文件 和 <string.h> 中. ...

  4. Fluent Python: Mutable Types as Parameter Defaults: Bad Idea

    在Fluent Python一书第八章有一个示例,未看书以先很难理解这个示例运行的结果,我们先看结果,然后再分析问题原因: 定义了如下Bus类: class Bus: def __init__(sel ...

  5. redis集群sentinel哨兵模式的搭建与实际应用

    参考资料:https://blog.csdn.net/men_wen/article/details/72724406 之前环境使用的keepalived+redis vip集群模式,现在我们服务切换 ...

  6. Alpha冲刺——第一天

    Alpha第一天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...

  7. python学习笔记01:安装python

    下载python: 从从https://www.python.org/downloads/下载python,根据操作系统的不同,选择不同的版本下载.注意:linux系统大多预装了python,可以直接 ...

  8. Javascript动态方法调用与参数修改的问题

    Javascript中可以对所传参数在函数内进行修改,如下 ? 1 2 3 4 5 function func1(name) {     name = 'lily';     alert(name); ...

  9. netbeans调试配置

    apache端口8050,xdebug端口9000 1.把项目放到apache的htdocs下(一定要放在htdocs上,要么调试的时候xdebug会一直卡在“等待连接中”) 2.把php_xdebu ...

  10. C/S结构 B/S结构

    [1]C/S 结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配到Client端和Server端来实现,降低了系统的通讯开销.目前大多数应 ...