solr全文检索部署开发
一、软件环境
- centos6
- solr-5.2.1.tgz
- 安装一次后,可以作为未安装版,后期省去配置
二、solr安装
-
解压
tar -xvf solr-5.2.1.tgz
-
先启动一次,再配置中文分词等。这样内部会去复制war到,进行解压。
./solr start #启动solr ./solr stop –all #停止服务 ./solr restart #重启服务
-
打开端口
/sbin/iptables -I INPUT -p tcp --dport 8983 -j ACCEPT /etc/rc.d/init.d/iptables save /etc/init.d/iptables status
-
访问网页solr
http://192.168.8.157:8983/solr/#/
-
创建项目的core,需要手动创建目录
创建core时,不会自动创建目录。
在此目录,手动创建core文件夹 /usr/local/src/solr/solr-5.2.1/server/solr
制作core文件夹,配置ik中文分词器等,已经做成压缩包,直接上传解决。
三、spingSSM整合使用solr
-
maven 依赖
<!-- solr--> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-solrj</artifactId> <version>5.2.1</version> </dependency> <!-- IK分词器,maven官网没有,自己按下面坐标建立路径 --> <dependency> <groupId>org.wltea.analyzer</groupId> <artifactId>ik-analyzer</artifactId> <version>5.3.0</version> </dependency>
-
整合步骤:
1) 输入查询的内容,进行查询,访问jt-search系统controller
2) 获取到查询内容,中文乱码get请求,加分页参数
3) 请求solr,solr和spring框架整合applicationContext-solrj.xml,httpSolrServer对象
4) 在jt-search.service就可以注入这个对象,拼接参数提交请求,得到响应,获取返回值,有两个:当前页的数据,记录总数。SysResult.status=200;SysResult.data=当前页数据List,SysResult.msg=记录总数。
5) 在jsp页面来展示,展示列表
-
spring配置文件:
applicationContext-solrj.xml
<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.HttpSolrClient"> <constructor-arg index="0" value="${SOLR.URL}"/> <!-- 设置响应解析器,solrj没有提供json解析器,所以通常用xml解析器 --> <property name="parser"> <bean class="org.apache.solr.client.solrj.impl.XMLResponseParser"/> </property> <!-- 设置重试次数,推荐设置为1 --> <property name="maxRetries" value="1"/> <!-- 建立连接的最长时间 ,单位是:毫秒--> <property name="connectionTimeout" value="500"/> </bean> </beans>
-
配置文件中的SOLR.URL
#http://192.168.1.13:8983/solr/jt/select?q=*%3A*&wt=json&indent=true SOLR.URL=http://solr.jt.com/solr/jt
-
Pojo层,构建查询出的对象Item
package com.chen.search.pojo; import org.apache.solr.client.solrj.beans.Field; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @JsonIgnoreProperties(ignoreUnknown=true) //当转换时,属性不匹配,忽略 public class Item extends BasePojo{ @Field //solr搜索的结果和实体对象属性映射 private Long id; @Field private String title; @Field private String sellPoint; @Field private Long price; @Field private Integer num; @Field private String barcode; @Field private String image; @Field private Long cid; @Field private Integer status; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getSellPoint() { return sellPoint; } public void setSellPoint(String sellPoint) { this.sellPoint = sellPoint; } public Long getPrice() { return price; } public void setPrice(Long price) { this.price = price; } public Integer getNum() { return num; } public void setNum(Integer num) { this.num = num; } public String getBarcode() { return barcode; } public void setBarcode(String barcode) { this.barcode = barcode; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public Long getCid() { return cid; } public void setCid(Long cid) { this.cid = cid; } public Integer getStatus() { return status; } public void setStatus(Integer status) { this.status = status; } public String[] getImages(){ return this.image.split(","); } }
-
Service层注入使用
package com.chen.search.service; import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.impl.HttpSolrClient; import org.apache.solr.client.solrj.response.QueryResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.chen.common.vo.SysResult; import com.chen.search.pojo.Item; @Service public class ItemService { //查询,通过solr来查询 @Autowired private HttpSolrClient httpSolrClient; public SysResult search(String q, Integer page, Integer rows){ /* * 步骤: * 1. 链接solr,配置参数,page当前页,rows一页条数,q查询条件 * 2. 进行查询,调用solr提供RESTFul,返回是一个自身返回对象,从这个对象中解析出来List和numFount * 3. 放置到SysResult * 4. 对搜索的关键字加亮 */ try { q = new String(q.getBytes("ISO-8859-1"),"utf-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } SolrQuery solrQuery = new SolrQuery(); Integer startPos = (Math.max(page, 1)-1)*rows; solrQuery.setStart(startPos); //哪一页第一条记录位置 solrQuery.setQuery(q); //设置查询条件 //高亮 solrQuery.setHighlight(true); solrQuery.setHighlightSimplePre("<span class=\"red\">"); solrQuery.setHighlightSimplePost("</span>"); solrQuery.addHighlightField("title"); //对title字段进行高亮 try { QueryResponse response = httpSolrClient.query(solrQuery); List<Item> itemList = response.getBeans(Item.class); //获取到当前的也记录 String numFount = ""+ response.getResults().getNumFound(); //获取到记录总数 return SysResult.build(200, numFount, itemList); //利用msg属性传递记录总数 } catch (Exception e) { e.printStackTrace(); } return SysResult.build(201, "solr查询异常!"); } }
-
Controller层
package com.chen.search.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.chen.common.vo.SysResult; import com.chen.search.service.ItemService; @Controller public class ItemController { @Autowired private ItemService itemService; //访问solr进行查询 //"http://search.chen.com/search/"+keyWords+"/"+page+"/"+rows; @RequestMapping("/search/{keyWords}/{page}/{rows}") @ResponseBody public SysResult search(@PathVariable String keyWords,@PathVariable Integer page,@PathVariable Integer rows){ return itemService.search(keyWords, page, rows); } }
end