安装 solr
1、下载 solr 和 tomcat
2、将D:\solr-4.9.1\example\webapps\solr.war
拷贝到 D:\apache-tomcat-9.0.12\webapps 这个目录下
3、这个D:\solr-4.9.1\example\solr
拷贝 D:\apache-tomcat-9.0.12\date
4、修改WEB-INF目录下的web.xml文件
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>D:\apache-tomcat-9.0.12\date\solr</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
5、将dist中的jar包和example/lib/ext中的jar包,
数据库jar包拷贝到Tomcat的WEB-INF下面的lib目录
6、以上配着完成基本已经完成 http://localhost:8080/solr/#/ 测试一下
新建core
7. D:\apache-tomcat-9.0.12\date\solr\在这个地方新建文件test,
D:\apache-tomcat-9.0.12\date\solr\collection1 这个目录下的所有文件
拷贝到 D:\apache-tomcat-9.0.12\date\solr\test
修改test中的 core.properties 里面的 name=test
8、重启tomcat http://localhost:8080/solr/#/ 测试一下
9、导入数据配置
<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
<lst name="defaults">
<str name="config">data-config.xml</str>
</lst>
</requestHandler>
10、数据源 增量更新
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/itripdb"
user="root" password="bdqn"/>
<document name="products">
<entity name="item" pk="id" query="SELECT id,hotelName,countryId,provinceId FROM itriphotel"
deltaImportQuery="SELECT id,hotelName,countryId,provinceId FROM itriphotel where id='${dih.delta.id}'"
deltaQuery="SELECT id,hotelName,countryId,provinceId FROM itriphotel where modifyDate > '${dih.last_index_time}'">
<field column="id" name="id" />
<field column="hotelName" name="hotelName" />
<field column="countryId" name="countryId" />
<field column="provinceId" name="provinceId" />
</entity>
</document>
</dataConfig>
11、字段类型声明
12、增量更新配置文件 dataimport.properties
http://wiki.apache.org/solr/DataImportHandler#Scheduling
13、配置监听器
<listener>
<listener-class>
org.apache.solr.handler.dataimport.scheduler.ApplicationListener
</listener-class>
</listener>
IK分词配置
14、解压 4.2 IK分词器.zip
在 C:\Users\Administrator\Desktop\IK-Analyzer-2012FF\dist 下
考培 IKAnalyzer2012FF_u1.jar 到 D:\apache-tomcat-9.0.12\webapps\solr\WEB-INF\lib
D:\apache-tomcat-9.0.12\webapps\solr\WEB-INF\lib
考培 IKAnalyzer.cfg.xml 和 stopword.dic 到 D:\apache-tomcat-9.0.12\webapps\solr\WEB-INF\classes
D:\apache-tomcat-9.0.12\date\solr\test\conf\solrconfig.xml 添加
15、添加分词字段
<field name="hotelName" type="text_ik" indexed="true" stored="true"/>
16、添加分词映射字段
<field name="key" type="text_ik" indexed="true" stored="false" multiValued="true"/>
<copyField source="hotelName" dest="key"/>
17、分词条件
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
<analyzer type="query" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
结合springmvc实现
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:solr="http://www.springframework.org/schema/data/solr"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置solr 服务器 -->
<solr:solr-client id="solrClient" timeout="3000" maxConnections="100" url="http://localhost:9999/solr/test/"/>
<!-- 配置solrTemplate -->
<bean id="solrTemplate" scope="singleton" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrClient"/>
</bean>
<bean id="query" scope="prototype" class="org.springframework.data.solr.core.query.SimpleQuery"/>
<context:component-scan base-package="com"/>
</beans>
maven依赖
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-solr</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
java 代码
package com;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.result.ScoredPage;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class SolrServiceImpl implements SolrService {
@Autowired
private SolrTemplate solrTemplate;
@Autowired
private Query query;
public List<HotelVo> findHotels(HotelVo vo) {
//面向对象的查询语句
Criteria criteria=new Criteria("id");//.and("hotelName").contains("杭州");
criteria=criteria.and("hotelName").contains("杭州");//"查询内容中是否包涵";
// criteria=criteria.and("address").contains("上城区");
// criteria=criteria.and("details").contains("西湖");
criteria= criteria.and("id").greaterThan(53);
Sort sort=new Sort(Sort.Direction.DESC,"id");
// query.setOffset(0);
// query.setRows(63);
query.addSort(sort);
query.addCriteria(criteria);
ScoredPage<HotelVo> scoredPage= solrTemplate.queryForPage(query,HotelVo.class);
List<HotelVo> list= scoredPage.getContent();
return list;
}
}