前情:
采用伪集群部署方式,在同一台服务器上部署3个zookeeper,3个solr服务。结构如下图:
1,下载压缩包,zookeeper,solr
zookeeper3.4.10下载地址:https://pan.baidu.com/s/12H-BlBTCJKaPVLdlE2rqbQ
solr-6.5.1 下载地址:https://pan.baidu.com/s/1jVS9tVmJgIwgzHoPr9mhNw
2,部署Zookeeper集群(部署一个,Copy多个)
详情请见: 我的另一篇文章《Zookeeper集群搭建》
3,solr部署
solr-6.5.1.tgz解压命名为solr-1
4,solr关联zookeeper
配置solr和zookeeper,在solr-1/bin 目录下 ,solr.in.sh文件中,加入如下参数,并复制solr-1,粘贴为solr-2,solr-3。
ZK_HOST="namenode:clientPort1, namenode:clientPort2, namenode:clientPort3"
其中 namenode为服务器节点IP,clientPort为zoo.cfg中的clientPort
5,更改solr-2,solr-3的启动端口
因为是在同一台机器上部署,所以要更改各个solr节点的默认启动端口。以solr-1为例,默认是8983,我改为8985.
/srv/new_solr/solrCloud/solr-1/bin 目录下的 solr.in.sh 文件
SOLR_PORT=8985
修改 /srv/new_solr/solrCloud/solr-1/server/etc 目录下的 jetty-https.xml文件
<Set name="port"><Property name="solr.jetty.https.port" default="8985" /></Set>
执行启动命令
/srv/new_solr/solrCloud/solr-1/bin/./solr start
solr-2,solr-3也是一样
启动成功后,进入一个节点,我的是 http://10.1.88.101:8985/solr/#/ 左侧能看到Cloud菜单,即是Cloud模式
6,创建collection的配置文件并上传。
我的配置文件是videoconf,这个可以从/srv/new_solr/solrCloud/solr-1/server/solr/configsets/ 下复制sample_techproducts_configs 文件夹到solr-1的根目录,命名为videoconf,作为要创建的collection的配置文件名。
进入/srv/new_solr/solrCloud/solr-1/server/scripts/cloud-scripts 目录,利用./zkCli.sh 上传solr的配置文件到zookeeper,如下命令,videoconf是要上传的配置文件名,video_model是上传后的显示名。
cd /srv/new_solr/solrCloud/solr-1/server/scripts/cloud-scripts/
./zkcli.sh -zkhost namenode:clientPort1, namenode:clientPort2, namenode:clientPort3 -cmd upconfig -confdir /srv/new_solr/solrCloud/solr-1/videoconf/ -confname video_model
7,创建collections
上传成功后,在solr 后台创建collection时可以看到之前命名的配置文件video_model。
创建时参数解析
name:将被创建的集合的名字
numShards:集合创建时需要创建逻辑碎片的个数
replicationFactor:分片的副本数。replicationFactor(复制因子)为 3 意思是每个逻辑碎片将有 3 份副本。
maxShardsPerNode:默认值为1,每个Solr服务器节点上最大分片数(4.2新增的)
注意三个数值:numShards、replicationFactor、liveSolrNode(当前存活的solr节点),一个正常的solrCloud集群不容许同一个liveSolrNode上部署同一个shard的多个replic,因此当maxShardsPerNode=1时,numShardsreplicationFactor>liveSolrNode时,报错。因此正确时因满足以下条件:numShardsreplicationFactor<liveSolrNode*maxShardsPerNode
如有错误collections,执行删除语句
http://10.1.88.101:8985/solr/admin/collections?action=DELETE&name=video_model
8,分词器配置,我选的是smart分词,这个lucene-analyzers-smartcn-6.5.1.jar包是在/srv/new_solr/solrCloud/solr-1/contrib/analysis-extras/lib下,
所以修改solrconfig.xml,添加分词器jar包位置
这是我改好的solrconfig.xml
https://pan.baidu.com/s/1qb56AlN_QzoiJn2ySBGqXg
可以下载下来用,也可自行修改
<lib dir="${solr.install.dir:../../../..}/contrib/analysis-extras/lucene-libs" regex=".*\.jar" />
修改manged-schema.xml,增加一个新的fileType 类型,并指定分词类型是smart
我的 manged-schema.xml
https://pan.baidu.com/s/1z5A2VdAwgho2KbxQTb81mA
可以下载下来参考,也可自行修改
<fieldType name="text_smart" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
</analyzer>
<analyzer type="query">
<filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
</analyzer>
</fieldType>
创建一个新的field 命名为 file_text_search_all ,并将需要分词的字段copy进来。
<field name="videoModelCourseId_l" type="long" indexed="true" stored="true" multiValued="false"/>
<field name="videoModelCourseName_s" type="string" indexed="true" stored="true" multiValued="false"/>
<field name="videoModelTeacherName_s" type="string" indexed="true" stored="true" multiValued="false"/>
<field name="videoModelCoursePrice_s" type="string" indexed="true" stored="true" multiValued="false"/>
<field name="videoModelModeltype_s" type="string" indexed="true" stored="true" multiValued="false"/>
<field name="videoModelOrdernum_i" type="int" indexed="true" stored="true" multiValued="false"/>
<field name="videoModelVideoTime_i" type="int" indexed="true" stored="true" multiValued="false"/>
<field name="videoModelCreateTime_s" type="string" indexed="true" stored="true" multiValued="false"/>
<field name="videoModelActivityNum_i" type="int" indexed="true" stored="true" multiValued="false"/>
<field name="file_text_search_all" type="text_smart" indexed="true" stored="true" multiValued="true" />
<!-- 为多个field指定一个共同的别名,通过这个别名进行query就能够同时对所有field进行搜索 -->
<copyField source="videoModelCourseName_s" dest="file_text_search_all"/>
9,更新配置文件,每次修改manage-schema,都需要执行上传操作,即下述命令
cd /srv/new_solr/solrCloud/solr-1/server/scripts/cloud-scripts/
./zkcli.sh -zkhost namenode:clientPort1, namenode:clientPort2, namenode:clientPort3 -cmd upconfig -confdir /srv/new_solr/solrCloud/solr-1/videoconf/ -confname video_model
10,查看分词示例
11,配置停词表,stopwords.txt
12,solrj的使用
连接SolrCloud,创建CloudSolrClient
13,批量创建时提高提交速度,设置软提交,硬提交
<autoCommit>
<maxTime>${solr.autoCommit.maxTime:15000}</maxTime>
<maxDocs>100000</maxDocs>
<openSearcher>false</openSearcher>
</autoCommit>
<autoSoftCommit>
<maxTime>${solr.autoSoftCommit.maxTime:15000}</maxTime>
<maxDocs>10000</maxDocs>
</autoSoftCommit>