1. solrconfig.xml
solrconfig.xml配置文件主要定義了SOLR的一些處理規則,包括索引數據的存放位置,更新,刪除,查詢的一些規則配置。
1.1. datadir節點
<dataDir>${solr.data.dir:d:/Server/Solr/data}</dataDir> 定義了索引數據和日志文件的存放位置
1.2. luceneMatchVersion
<luceneMatchVersion>4.10.1</luceneMatchVersion> 表示solr底層使用的是lucene4.8
1.3. lib
<lib dir="../../../contrib/extraction/lib"regex=".*\.jar"/> 表示solr引用包的位置,當dir對應的目錄不存在時候,會忽略此屬性
1.4. directoryFactory
索引存儲方案,共有以下存儲方案
1、 solr.StandardDirectoryFactory,這是一個基於文件系統存儲目錄的工廠,它會試圖選擇最好的實現基於你當前的操作系統和Java虛擬機版本。
2、 solr.SimpleFSDirectoryFactory,適用於小型應用程序,不支持大數據和多線程。
3、 solr.NIOFSDirectoryFactory,適用於多線程環境,但是不適用在windows平台(很慢),是因為JVM還存在bug。
4、 solr.MMapDirectoryFactory,這個是solr3.1到4.0版本在linux64位系統下默認的實現。它是通過使用虛擬內存和內核特性調用mmap去訪問存儲在磁盤中的索引文件。它允許lucene或solr直接訪問I/O緩存。如果不需要近實時搜索功能,使用此工廠是個不錯的方案。
5、 solr.NRTCachingDirectoryFactory,此工廠設計目的是存儲部分索引在內存中,從而加快了近實時搜索的速度。
6、 solr.RAMDirectoryFactory,這是一個內存存儲方案,不能持久化存儲,在系統重啟或服務器crash時數據會丟失。且不支持索引復制
<directoryFactory class="${solr.directoryFactory:solr.NRTCachingDirectoryFactory}" name="DirectoryFactory">
<str name="solr.hdfs.home">${solr.hdfs.home:}</str>
<str name="solr.hdfs.confdir">${solr.hdfs.confdir:}</str>
<str name="solr.hdfs.blockcache.enabled">${solr.hdfs.blockcache.enabled:true}</str>
<str name="solr.hdfs.blockcache.global">${solr.hdfs.blockcache.global:true}</str>
</directoryFactory>
1.5. codecFactory
編解碼工廠允許使用自定義的編解碼器。例如:如果想啟動per-field DocValues格式, 可以在solrconfig.xml里面設置SchemaCodecFactory:
docValuesFormat="Lucene42": 這是默認設置,所有數據會被加載到堆內存中。
docValuesFormat="Disk": 這是另外一個實現,將部分數據存儲在磁盤上。
docValuesFormat="SimpleText": 文本格式,非常慢,用於學習。
<codecFactory class="solr.SchemaCodecFactory"/>
<schemaFactory class="ClassicIndexSchemaFactory"/>
1.6. indexconfig
用於設置索引的低級別的屬性
1、<filter class="solr.LimitTokenCountFilterFactory" maxTokenCount="10000"/>//限制token最大長度
2、<writeLockTimeout>1000</writeLockTimeout>//IndexWriter等待解鎖的最長時間(毫秒)。
3、<maxIndexingThreads>8</maxIndexingThreads>//
4、<useCompoundFile>false</useCompoundFile>//solr默認為false。如果為true,索引文件減少,檢索性能降低,追求平衡。
5、<ramBufferSizeMB>100</ramBufferSizeMB>//緩存
6、<maxBufferedDocs>1000</maxBufferedDocs>//同上。兩個同時定義時命中較低的那個。
7、<mergePolicy class="org.apache.lucene.index.TieredMergePolicy">
<int name="maxMergeAtOnce">10</int>
<int name="segmentsPerTier">10</int>
</mergePolicy>
//合並策略。
8、<mergeFactor>10</mergeFactor>//合並因子,每次合並多少個segments。
9、<mergeScheduler class="org.apache.lucene.index.ConcurrentMergeScheduler"/>//合並調度器。
10、<lockType>${solr.lock.type:native}</lockType>//鎖工廠。
11、<unlockOnStartup>false</unlockOnStartup>//是否啟動時先解鎖。
12、<termIndexInterval>128</termIndexInterval>//Lucene loads terms into memory 間隔
13、<reopenReaders>true</reopenReaders>//重新打開,替代先關閉-再打開。
14、<deletionPolicy class="solr.SolrDeletionPolicy">//提交刪除策略,必須實現org.apache.lucene.index.IndexDeletionPolicy
15、<str name="maxCommitsToKeep">1</str>
16、<str name="maxOptimizedCommitsToKeep">0</str>
17、<str name="maxCommitAge">30MINUTES</str> OR <str name="maxCommitAge">1DAY</str><br>
18、 <infoStream file="INFOSTREAM.txt">false</infoStream>//相當於把創建索引時的日志輸出。
<lockType>${solr.lock.type:native}</lockType>
設置索引庫的鎖方式,主要有三種:
- single:適用於只讀的索引庫,即索引庫是定死的,不會再更改
- native:使用本地操作系統的文件鎖方式,不能用於多個solr服務共用同一個索引庫。Solr3.6 及后期版本使用的默認鎖機制。
- simple:使用簡單的文件鎖機制
1.7. updateHandler
<updateLog>
<str name="dir">${solr.ulog.dir:}</str>
</updateLog>
設置索引庫更新日志,默認路徑為solr home下面的data/tlog。隨着索引庫的頻繁更新,tlog文件會越來越大,所以建議提交索引時采用硬提交方式<autoCommit>,即批量提交。
<autoCommit>
<maxTime>15000</maxTime>
<maxDocs>10000</maxDocs>
<openSearcher>false</openSearcher>
</autoCommit>
自動硬提交方式:maxTime:設置多長時間提交一次maxDocs:設置達到多少文檔提交一次openSearcher:文檔提交后是否開啟新的searcher,如果false,文檔只是提交到index索引庫,搜索結果中搜不到此次提交的文檔;如果true,既提交到index索引庫,也能在搜索結果中搜到此次提交的內容。
<updateHandler class="solr.DirectUpdateHandler2">
<!-- 允許事務日志 -->
<updateLog>
<str name="dir">${solr.ulog.dir:}</str>
</updateLog>
<!--
在滿足一定條件時自動提交。maxDocs/maxTime/openSearcher
-->
<autoCommit>
<maxTime>15000</maxTime>
<openSearcher>false</openSearcher>
</autoCommit>
<!-- 軟提交VS硬提交 -->
<!--
<autoSoftCommit>
<maxTime>1000</maxTime>
</autoSoftCommit>
-->
<!--
更新相關事件監聽器
postCommit - fired after every commit or optimize command
postOptimize - fired after every optimize command
-->
<!-- The RunExecutableListener executes an external command from a
hook such as postCommit or postOptimize.
exe - the name of the executable to run
dir - dir to use as the current working directory. (default=".")
wait - the calling thread waits until the executable returns.
(default="true")
args - the arguments to pass to the program. (default is none)
env - environment variables to set. (default is none)
-->
<!--
<listener event="postCommit" class="solr.RunExecutableListener">
<str name="exe">solr/bin/snapshooter</str>
<str name="dir">.</str>
<bool name="wait">true</bool>
<arr name="args"> <str>arg1</str> <str>arg2</str> </arr>
<arr name="env"> <str>MYVAR=val1</str> </arr>
</listener>
-->
</updateHandler>
1.8. Query 查詢
<maxBooleanClauses>1024</maxBooleanClauses>
設置boolean 查詢中,最大條件數。在范圍搜索或者前綴搜索時,會產生大量的 boolean 條件,如果條件數達到這個數值時,將拋出異常,限制這個條件數,可以防止條件過多查詢等待時間過長。
<filterCache class="solr.FastLRUCache" size="512" initialSize="512" autowarmCount="0"/>
<queryResultCache class="solr.LRUCache" size="512" initialSize="512" autowarmCount="0"/>
<documentCache class="solr.LRUCache" size="512" initialSize="512" autowarmCount="0"/>
<queryResultMaxDocsCached>200</queryResultMaxDocsCached>
<maxWarmingSearchers>2</maxWarmingSearchers>
1.9. Request Dispatcher請求轉發器
<!-- Request Dispatcher
主要是介紹當有請求訪問SolrCore時SolrDispatchFilter如何處理。
handleSelect是一個以前版本中遺留下來的屬性,會影響請求的對應行為(比如/select?qt=XXX)。
當handleSelect="true"時導致SolrDispatchFilter將請求轉發給qt指定的處理器(前提是/select已經注冊)。
當handleSelect="false"時會直接訪問/select,若/select未注冊則為404。
-->
<requestDispatcher handleSelect="false" >
<!-- Request Parsing:請求解析
這些設置說明Solr Requests如何被解析,以及對ContentStreams有什么限制。
enableRemoteStreaming - 是否允許使用stream.file和stream.url參數來指定遠程streams。
multipartUploadLimitInKB - 指定多文件上傳時Solr允許的最大的size。
formdataUploadLimitInKB - 表單通過POST請求發送的最大size
-->
<requestParsers enableRemoteStreaming="true"
multipartUploadLimitInKB="2048000"
formdataUploadLimitInKB="2048"/>
<!-- HTTP Caching
設置HTTP緩存的相關參數。
-->
<httpCaching never304="true" />
<!--
<httpCaching never304="true" >
<cacheControl>max-age=30, public</cacheControl>
</httpCaching>
-->
<!--
<httpCaching lastModifiedFrom="openTime"
etagSeed="Solr">
<cacheControl>max-age=30, public</cacheControl>
</httpCaching>
-->
</requestDispatcher>
2. schema.xml
schema.xml文件里面主要定義了索引數據類型,索引字段等信息。
2.1. fieldtype
fieldtype節點主要用來定義數據類型。
<fieldType name="string" sortMissingLast="true" class="solr.StrField"/>
<!-- boolean type: "true" or "false" -->
<fieldType name="boolean" sortMissingLast="true" class="solr.BoolField"/>
name指定的是節點定義的名稱
class指向org.apache.solr.analysis中定義的類型名稱
fieldtype還可以自己定義當前類型建立索引和查詢數據的時候使用的查詢分析器。
tokenizer指定分詞器
filter指定過濾器
<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.StandardTokenizerFactory"/>
<filter class="solr.StopFilterFactory" words="stopwords.txt" ignoreCase="true"/>
<filter class="solr.SynonymFilterFactory" ignoreCase="true" expand="true" synonyms="synonyms.txt"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
positionIncrementGap:可選屬性,定義在同一個文檔中此類型數據的空白間隔,避免短語匹配錯誤。
positionIncrementGap=100 只對 multiValue = true 的fieldType有意義。
StrField類型不被分析,而是被逐字地索引/存儲
solr.TextField 允許用戶通過分析器來定制索引和查詢,分析器包括一個分詞器(tokenizer)和多個過濾器(filter)
2.2. field
field節點指定建立索引和查詢數據的字段。
name代表數據字段名稱
type代表數據類型,也就是之前定義的fieldtype
indexed代表是否被索引
stored代表是否被存儲
multiValued是否有多個值,如果字段可能有多個值,盡可能設為true
_version_節點和_root_節點是必須保留的,不能刪除
<field name="_version_" stored="true" indexed="true" type="long"/>
<field name="_root_" stored="false" indexed="true" type="string"/>
<field name="ProductCode" stored="true" indexed="true" type="string" multiValued="false" required="true"/>
<field name="ProductName" stored="true" indexed="true" type="text_general"/>
2.3. copyfield
通過這個節點,可以把一個字段的值復制到另一個字段中,也可以把多個字段的值同時復制到另一個字段中,這樣搜索的時候都可以根據一個字段來進行搜索。
<copyField source="ProductName" dest="text"/>
<copyField source="ProductCode" dest="text"/>
2.4. dynamicField
dynamicField 表示動態字段,可以動態定義一個字段,只要符合規則的字段都可以。
<dynamicField name="*_i" stored="true" indexed="true" type="int"/>
*_i只要以_i結尾的字段都滿足這個定義。
2.5. uniquekey
<uniqueKey>id</uniqueKey>
uniquekey節點是文檔的唯一標示,相當於主鍵,每次更新,刪除的時候都根據這個字段來進行操作。必須填寫。
2.6. defaultSearchField
<defaultSearchField>text</defaultSearchField>
defaultSearchField指定搜索的時候默認搜索字段的值。
2.7. solrQueryParser
<solrQueryParser defaultOperator="OR"/>
solrQueryParser指定搜索時多個詞之間的關系,可以是or,and兩種。
2.8. 性能優化
1、 將所有只用於搜索的,而不需要作為結果的field(特別是一些比較大的field)的stored設置為false;
2、 將不需要被用於搜索的,而只是作為結果返回的field的indexed設置為false;
3、 刪除所有不必要的copyField聲明為了索引字段的最小化和搜索的效率;
4、 將所有的 text fields的index都設置成false,然后使用copyField將他們都復制到一個總的 text field上,然后進行搜索。
