主要结构:

查询 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. python学习笔记03 --------------程序交互与格式化输出

    1.读取用户输入内容 语法:input() 例: name = input('你的名字是?) print('你好'+name) 程序会等待用户输入名字后打印:你好(用户输入的名字) 注意:input接 ...

  2. CSP201403-2:窗口

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...

  3. mysql中的select语句where条件group by ,having , order by,limit的顺序及用法

    -- 语法: SELECT select_list FROM table_name [ WHERE search_condition ] [ GROUP BY group_by_expression ...

  4. Python3 小工具-TCP半连接扫描

    from scapy.all import * import optparse import threading def scan(ip,port): pkt=IP(dst=ip)/TCP(dport ...

  5. Caused by: java.lang.NoClassDefFoundError: javax/el/ELManager

    出现问题的原因: 在将springboot项目部署到Linux下的Tomcat中,项目无法正常启动(本地能正常运行),Tomcat启动日志中出现: Caused by: java.lang.NoCla ...

  6. 1.安装CDH5.12.x

    安装方式安装前准备安装步骤安装过程修改/etc/hosts设置ssh 互信修改linux 系统设置安装JDK1.8安装python2.7安装mysql/postgreysql数据库安装ntp设置本地y ...

  7. JavaWeb基础 - 会话

    会话概述 什么是会话 简单的理解:用户打开浏览器,点击多个超链接,访问Web服务器上多个资源,然后关闭浏览器,整个过程称之为一次会话. 需要解决的问题 每个用户在使用浏览器与服务器会话的过程中,会产生 ...

  8. <Effective C++>读书摘要--Ctors、Dtors and Assignment Operators<二>

    <Item 9> Never call virtual functions during construction or destruction 1.you shouldn't call ...

  9. OSG学习:矩阵变换节点示例

    #include<osgViewer\Viewer> #include<osg\Node> #include<osg\Geode> #include<osg\ ...

  10. 【Quartz.net】- Cron表达式

    一.结构 corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份 二.各字段的含义   字段 允许值 允许的特殊字符 秒(Seconds) 0~59的整数 , - * / ...