前言
solr是apach基于Lucene开发的成熟的框架,这里我们学习如何部署、使用。关于集群会在后面继续添加
linux部署
mkdir /usr/local/solr && cd /usr/local/solr
# 把tomcat、solr和IK分词器复制到当前目录
wget http://archive.apache.org/dist/lucene/solr/4.10.3/solr-4.10.3.tgz
wget http://124.202.164.13/files/208300000CA11C0C/mirror.bit.edu.cn/apache/tomcat/tomcat-7/v7.0.91/bin/apache-tomcat-7.0.91.tar.gz
wget IK……
# 处理tomcat
tar -zxvf apache-tomcat-7.0.91.tar.gz
mv apache-tomcat-7.0.91 tomcat
# 处理solr
tar -zxvf solr-4.10.3.tar
# 处理解析器
unzip IKAnalyzer2012FF_hf1.zip -d ./ik
# 复制solr.war包到tomcat
cp solr-4.10.3/example/webapps/solr.war tomcat/webapps
/usr/local/solr/tomcat/bin/startup.sh
/usr/local/solr/tomcat/bin/shutdown.sh
rm tomcat/webapps/solr.war
# 复制项目需要的jar包
cp solr-4.10.3/example/lib/ext/* tomcat/webapps/solr/WEB-INF/lib
# 复制出solr实例
cp -r solr-4.10.3/example/solr ./
mv solr solrhome
# 配置
vim tomcat/webapps/solr/WEB-INF/web.xml
# 取消注释节点:env-entry,并修改value为:/usr/local/solr/solrhome
增加一个实例
cp -r solrhome/collection1 solrhome/collection2
vim solrhome/collection2/core.properties
# 修改为collection2即可
配置中文分词器
cp ik/IKAnalyzer2012FF_u1.jar tomcat/webapps/solr/WEB-INF/lib
mkdir tomcat/webapps/solr/WEB-INF/classes
cp ik/IKAnalyzer.cfg.xml tomcat/webapps/solr/WEB-INF/classes/
cp ik/stopword.dic tomcat/webapps/solr/WEB-INF/classes/
# 自定义扩展词典
touch tomcat/webapps/solr/WEB-INF/classes/ext.dic
vim tomcat/webapps/solr/WEB-INF/classes/IKAnalyzer.cfg.xml
# 取消自定义扩展词典的注释
# 配置一个使用分词器的域
vim solrhome/collection1/conf/schema.xml
<!-- IKAnalyzer-->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
<!--IKAnalyzer Field-->
<field name="content_ik" type="text_ik" indexed="true" stored="true" />
导入插件集成
mkdir solrhome/collection1/lib
cp solr-4.10.3/dist/solr-dataimporthandler-4.10.3.jar solrhome/collection1/lib/
cp solr-4.10.3/dist/solr-dataimporthandler-extras-4.10.3.jar solrhome/collection1/lib/
# 下载mysql的jar包到lib目录
wget -P solrhome/collection1/lib/ https://raw.githubusercontent.com/tianyunperfect/jar_back/master/mysql-connector-java-5.1.32.jar
vim 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>
# 配置插件的配置文件
touch solrhome/collection1/conf/data-config.xml
# 添加
<?xml version="1.0" encoding="UTF-8" ?>
<dataConfig>
<dataSource type="JdbcDataSource"
driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://192.168.148.92:3306/lucene"
user="root"
password="root"/>
<document>
<entity name="product" query="SELECT pid,name,catalog_name,price,description,picture FROM products ">
<field column="pid" name="id"/>
<field column="name" name="product_name"/>
<field column="catalog_name" name="product_catalog_name"/>
<field column="price" name="product_price"/>
<field column="description" name="product_description"/>
<field column="picture" name="product_picture"/>
</entity>
</document>
</dataConfig>
mysql -u root -p
use mysql;
grant all privileges on *.* to 'root'@'%' identified by 'root' with grant option;
flush privileges;
使用
配置
applicationContext-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:solr="http://www.springframework.org/schema/data/solr"
xsi:schemaLocation="http://www.springframework.org/schema/data/solr
http://www.springframework.org/schema/data/solr/spring-solr-1.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- solr服务器地址 -->
<solr:solr-server id="solrServer" url="http://192.168.63.128:8080/solr" />
<!-- solr模板,使用solr模板可对索引库进行CRUD的操作 -->
<bean id="solrTemplate" class="org.springframework.data.solr.core.SolrTemplate">
<constructor-arg ref="solrServer" />
</bean>
</beans>
使用
@Autowired
private SolrTemplate solrTemplate;
solrTemplate.saveBeans(items);
solrTemplate.commit();