主要结构:

查询 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. lintcode100 删除排序数组中的重复数字

    删除排序数组中的重复数字   给定一个排序数组,在原数组中删除重复出现的数字,使得每个元素只出现一次,并且返回新的数组的长度. 不要使用额外的数组空间,必须在原地没有额外空间的条件下完成. 您在真实的 ...

  2. css多行文本溢出显示省略号(…)

    text-overflow:ellipsis属性可以实现单行文本的溢出显示省略号(…).但部分浏览器还需要加宽度width属性. css代码: overflow: hidden; text-overf ...

  3. 线性代数之——对角化和 A 的幂

    利用特征向量的属性,矩阵 \(A\) 可以变成一个对角化矩阵 \(\Lambda\). 1. 对角化 假设一个 \(n×n\) 的矩阵 \(A\) 有 \(n\) 个线性不相关的特征向量 \(x_1, ...

  4. 查找当前对象中的方法对象的属性叫做_event_name的方法

    setattr( self, '_events', {e._event_name: e for _, e in getmembers(self, lambda x: ismethod(x) and h ...

  5. 1.EOS源码编译运行

    目前网络上都是针对老版EOS2.0源码编译的文章,我在mac上参考这些文章编译,最后发现根本就不对,最新版本只需一条命令(./eosio_build.sh,依赖库会自动安装的)即可.我根据这些文章手动 ...

  6. 使用HTML5制作时钟

    之前看到别人用HTML5制作时钟,自己也写了一个,这是很久以前写的了,没有注释,现在自己看都晕了(注释的重要性就体现在这边了),找时间加上注释,让自己和别人都比较好理解. <!DOCTYPE h ...

  7. http://www.cnblogs.com/120626fj/p/7545958.html

    1.本周PSP 2.本周进度条: 代码行,博文字数,用到的知识点 3.累计进度图 3.1累计代码折线图 3.2累计博文字数折线图 4.本周PSP饼状图

  8. js中斜杠转义

    js中“/”不需要转义. if(myPath.indexOf("/Upload/EmailFile/")!=-1){ alert("有附件!")}

  9. 修改IntelliJ IDEA字体

  10. cacti监控多个mysql端口

    1. 在Console -> Data Templates 找到mysql-cacti-templates的mysql模板,编辑: 在这里面把Port的Use Per-Data Source V ...