需要工具:solr-4.10.3以及IK Analyzer 2012FF_hf1中文分词器
下载:https://download.csdn.net/download/ly_linyuan/10373271
window中
1.把solr的war包复制到自己的tomcat 的webapp目录下。既\solr-4.10.3\dist\solr-4.10.3.war复制到(我这里tomcat路径为)D:\temp\apache-tomcat-7.0.53\webapps下。
2.改名为solr.war
3.执行tomcat的bin目录下的startup.bat启动tomcat解压solr.war
4.解压后删除未解压的原solr.war文件
5.把\solr-4.10.3\example\lib\ext目录下的所有的jar包添加到tomcat中的solr/WEB-INF/lib中
6.将solr-4.10.3文件下的example中的solr文件,复制到磁盘根目录下(不要有中文名) 我这里是D盘,改名为solrhome 即文件路径D:\solrhome
7.更改tomcat下的slor的WEB-INF下的web.xml文件,将solrhome的路径添加进去
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>D:\solrhome</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
8.启动tomcat 访问localhost:8080/solr 可以进入solr浏览器操作界面
9.增加实例,在solrhome中添加 复制collection1命名为collection2,
并将core.properties中的name=collection1更改为name=collection2
10.添加中文分词器:
1)将中文分词器的jar包IKAnalyzer2012FF_u1.jar复制到apache-tomcat-7.0.53\webapps\solr\WEB-INF\lib中
2)在apache-tomcat-7.0.53\webapps\solr\WEB-INF下新建一个classes文件
3)将IK Analyzer 2012FF_hf1中的IKAnalyzer.cfg.xml和stopword.dic以及自己新建的分词文档复制到新建的classes文件中
4)在D:solrhome\collectoin1\conf\schema.xml中末尾处进行配置
<!-- IKAnalyzer-->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<!--IKAnalyzer Field-->
<field name="title_ik" type="text_ik" indexed="true" stored="true" />
实例
1.针对具体的业务需要自定义一套Field,如下是商品信息Field:定义在D:solrhome\collectoin1\conf\schema.xml中
<!--good-->
<field name="good_name" type="text_ik" indexed="true" stored="true"/>
<field name="good_image" type="string" indexed="false" stored="true" />
<field name="good_marketprice" type="float" indexed="true" stored="true"/>
<field name="good_price" type="float" indexed="true" stored="true"/>
<field name="good_description" type="string" indexed="true" stored="true" />
<field name="good_source" type="string" indexed="true" stored="true" />
2.在D:solrhome\collectoin1中新建lib文件夹
3.将solr-4.10.3\dist中的solr-dataimporthandler-4.10.3.jar与solr-dataimporthandler-extras-4.10.3.jar复制到新建的lib文件夹中
4.需要导入数据库,添加数据库驱动包mysql-connector-java-5.1.7-bin.jar
5.创建一个data-config.xml,保存到collection1\conf\目录下
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/lymall"
user="root"
password="123456"/>
<document>
<entity name="good" query="SELECT id,name,image,marketprice,price,description,source FROM good ">
<field column="id" name="id"/>
<field column="name" name="good_name"/>
<field column="image" name="good_image"/>
<field column="marketprice" name="good_marketprice"/>
<field column="price" name="good_price"/>
<field column="description" name="good_description"/>
<field column="source" name="good_source"/>
</entity>
</document>
</dataConfig>
6.在D:\solrhome\collection1\conf中的solrconfig.xml文件中添加
<requestHandler name="/dataimport"
class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
7.重启tomcat 访问localhost:8080/solr 进入collection1中的Dataimport
8.导入数据库中的数据 选择full-import全部导入 勾选clean(自动清理)和commit(自动提交)
9.在solr浏览器界面查询数据效验一下:
10.solr浏览器界面操作删除指南
1)删除(指定删除):
<delete>
<id>1</id>
</delete>
<commit>
2)删除所有:
<delete>
<query>*:*</query>
</delete>
<commit/>
应用:
11.在项目中配置spring整合solr的配置文件
spring-solr.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- SolrServer的配置 -->
<bean id="httpSolrServer" class="org.apache.solr.client.solrj.impl.HttpSolrServer">
<!-- index=0 代表调用有一个构造参数的solrServer的构造方法 -->
<constructor-arg index="0" value="http://localhost:8081/solr"/>
</bean>
</beans>
12.先建立一个之前定义的商品信息的pojo
package com.ly.pojo;
/**
* 商品详情显示属性
* @author linyuan
*
*/
public class GoodModel {
// 商品编号
private String id;
// 商品名称
private String name;
// 商品市场价格
private float marketprice;
// 价格
private float price;
// 商品描述
private String description;
// 图片名称
private String image;
// 商品供应商地址
private String source;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getMarketprice() {
return marketprice;
}
public void setMarketprice(float marketprice) {
this.marketprice = marketprice;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
}
13.后台查询代码:
@Autowired
private SolrServer solrServer;
/**
* 根据商品名称查询所有分类的相关名称商品
* @throws SolrServerException
*/
@Override
public List<GoodModel> query(String name, String price, String sort,String source) throws SolrServerException {
//创建查询条件对象
SolrQuery solrQuery = new SolrQuery();
//设置默认搜索域
solrQuery.set("df", "good_name");
//设置查询关键字
if(name != null && !"".equals(name)){
solrQuery.setQuery(name);
} else {
solrQuery.setQuery("*:*");
}
//设置过滤条件按照分类名称进行过滤
if(name != null && !"".equals(name)){
solrQuery.addFilterQuery("good_name:" + name);
}
//设置过滤条件按照价格进行过滤
if(price != null && !"".equals(price)){
String[] split = price.split("-");
if(split != null && split.length > 1){
solrQuery.addFilterQuery("good_price:["+split[0]+" TO "+split[1]+"]");
}
}
//设置排序
if("1".equals(sort)){
solrQuery.addSort("good_price", ORDER.asc);
} else {
solrQuery.addSort("good_price", ORDER.desc);
}
//设置高亮显示
solrQuery.setHighlight(true);
//设置高亮显示的域
solrQuery.addHighlightField("good_name");
//设置高亮前缀
solrQuery.setHighlightSimplePre("<span style=\"color:red\">");
//设置高亮后缀
solrQuery.setHighlightSimplePost("</span>");
//查询并获取查询响应
QueryResponse queryResponse = solrServer.query(solrQuery);
//从响应中获取查询结果集
SolrDocumentList docList = queryResponse.getResults();
//创建返回结果对象
List<GoodModel> goodList = new ArrayList<GoodModel>();
//遍历结果集
if(docList != null){
//获取总记录数
for(SolrDocument doc : docList){
GoodModel good = new GoodModel();
good.setId(String.valueOf(doc.get("id")));
//获取高亮
Map<String, Map<String, List<String>>> highlighting = queryResponse.getHighlighting();
if(highlighting != null){
List<String> list = highlighting.get(doc.get("id")).get("good_name");
if(list != null && list.size() > 0){
good.setName(list.get(0));
} else {
good.setName(String.valueOf(doc.get("good_name")));
}
} else {
good.setName(String.valueOf(doc.get("good_name")));
}
if(doc.get("good_price") != null && !"".equals(doc.get("good_price"))){
good.setPrice(Float.valueOf(doc.get("good_price").toString()));
}
good.setSource(String.valueOf(doc.get("good_source")));
good.setImage(String.valueOf(doc.get("good_image")));
goodList.add(good);
}
}
return goodList;
}
14:效果展示: