1. 备份单Core的solr环境
[root@demoserver solr]# ll
total 169632
drwxr-xr-x. 4 root root 4096 Jan 29 14:17 master
drwxr-xr-x. 4 root root 4096 Feb 16 15:08 slave
drwxr-xr-x. 4 root root 4096 Feb 16 16:18 slave_2
[root@demoserver solr]# tar -zcvf master_slave.tar.gz .
[root@demoserver solr]# ll
total 169632
drwxr-xr-x. 4 root root 4096 Jan 29 14:17 master
-rw-r--r--. 1 root root 173689613 Feb 16 17:11 master_slave.tar.gz
drwxr-xr-x. 4 root root 4096 Feb 16 15:08 slave
drwxr-xr-x. 4 root root 4096 Feb 16 16:18 slave_2
2、 搭建MutiCore的solr环境;
第一步: Solr Multicore solr 1.3 的新特性
Solr Multicore 是 solr 1.3 的新特性。其目的一个solr实例,可以有多个搜索应用。
我们知道你既可以把不同类型的数据放到同一index中,也可以使用分开的多indexes。基于这一点,你只需知道如何使用多
indexes(实际上就是运行Solr的多实例)。尽管如此,为每一个类型添加一个完整的Solr实例会显得太臃肿庞大。
Solr1.3引入了Solr core的概念,该方案使用一个Solr实例管理多个indexes,这样就有热点core(hot core)的重读(reloading)
与交换(swap,通常是读index与写index交换),那么管理一个 core或index也容易些。每个Solr core由它自己的配置文件和索引数据组成。在多core执行搜索和索引几乎
和没有使用core一样。你只是添加core的名字为各自不同的URL。
单core情况下的如下搜索:
http://localhost:8983/solr/select?q=dave%20matthews
在多core环境下,你可以通过如下方式访问一个名为mbartists的core:
http://localhost:8983/solr/core0/select?q=dave%20matthews
并非在URL中引入core name的参数名值对,而是用不同的context。这样就可以像在单core中执行你的管理任务,搜索,更新操作。
第二步:MultiCore的配置方法
1、找到solr下载包中的example文件夹,在它的下面有个multicore文件夹,将这个文件夹下面的core0、core1和solr.xml拷贝到
/home/solr/master/solr4home下面,对应的centos的文件如下操作:
[root@demoserver solr4home]# ll
total 32
drwxr-xr-x. 2 root root 4096 Jan 29 14:07 bin
drwxr-xr-x. 4 root root 4096 Feb 1 01:30 collection1
drwxr-xr-x. 4 root root 4096 Feb 16 18:13 core0
drwxr-xr-x. 4 root root 4096 Feb 16 18:13 core1
-rw-r--r--. 1 root root 2473 Jan 29 14:07 README.txt
-rw-r--r--. 1 root root 1334 Feb 16 17:48 solr.xml
-rw-r--r--. 1 root root 1715 Feb 16 17:30 solr.xml.20140216
-rw-r--r--. 1 root root 501 Jan 29 14:07 zoo.cfg
注意:有一个 solr.xml(这只是默认文件,当然也可以指定别的文件),如:
<?xml version="1.0" encoding="UTF-8" ?>
这个文件是告诉solr应该加载哪些core,
schema.xml与solrconfig.xml,可以把实际应用的/solr/conf/schema.xml复制过来(注意:solrconfig.xml不要复制)。
2、启动tomcat,访问应用,就可以看到有 Admin core0 和 Admin core1
http://192.168.17.128:9080/solr/#/
http://192.168.17.128:9080/solr/\#/core1
http://192.168.17.128:9080/solr/core0/select?q=\*%3A\*&wt=json&indent=true
3、采用上面的默认solr.xml,索引文件将存放在同一个目录下面,在这里将存放在C:\solr-tomcat\solr\data,如果你想更改目录,或者两个应用存放在不同的目录,请参见下面的xml。
4、为何使用多core ?
Solr实例支持多core比启用多index要好(do more)。多core同时解决了在生产环境下的一些关键需求:
1、 重建索引;
2、 测试配置变更;
3、 合并索引;
4、 运行时重命名core;
为何多core不是默认的?
多core是1.3版本中才加的,1.4后更成熟。我们强烈建议你使用多core,既是你现在的solr.xml只配置了一个core,虽然会比单个索引稍复杂,
但可以带来管理core上的好处。或许一天单个core可能最终RELOAD and STATUS命令,又或许单个core最终会被废禁。多个core会是Solr将来支持
大规模分布式索引的关键。因此,以后可以期待更多。
5. 配置IKAnalyzer分词器
<?xml version="1.0" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<schema name="example core zero" version="1.1">
<types>
<fieldtype name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
<!-- added by shenfl at 2014/2/16 date -->
<fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/>
<!-- added by shenfl at 2014/2/16 IKAnalyzer -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
</types>
<fields>
<!-- general -->
<field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
<field name="type" type="string" indexed="true" stored="true" multiValued="false" />
<field name="name" type="string" indexed="true" stored="true" multiValued="false" />
<field name="core0" type="string" indexed="true" stored="true" multiValued="false" />
<field name="_version_" type="long" indexed="true" stored="true"/>
<!-- added by at 2014/2/16 self define field at shenfl -->
<field name="wareCode" type="string" indexed="true" stored="true"/>
<field name="wareName" type="text_ik" indexed="true" stored="true"/>
<field name="wareStatus" type="string" indexed="true" stored="true"/>
<field name="openDate" type="date" indexed="true" stored="true"/>
<field name="shopPrice" type="string" indexed="true" stored="true"/>
<field name="marketPrice" type="string" indexed="true" stored="true"/>
<field name="deliverType" type="string" indexed="true" stored="true"/>
<field name="wareIntegralValue" type="long" indexed="true" stored="true"/>
<field name="gCurValue" type="long" indexed="true" stored="true"/>
<field name="mCurValue" type="long" indexed="true" stored="true"/>
<field name="eCurValue" type="long" indexed="true" stored="true"/>
<field name="isHtmlChannel" type="string" indexed="true" stored="true"/>
<field name="isWapChannel" type="string" indexed="true" stored="true"/>
<field name="isSearch" type="string" indexed="true" stored="true"/>
<field name="sKind" type="long" indexed="true" stored="true"/>
<field name="sName" type="string" indexed="true" stored="true"/>
<field name="mKind" type="long" indexed="true" stored="true"/>
<field name="mName" type="string" indexed="true" stored="true"/>
</fields>
<!-- field to use to determine and enforce document uniqueness. -->
<uniqueKey>id</uniqueKey>
<!-- field for the QueryParser to use when an explicit fieldname is absent -->
<defaultSearchField>name</defaultSearchField>
<!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->
<solrQueryParser defaultOperator="OR"/>
</schema>
4. 多核心方式主从索引环境搭建
多核心主从索引的复制方式同单核心索引复制方式一致。
slave中的core0、core1分别对应master中的core0、core1,具体配置步骤:
Master服务器搭建
第一步:创建Master存放目录
[solr@demoserver solr4home]$ ll
total 36
drwxr-xr-x. 2 solr solr 4096 Jan 29 14:07 bin
drwxr-xr-x. 4 solr solr 4096 Feb 1 01:30 collection1
drwxr-xr-x. 4 solr solr 4096 Feb 16 18:13 core0
drwxr-xr-x. 4 solr solr 4096 Feb 16 18:13 core1
drwxr-xr-x. 3 solr solr 4096 Feb 16 19:01 data
-rw-r--r--. 1 solr solr 2473 Jan 29 14:07 README.txt
-rw-r--r--. 1 solr solr 1334 Feb 21 13:35 solr.xml
-rw-r--r--. 1 solr solr 1715 Feb 16 17:30 solr.xml.20140216
-rw-r--r--. 1 solr solr 501 Jan 29 14:07 zoo.cfg
第二步:编辑solr.xml文件,添加多个core的配置
[solr@demoserver solr4home]$ cat solr.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
All (relative) paths are relative to the installation path
persistent: Save changes made via the API to this file
sharedLib: path to a lib directory that will be shared across all cores
-->
<solr persistent="false">
<!--
adminPath: RequestHandler path to manage cores.
If 'null' (or absent), cores will not be manageable via request handler
-->
<cores adminPath="/admin/cores">
<core name="core0" instanceDir="core0" />
<core name="core1" instanceDir="core1" />
</cores>
</solr>
第三步:创建配置文件,也可以复制
[solr@demoserver conf]$ pwd
/home/solr/master/solr4home/core0/conf
[solr@demoserver conf]$ ll
total 12
-rw-r--r--. 1 solr solr 3646 Feb 25 14:10 schema.xml
-rw-r--r--. 1 solr solr 5116 Feb 25 14:00 solrconfig.xm
第四步: schema.xml配置文件
<?xml version="1.0" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<schema name="example core zero" version="1.1">
<types>
<fieldtype name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
<!-- added by shenfl at 2014/2/16 date -->
<fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/>
<!-- added by shenfl at 2014/2/16 IKAnalyzer -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
</types>
<fields>
<!-- general -->
<field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
<field name="type" type="string" indexed="true" stored="true" multiValued="false" />
<field name="name" type="string" indexed="true" stored="true" multiValued="false" />
<field name="core0" type="string" indexed="true" stored="true" multiValued="false" />
<field name="_version_" type="long" indexed="true" stored="true"/>
<!-- added by at 2014/2/16 self define field at shenfl -->
<field name="wareCode" type="string" indexed="true" stored="true"/>
<field name="wareName" type="text_ik" indexed="true" stored="true"/>
<field name="wareStatus" type="string" indexed="true" stored="true"/>
<field name="openDate" type="date" indexed="true" stored="true"/>
<field name="shopPrice" type="string" indexed="true" stored="true"/>
<field name="marketPrice" type="string" indexed="true" stored="true"/>
<field name="deliverType" type="string" indexed="true" stored="true"/>
<field name="wareIntegralValue" type="long" indexed="true" stored="true"/>
<field name="gCurValue" type="long" indexed="true" stored="true"/>
<field name="mCurValue" type="long" indexed="true" stored="true"/>
<field name="eCurValue" type="long" indexed="true" stored="true"/>
<field name="isHtmlChannel" type="string" indexed="true" stored="true"/>
<field name="isWapChannel" type="string" indexed="true" stored="true"/>
<field name="isSearch" type="string" indexed="true" stored="true"/>
<field name="sKind" type="long" indexed="true" stored="true"/>
<field name="sName" type="string" indexed="true" stored="true"/>
<field name="mKind" type="long" indexed="true" stored="true"/>
<field name="mName" type="string" indexed="true" stored="true"/>
</fields>
<!-- field to use to determine and enforce document uniqueness. -->
<uniqueKey>id</uniqueKey>
<!-- field for the QueryParser to use when an explicit fieldname is absent -->
<defaultSearchField>name</defaultSearchField>
<!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->
<solrQueryParser defaultOperator="OR"/>
</schema>
第五步: solrconfig.xml配置文件
[solr@demoserver conf]$ cat solrconfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
This is a stripped down config file used for a simple example...
It is *not* a good example to work from.
-->
<config>
<luceneMatchVersion>4.4</luceneMatchVersion>
<directoryFactory name="DirectoryFactory" class="${solr.directoryFactory:solr.StandardDirectoryFactory}"/>
<dataDir>${solr.core0.data.dir:}</dataDir>
<schemaFactory class="ClassicIndexSchemaFactory"/>
<updateHandler class="solr.DirectUpdateHandler2">
<updateLog>
<str name="dir">${solr.core0.data.dir:}</str>
</updateLog>
</updateHandler>
<requestHandler name="/get" class="solr.RealTimeGetHandler">
<lst name="defaults">
<str name="omitHeader">true</str>
</lst>
</requestHandler>
<requestHandler name="/replication" class="solr.ReplicationHandler" startup="lazy" >
<lst name="master">
<!-- 当有新的索引被提交,标记新的索引和保存新索引的文件名 -->
<str name="replicateAfter">commit</str>
<!-- 当系统启动时,标记新的索引和保存新索引的文件名 -->
<str name="replicateAfter">startup</str>
<!-- 当索引优化时,标记新的索引和保存新索引的文件名,因为 commit,optimize 基本上是同时存在的,所以一般不用设置 optimize-->
<!-- <str name="replicateAfter">optimize</str>-->
<!-- 索引优化后创建一份备份索引,这个在复制过程中不需要设置 -->
<!--<str name="backupAfter">optimize</str>-->
<!-- 索引复制时,也同步从服务器的配置文件 solrconfig_slave.xml:solrconfig.xml 表示复制时,在slave端以solrconfig.xml进行保存,这样slave才能认识这个文件-->
<str name="confFiles">solrconfig_slave.xml:solrconfig.xml,schema.xml,stopwords.txt,elevate.xml</str>
<!-- 设置验证信息 -->
<str name="httpBasicAuthUser">admin</str>
<str name="httpBasicAuthPassword">123456</str>
</lst>
</requestHandler>
<requestDispatcher handleSelect="true" >
<requestParsers enableRemoteStreaming="false" multipartUploadLimitInKB="2048" formdataUploadLimitInKB="2048" />
</requestDispatcher>
<requestHandler name="standard" class="solr.StandardRequestHandler" default="true" />
<requestHandler name="/analysis/field" startup="lazy" class="solr.FieldAnalysisRequestHandler" />
<requestHandler name="/update" class="solr.UpdateRequestHandler" />
<requestHandler name="/admin/" class="org.apache.solr.handler.admin.AdminHandlers" />
<requestHandler name="/admin/ping" class="solr.PingRequestHandler">
<lst name="invariants">
<str name="q">solrpingquery</str>
</lst>
<lst name="defaults">
<str name="echoParams">all</str>
</lst>
</requestHandler>
<!-- config for the admin interface -->
<admin>
<defaultQuery>solr</defaultQuery>
</admin>
</config>
Slave服务器搭建
第一步:创建slave存放目录
[solr@demoserver solr4home]$ pwd
/home/solr/slave/solr4home
[solr@demoserver solr4home]$ ll
total 36
drwxr-xr-x. 2 solr solr 4096 Feb 24 20:58 bin
drwxr-xr-x. 4 solr solr 4096 Feb 24 20:58 collection1
drwxr-xr-x. 4 solr solr 4096 Feb 24 20:58 core0
drwxr-xr-x. 4 solr solr 4096 Feb 24 20:58 core1
drwxr-xr-x. 3 solr solr 4096 Feb 24 20:58 data
-rw-r--r--. 1 solr solr 2473 Feb 24 20:58 README.txt
-rw-r--r--. 1 solr solr 1334 Feb 24 20:58 solr.xml
-rw-r--r--. 1 solr solr 1715 Feb 24 20:58 solr.xml.20140216
-rw-r--r--. 1 solr solr 501 Feb 24 20:58 zoo.cfg
第二步:编辑solr.xml文件,添加多个core的配置
[solr@demoserver solr4home]$ cat solr.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!--
All (relative) paths are relative to the installation path
persistent: Save changes made via the API to this file
sharedLib: path to a lib directory that will be shared across all cores
-->
<solr persistent="false">
<!--
adminPath: RequestHandler path to manage cores.
If 'null' (or absent), cores will not be manageable via request handler
-->
<cores adminPath="/admin/cores">
<core name="core0" instanceDir="core0" />
<core name="core1" instanceDir="core1" />
</cores>
</solr>
第三步:创建配置文件,也可以复制
[solr@demoserver conf]$ pwd
/home/solr/slave/solr4home/core0/conf
[solr@demoserver conf]$ ll
total 12
-rw-r--r--. 1 solr solr 3646 Feb 25 14:10 schema.xml
-rw-r--r--. 1 solr solr 5116 Feb 25 14:00 solrconfig.xm
第四步: schema.xml配置文件
<?xml version="1.0" ?>
<schema name="example core zero" version="1.1">
<types>
<fieldtype name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>
<!-- added by shenfl at 2014/2/16 date -->
<fieldType name="date" class="solr.TrieDateField" precisionStep="0" positionIncrementGap="0"/>
<!-- added by shenfl at 2014/2/16 IKAnalyzer -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>
</types>
<fields>
<!-- general -->
<field name="id" type="string" indexed="true" stored="true" multiValued="false" required="true"/>
<field name="type" type="string" indexed="true" stored="true" multiValued="false" />
<field name="name" type="string" indexed="true" stored="true" multiValued="false" />
<field name="core0" type="string" indexed="true" stored="true" multiValued="false" />
<field name="_version_" type="long" indexed="true" stored="true"/>
<!-- added by at 2014/2/16 self define field at shenfl -->
<field name="wareCode" type="string" indexed="true" stored="true"/>
<field name="wareName" type="text_ik" indexed="true" stored="true"/>
<field name="wareStatus" type="string" indexed="true" stored="true"/>
<field name="openDate" type="date" indexed="true" stored="true"/>
<field name="shopPrice" type="string" indexed="true" stored="true"/>
<field name="marketPrice" type="string" indexed="true" stored="true"/>
<field name="deliverType" type="string" indexed="true" stored="true"/>
<field name="wareIntegralValue" type="long" indexed="true" stored="true"/>
<field name="gCurValue" type="long" indexed="true" stored="true"/>
<field name="mCurValue" type="long" indexed="true" stored="true"/>
<field name="eCurValue" type="long" indexed="true" stored="true"/>
<field name="isHtmlChannel" type="string" indexed="true" stored="true"/>
<field name="isWapChannel" type="string" indexed="true" stored="true"/>
<field name="isSearch" type="string" indexed="true" stored="true"/>
<field name="sKind" type="long" indexed="true" stored="true"/>
<field name="sName" type="string" indexed="true" stored="true"/>
<field name="mKind" type="long" indexed="true" stored="true"/>
<field name="mName" type="string" indexed="true" stored="true"/>
</fields>
<!-- field to use to determine and enforce document uniqueness. -->
<uniqueKey>id</uniqueKey>
<!-- field for the QueryParser to use when an explicit fieldname is absent -->
<defaultSearchField>name</defaultSearchField>
<!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->
<solrQueryParser defaultOperator="OR"/>
</schema>
第五步: solrconfig.xml配置文件
<requestHandler name="/replication" class="solr.ReplicationHandler" startup="lazy" >
<lst name="slave">
<!-- 主服务器的URL,core_1是配置了多核,如果不是多核,连接应该是 http://www.abc.com/solr/replication -->
<!-- <str name="masterUrl">http://www.abc.com/solr/core_1/replication</str> -->
<str name="masterUrl">http://192.168.17.128:9080/solr/core0/replication</str>
<!-- 1分钟同步一次 -->
<str name="pollInterval">00:00:60</str>
<!-- 压缩机制,来传输索引,可选 internal|external,internal表示内网,external表示外网 -->
<str name="compression">internal</str>
<!-- 设置超时时间 -->
<str name="httpConnTimeout">5000</str>
<str name="httpReadTimeout">10000</str>
<!-- 设置验证信息,要和master 服务器配置一样 -->
<str name="httpBasicAuthUser">admin</str>
<str name="httpBasicAuthPassword">123456</str>
</lst>
</requestHandler>
以上重点介绍了Master、Slave架构中core0的配置操作步骤,其中Master、Slave架构中core1的配置和core0一样。