solr 配置文件詳解

1、字段配置(schema)

 

 

schema.xml位於solr/conf/目錄下,相似於數據表配置文件,html

定義了加入索引的數據的數據類型,主要包括type、fields和其餘的一些缺省設置。java

 

一、先來看下type節點,這裏面定義FieldType子節點,包括name,class,positionIncrementGap等一些參數。

  • name:就是這個FieldType的名稱。
  • class:指向org.apache.solr.analysis包裏面對應的class名稱,用來定義這個類型的行爲。

[html] view plain copy數據庫

 

  1. <schema name="example" version="1.2">    
  2.   <types>    
  3.     <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>    
  4.     <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" omitNorms="true"/>    
  5.     <fieldtype name="binary" class="solr.BinaryField"/>    
  6.     <fieldType name="int" class="solr.TrieIntField" precisionStep="0" omitNorms="true"     
  7.                                                                 positionIncrementGap="0"/>    
  8.     <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" omitNorms="true"     
  9.                                                                 positionIncrementGap="0"/>    
  10.     <fieldType name="long" class="solr.TrieLongField" precisionStep="0" omitNorms="true"     
  11.                                                                 positionIncrementGap="0"/>    
  12.     <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" omitNorms="true"     
  13.                                                                 positionIncrementGap="0"/>    
  14.   ...    
  15.   </types>    
  16.   ...    
  17. </schema>    

 

[xhtml] view plain copyapache

 

  1. <schema name="example" version="1.2">  
  2.   <types>  
  3.     <fieldType name="string" class="solr.StrField" sortMissingLast="true" omitNorms="true"/>  
  4.     <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true" omitNorms="true"/>  
  5.     <fieldtype name="binary" class="solr.BinaryField"/>  
  6.     <fieldType name="int" class="solr.TrieIntField" precisionStep="0" omitNorms="true"   
  7.                                                                 positionIncrementGap="0"/>  
  8.     <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" omitNorms="true"   
  9.                                                                 positionIncrementGap="0"/>  
  10.     <fieldType name="long" class="solr.TrieLongField" precisionStep="0" omitNorms="true"   
  11.                                                                 positionIncrementGap="0"/>  
  12.     <fieldType name="double" class="solr.TrieDoubleField" precisionStep="0" omitNorms="true"   
  13.                                                                 positionIncrementGap="0"/>  
  14.   ...  
  15.   </types>  
  16.   ...  
  17. </schema>  

 

 

必要的時候fieldType還須要本身定義這個類型的數據在創建索引和進行查詢的時候要使用的分析器analyzer,包括分詞和過濾,以下:緩存

 

[html] view plain copy服務器

 

  1. <fieldType name="text_ws" class="solr.TextField" positionIncrementGap="100">    
  2.   <analyzer>     
  3.     <tokenizer class="solr.WhitespaceTokenizerFactory"/>     
  4.   </analyzer>     
  5. </fieldType>     
  6. <fieldType name="text" class="solr.TextField" positionIncrementGap="100">     
  7.   <analyzer type="index">     
  8.     <!--這個分詞包是空格分詞,在向索引庫添加text類型的索引時,Solr會首先用空格進行分詞     
  9.          而後把分詞結果依次使用指定的過濾器進行過濾,最後剩下的結果,纔會加入到索引庫中以備查詢。     
  10.       注意:Solr的analysis包並無帶支持中文的包,須要本身添加中文分詞器,google下。       
  11.      -->     
  12.     <tokenizer class="solr.WhitespaceTokenizerFactory"/>     
  13.         <!-- in this example, we will only use synonyms at query time     
  14.         <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt"     
  15.                                                   ignoreCase="true" expand="false"/>    
  16.         -->     
  17.         <!-- Case insensitive stop word removal.     
  18.           add enablePositionIncrements=true in both the index and query     
  19.           analyzers to leave a 'gap' for more accurate phrase queries.     
  20.         -->     
  21.       <filter class="solr.StopFilterFactory"     
  22.                 ignoreCase="true"     
  23.                 words="stopwords.txt"     
  24.                 enablePositionIncrements="true"     
  25.                 />     
  26.       <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1"     
  27.               generateNumberParts="1" catenateWords="1" catenateNumbers="1"     
  28.               catenateAll="0" splitOnCaseChange="1"/>     
  29.       <filter class="solr.LowerCaseFilterFactory"/>     
  30.       <filter class="solr.SnowballPorterFilterFactory" language="English"      
  31.                                                        protected="protwords.txt"/>    
  32.     </analyzer>     
  33.     <analyzer type="query">     
  34.       <tokenizer class="solr.WhitespaceTokenizerFactory"/>     
  35.         <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true"     
  36.                                                                           expand="true"/>    
  37.         <filter class="solr.StopFilterFactory"     
  38.                 ignoreCase="true"     
  39.                 words="stopwords.txt"     
  40.                 enablePositionIncrements="true"     
  41.                 />     
  42.         <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1"     
  43.                 generateNumberParts="1" catenateWords="0" catenateNumbers="0"     
  44.                                         catenateAll="0" splitOnCaseChange="1"/>    
  45.         <filter class="solr.LowerCaseFilterFactory"/>     
  46.         <filter class="solr.SnowballPorterFilterFactory" language="English"     
  47.                                                          protected="protwords.txt"/>    
  48.       </analyzer>     
  49. </fieldType>    


 

 

二、再來看下fields節點內定義具體的字段(相似數據庫的字段)

含有如下屬性:網絡

  • name:字段名
  • type:以前定義過的各類FieldType
  • indexed:是否被索引
  • stored:是否被存儲(若是不須要存儲相應字段值,儘可能設爲false)
  • multiValued:是否有多個值(對可能存在多值的字段儘可能設置爲true,避免建索引時拋出錯誤)

 

[html] view plain copy多線程

 

  1. <fields>     
  2.     <field name="id" type="integer" indexed="true" stored="true" required="true" />    
  3.     <field name="name" type="text" indexed="true" stored="true" />     
  4.     <field name="summary" type="text" indexed="true" stored="true" />     
  5.     <field name="author" type="string" indexed="true" stored="true" />     
  6.     <field name="date" type="date" indexed="false" stored="true" />     
  7.     <field name="content" type="text" indexed="true" stored="false" />     
  8.     <field name="keywords" type="keyword_text" indexed="true" stored="false" multiValued="true" />    
  9.     <!--拷貝字段-->     
  10.     <field name="all" type="text" indexed="true" stored="false" multiValued="true"/>    
  11. </fields>    


 

 

三、建議創建一個拷貝字段,將全部的 全文本 字段複製到一個字段中,以便進行統一的檢索:

     如下是拷貝設置:app

[html] view plain copy性能

 

  1. <copyField source="name" dest="all"/>     
  2. <copyField source="summary" dest="all"/>    

 

四、動態字段,沒有具體名稱的字段,用dynamicField字段

如:name爲*_i,定義它的type爲int,那麼在使用這個字段的時候,任務以_i結果的字段都被認爲符合這個定義。如name_i, school_i

[html] view plain copy

 

  1. <dynamicField name="*_i"  type="int"    indexed="true"  stored="true"/>    
  2. <dynamicField name="*_s"  type="string"  indexed="true"  stored="true"/>    
  3. <dynamicField name="*_l"  type="long"   indexed="true"  stored="true"/>    
  4. <dynamicField name="*_t"  type="text"    indexed="true"  stored="true"/>    
  5. <dynamicField name="*_b"  type="boolean" indexed="true"  stored="true"/>    
  6. <dynamicField name="*_f"  type="float"  indexed="true"  stored="true"/>    
  7. <dynamicField name="*_d"  type="double" indexed="true"  stored="true"/>    
  8. <dynamicField name="*_dt" type="date"    indexed="true"  stored="true"/>    


 

 

 

二. schema.xml文檔註釋中的信息

 

一、爲了改進性能,能夠採起如下幾種措施:

  • 將全部只用於搜索的,而不須要做爲結果的field(特別是一些比較大的field)的stored設置爲false
  • 將不須要被用於搜索的,而只是做爲結果返回的field的indexed設置爲false
  • 刪除全部沒必要要的copyField聲明
  • 爲了索引字段的最小化和搜索的效率,將全部的 text fields的index都設置成field,而後使用copyField將他們都複製到一個總的 text field上,而後對他進行搜索。
  • 爲了最大化搜索效率,使用java編寫的客戶端與solr交互(使用流通訊)
  • 在服務器端運行JVM(省去網絡通訊),使用盡量高的Log輸出等級,減小日誌量。

二、schema名字

<schema name="example" version="1.2">

  • name:標識這個schema的名字
  • version:如今版本是1.2

三、filedType

 

<fieldTypename="string" class="solr.StrField" sortMissingLast="true" omitNorms="true" />

  • name:標識而已。
  • class和其餘屬性決定了這個fieldType的實際行爲。(class以solr開始的,都是在org.appache.solr.analysis包下)

可選的屬性:

  • sortMissingLast和sortMissingFirst兩個屬性是用在能夠內在使用String排序的類型上(包括:string,boolean,sint,slong,sfloat,sdouble,pdate)。
  • sortMissingLast="true",沒有該field的數據排在有該field的數據以後,而無論請求時的排序規則。
  • sortMissingFirst="true",跟上面倒過來唄。
  • 2個值默認是設置成false

 

StrField類型不被分析,而是被逐字地索引/存儲。

StrField和TextField都有一個可選的屬性「compressThreshold」,保證壓縮到不小於一個大小(單位:char)

 

 

<fieldType name="text" class="solr.TextField" positionIncrementGap="100">

 

solr.TextField 容許用戶經過分析器來定製索引和查詢,分析器包括 一個分詞器(tokenizer)和多個過濾器(filter)

 

  • positionIncrementGap:可選屬性,定義在同一個文檔中此類型數據的空白間隔,避免短語匹配錯誤。

 

 

<tokenizerclass="solr.WhitespaceTokenizerFactory" />

空格分詞,精確匹配。

 

 

<filterclass="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="1" catenateNumbers="1" catenateAll="0" splitOnCaseChange="1" />

在分詞和匹配時,考慮 "-"連字符,字母數字的界限,非字母數字字符,這樣 "wifi"或"wi fi"都能匹配"Wi-Fi"。

 

<filterclass="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true" />

同義詞

 

<filterclass="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" enablePositionIncrements="true" />

在禁用字(stopword)刪除後,在短語間增長間隔

stopword:即在創建索引過程當中(創建索引和搜索)被忽略的詞,好比is this等經常使用詞。在conf/stopwords.txt維護。

 

 

 

四、fields

 

<fieldname="id" type="string" indexed="true" stored="true" required="true" />

  • name:標識而已。
  • type:先前定義的類型。
  • indexed:是否被用來創建索引(關係到搜索和排序)
  • stored:是否儲存
  • compressed:[false],是否使用gzip壓縮(只有TextField和StrField能夠壓縮)
  • mutiValued:是否包含多個值
  • omitNorms:是否忽略掉Norm,能夠節省內存空間,只有全文本field和need an index-time boost的field須要norm。(具體沒看懂,註釋裏有矛盾)
  • termVectors:[false],當設置true,會存儲 term vector。當使用MoreLikeThis,用來做爲類似詞的field應該存儲起來。
  • termPositions:存儲 term vector中的地址信息,會消耗存儲開銷。
  • termOffsets:存儲 term vector 的偏移量,會消耗存儲開銷。
  • default:若是沒有屬性須要修改,就能夠用這個標識下。

 

<fieldname="text" type="text" indexed="true" stored="false" multiValued="true" />

一應俱全(有點誇張)的field,包含全部可搜索的text fields,經過copyField實現。

 

<copyFieldsource="cat" dest="text" />

<copyFieldsource="name" dest="text" />

<copyFieldsource="manu" dest="text" />

<copyFieldsource="features" dest="text" />

<copyFieldsource="includes" dest="text" />

在添加索引時,將全部被拷貝field(如cat)中的數據拷貝到text field中

做用:

  • 將多個field的數據放在一塊兒同時搜索,提供速度
  • 將一個field的數據拷貝到另外一個,能夠用2種不一樣的方式來創建索引。

 

 

<dynamicFieldname="*_i" type="int" indexed="true" stored="true" />

 

若是一個field的名字沒有匹配到,那麼就會用動態field試圖匹配定義的各類模式。

  • "*"只能出如今模式的最前和最後
  • 較長的模式會被先去作匹配
  • 若是2個模式同時匹配上,最早定義的優先

 

<dynamicFieldname="*" type="ignored" multiValued="true" />

若是經過上面的匹配都沒找到,能夠定義這個,而後定義個type,當String處理。(通常不會發生)

但若不定義,找不到匹配會報錯。

 

 

五、其餘一些標籤

 

<uniqueKey>id</uniqueKey>

文檔的惟一標識, 必須填寫這個field(除非該field被標記required="false"),不然solr創建索引報錯。

<defaultSearchField>text</defaultSearchField>

若是搜索參數中沒有指定具體的field,那麼這是默認的域。

<solrQueryParserdefaultOperator="OR" />

配置搜索參數短語間的邏輯,能夠是"AND|OR"。

 

 

 

3、solrconfig.xml

 

一、索引配置

 

mainIndex 標記段定義了控制Solr索引處理的一些因素.

  • useCompoundFile:經過將不少 Lucene 內部文件整合到單一一個文件來減小使用中的文件的數量。這可有助於減小 Solr 使用的文件句柄數目,代價是下降了性能。除非是應用程序用完了文件句柄,不然false 的默認值應該就已經足夠。

  • useCompoundFile:經過將不少Lucene內部文件整合到一個文件,來減小使用中的文件的數量。這可有助於減小Solr使用的文件句柄的數目,代價是下降了性能。除非是應用程序用完了文件句柄,不然false的默認值應該就已經足夠了。
  • mergeFacor:決定Lucene段被合併的頻率。較小的值(最小爲2)使用的內存較少但致使的索引時間也更慢。較大的值可以使索引時間變快但會犧牲較多的內存。(典型的 時間與空間 的平衡配置)
  • maxBufferedDocs:在合併內存中文檔和建立新段以前,定義所需索引的最小文檔數。段 是用來存儲索引信息的Lucene文件。較大的值可以使索引時間變快但會犧牲較多內存。
  • maxMergeDocs:控制可由Solr合併的 Document 的最大數。較小的值(<10,000)最適合於具備大量更新的應用程序。
  • maxFieldLength:對於給定的Document,控制可添加到Field的最大條目數,進而階段該文檔。若是文檔可能會很大,就須要增長這個數值。而後,若將這個值設置得太高會致使內存不足錯誤。
  • unlockOnStartup:告知Solr忽略在多線程環境中用來保護索引的鎖定機制。在某些狀況下,索引可能會因爲不正確的關機或其餘錯誤而一直處於鎖定,這就妨礙了添加和更新。將其設置爲true能夠禁用啓動索引,進而容許進行添加和更新。(鎖機制)

 

二、查詢處理配置

 

query標記段中如下一些與緩存無關的特性:

  • maxBooleanClauses:定義可組合在一塊兒造成以個查詢的字句數量的上限。正常狀況1024已經足夠。若是應用程序大量使用了通配符或範圍查詢,增長這個限制將能避免當值超出時,拋出TooMangClausesException。
  • enableLazyFieldLoading: 若是應用程序只會檢索Document上少數幾個Field,那麼能夠將這個屬性設置爲true。懶散加載的一個常見場景大都發生在應用程序返回一些列搜 索結果的時候,用戶經常會單擊其中的一個來查看存儲在此索引中的原始文檔。初始的現實經常只須要現實很短的一段信息。如果檢索大型的Document,除 非必需,不然就應該避免加載整個文檔。

 

query部分負責定義與在Solr中發生的時間相關的幾個選項:

 

 

概念:Solr(其實是Lucene)使用稱爲Searcher的Java類 來處理Query實例。Searcher將索引內容相關的數據加載到內存中。根據索引、CPU已經可用內存的大小,這個過程可能須要較長的一段時間。要改 進這一設計和顯著提升性能,Solr引入了一張「溫暖」策略,即把這些新的Searcher聯機以便爲現場用戶提供查詢服務以前,先對它們進行「熱身」。

  • newSearcher和firstSearcher事件,可使用這些事件來制定實例化新Searcher或第一個Searcher時,應該執 行哪些查詢。若是應用程序指望請求某些特定的查詢,那麼在建立新Searcher或第一個Searcher時就應該反註釋這些部分並執行適當的查詢。

 

query中的智能緩存:

 

  • filterCache:經過存儲一個匹配給定查詢的文檔 id 的無序集,過濾器讓 Solr 可以有效提升查詢的性能。緩存這些過濾器意味着對Solr的重複調用能夠致使結果集的快速查找。更常見的場景是緩存一個過濾器,而後再發起後續的精煉查 詢,這種查詢能使用過濾器來限制要搜索的文檔數。
  • queryResultCache:爲查詢、排序條件和所請求文檔的數量緩存文檔 id 的有序集合。
  • documentCache:緩存Lucene Document,使用內部Lucene文檔id(以便不與Solr惟一id相混淆)。因爲Lucene的內部Document id 能夠因索引操做而更改,這種緩存不能自熱。
  • Named caches:命名緩存是用戶定義的緩存,可被 Solr定製插件 所使用。

其中filterCache、queryResultCache、Named caches(若是實現了org.apache.solr.search.CacheRegenerator)能夠自熱。

每一個緩存聲明都接受最多四個屬性:

  • class:是緩存實現的Java名
  • size:是最大的條目數
  • initialSize:是緩存的初始大小
  • autoWarmCount:是取自舊緩存以預熱新緩存的條目數。若是條目不少,就意味着緩存的hit會更多,只不過須要花更長的預熱時間。

對於全部緩存模式而言,在設置緩存參數時,都有必要在內存、cpu和磁盤訪問之間進行均衡。統計信息管理頁(管理員界面的Statistics)對 於分析緩存的 hit-to-miss 比例以及微調緩存大小的統計數據都很是有用。並且,並不是全部應用程序都會從緩存受益。實際上,一些應用程序反而會因爲須要將某個永遠也用不到的條目存儲在 緩存中這一額外步驟而受到影響。

相關文章
相關標籤/搜索