自己對solr也是新手,所以很多東西也不夠瞭解,這裡只是記錄一下自己的學習過程。
主要參考資料:
- http://wiki.apache.org/solr/Solr.xml%204.4%20and%20beyond
- http://wiki.apache.org/solr/Core%20Discovery%20%284.4%20and%20beyond%29
- http://wiki.apache.org/solr/CoreAdmin
本文主要記述SOLR4.4版本與舊版本在multicore配置方面的不同之處,所以引用了很多官方文檔的內容,由於自己也是剛接觸solr而且水平也低,因此可能介紹地不明不白,如果有疑問請移步到上面列出的官方wiki文檔。
有關solr multicore的簡介
關於solr multicore的介紹請參照官方的相關wiki頁面:
http://wiki.apache.org/solr/CoreAdmin
該wiki上有這樣幾句話來介紹solr multicore:
In solr, the "multi core" concept refers to the ability to have multiple cores running in the same webapp.
Multiple cores let you have a single Solr instance with separate configurations and indexes, with their own config and schema for very different applications, but still have the convenience of unified
administration. Individual indexes are still fairly isolated, but you can manage them as a single application, create new indexes on the fly by spinning up new SolrCores, and even make one SolrCore replace another SolrCore without ever restarting your Servlet
Container.
自己的英文不好,就不試著翻譯了,想必都能看出來大體意思。multicore是在solr1.3中加入的,其目的就是運行一個solr執行個體,可以有多個搜尋應用(這是別人的原話,引用一下)。
solr4.4中的multicore
在solr4.4之前的版本中,multicore都是在%SOLR_HOME%/solr.xml中配置的,雖然我沒有使用過較舊版本的solr,但這從網上一些教程上可以看得出來。比如這裡:
- http://blog.chenlb.com/2009/01/try-solr-multicore.html
- http://blog.csdn.net/jiushuai/article/details/8080758
大體配置如下所示:
<cores adminPath="/admin/cores"> <core name="core0" instanceDir="core0" /> <core name="core1" instanceDir="core1" /></cores>
但solr4.4版本中的multicore相對於以前版本改變是非常大的,這些改變可以從以下兩個頁面看到:
- http://wiki.apache.org/solr/Solr.xml%204.4%20and%20beyond
- http://wiki.apache.org/solr/Core%20Discovery%20%284.4%20and%20beyond%29
從上面的頁面中可以看出,從solr4.4開始,%SOLR_HOME%下的solr.xml結構發生變化,這些變化在5.0中將會強制執行。下面是官方wiki的介紹:
Optionally as of Solr4.4 and mandatory for Solr5.0,
the structure of the solr.xml file has changed. In a nutshell, <cores> and <core> have been replaced by auto-discovering cores. Whether to use old or new-style core definitions is determined by whether the <cores> tag is present in solr.xml. In 5.0, presence
of the <cores> tag will generate an error on startup.
An optional property coreRootDirectory can cause the discovery process to start at an arbitrary directory other than SOLR_HOME.
We'll distribute a new-style solr.xml as the default in the example directory with Solr4.4
The sharedLib attribute on the top-level <solr> element is replaced by a child str element (see below). In theory, the old mechanism should continue to support sharedLib,
but SOLR-4791 documents the fact that it doesn't work in 4.3.0.
上面幾段話的大體意思是從Solr4.4開始在solr.xml檔案中取消了<cores/>節點,用自動探測cores來代替,這個改變在Solr4.4時是可選的,而從Solr5.0開始強制採用。
SOLR4.4中multicore的自動檢測
那麼SOLR是如何自動探測cores的呢?wiki同樣有相關的描述:
Core discovery process
Exploration of the core tree terminates when a file named core.properties is encountered. Discovery of a file of that name is assumed to define the root of a core. There is no a-priori limit
on the depth of the tree. That is, the directories under the core root are explored until a core.properties file is encountered, and that directory is assumed to be the instanceDir for that core. Subdirectories of any directory that has a core.properties file
are NOT examined for additional cores.
從上面的介紹中可以看到,探測將從core root開始(core root可以在solr.xml檔案中以coreRootDirectory屬性定義),不限制深度地遞迴遍曆其所有子目錄,直到一個名為core.properties的設定檔被發現。含有該core.properties檔案的目錄將被當作一個core的instanceDir目錄,並且該目錄下的子目錄即使再含有core.properties檔案也不會被當作另外core的instanceDir。
那麼core.properties檔案都可配置哪些資訊呢?如下所示:
- name - the name of the core. If not specified, the name comes from the containing directory.
- config - the configuration file. Defaults to solrconfig.xml
- dataDir - the directory where the index, tlog, etc. are stored. Again, since this is discovery-based, omit this unless you have special needs.
- ulogDir - where the transaction log resides. It may be advantageous to put the transaction lot on a different disk than the index.
- schema - the schema file. Defaults to schema.xml
- shard - the shard ID.
- collection - the collection to which this core belongs
roles - SolrCloud role definition
- properties - properties file to override core definitions. TBD: This is probably obsolete since we're reading a properties file in the first place. Is there a use case for supporting this now?
loadOnStartup - [true|false] this core should be loaded and a searcher opened when Solr starts.
transient - [true|false] this core may be unloaded if the core cache exceeds transientCacheSize (defined in solr.propreties)
coreNodeName - SolrCloud core node name
所有的配置項都有相應的解釋,也很簡單,這裡就不作過多解釋。
- 注1:如果core.properties檔案是空的,也就是說裡面沒有任何文字內容,那麼這個core的名字將與core.properties檔案所在的目錄名相同。比如一個空的core.properties檔案出現在目錄/solr/home/core1中, 則core的名字預設為 "core1",instanceDir 就是 /solr/home/core1,dataDir 就是 /solr/home/core1/data目錄等。
- 注2:很容易想像地到,如果要臨時忽略掉一個core,那麼最簡單的方式就是將該core對應的instanceDir目錄中的core.properites檔案重新命名,比如core.properties.bak。
- 注3:上面提到在SOLR4.4版本solr.xml中的cores節點是可選的,也就是可有可無。是否含有cores節點將作為該solr.xml被按照新樣式還是舊樣式解析的依據,如果我們既在solr.xml檔案中配置的了cores節點又在instanceDir目錄中放置了core.properties檔案,那麼solr.xml檔案中的配置優先。
執行個體配置
前面的內容都是空泛的理論,下面記一下自己的配置。
我的SOLR_HOME定義在/opt/tomcat-6.0.37/webapps/solr/solr_home目錄,目錄結構如下:
其中我的solr.xml檔案中沒有定義<cores/>節點,那麼該solr.xml將被當作新樣式的設定檔解析,也就是說SOLR將自動探測cores的定義。另外該目錄下的collection1和core1子目錄均含有core.properties設定檔,其中core1目錄下的core.properties檔案是空的,collection1目錄下的core.properties檔案只有一行內容:
name=core0
值得注意的是collection1和core1兩個目錄結構是相似的,不同的僅是core.properties檔案和各自的schema.xml設定檔,其目錄結構大體如下所示:
那麼,根據上面的介紹,collection1和core1目錄都會被當作core的instanceDir,兩個core的名字分別為core0和core1。
啟動Tomcat,瀏覽器訪問的時候就可以看到在core挑選清單上有兩個core,分別為core0和core1,貼張圖片驗證一下: