Solr學習筆記(一):基礎篇

簡介

Solr是一種開放源碼的、底層的核心技術是使用Lucene 來實現的搜索引擎。

OK,這里提到了search engine,《solr in action》中,詳細說明了search engine的適用場景,以及和 DB的區別,對我收獲很大,摘抄一段:

1. Search engine

Search engines like Solr are optimized to handle data exhibiting four main characteristics:

  • Text-centric
  • Read- dominant
  • Document- oriented
  • Flexible schema

1.1 Text-centric

A search engine supports non text data such as dates and numbers, but its primary strength is handling text data based on natural language.

If users aren’t interested in the information in the text, a search engine may not be the best solution for your problem.

Think about whether your data is text-centric. The main consideration is whether or not the text fields in your data contain information that users will want to query.

Solr等搜索引擎為搜索包含自然語言的文本內容做了優化,比如電子郵件,網頁,簡歷,PDF文檔,或是推特、微博、博客這些社交內容等等,都適合用Solr來處理。

1.2 Read- dominant

Think of read-dominant as meaning that documents are read far more often than they’re created or updated.

If you must update existing data in a search engine often, that could be an indication that a search engine might not be the best solution for your needs. Another NoSQL technology, like Cassandra, might be a better choice when you need fast random writes to existing data.

1.3 Document-oriented

In a search engine, a document is a self-contained collection of fields, in which each field only holds data(can have multiple values) and doesn’t contain subfields.

A search engine isn’t the place to store data unless it’s useful for search or displaying results

In general, you should store the minimal set of information for each document needed to satisfy search requirements.

1.4 Flexible schema

In a relational database, every row in a table has the same structure. In Solr, documents can have different fields.

2. Solr vs Lucene

兩者的區別有:

  • Lucene本質上是搜索庫,不是獨立的應用程序,而Solr是
  • Lucene專注于搜索底層的建設,而Solr專注于企業應用
  • Lucene不負責支撐搜索服務所必須的管理,而Solr負責

所以說,一句話概括: Solr是Lucene面向企業搜索應用的擴展

Solr 提供了層面搜索、命中醒目顯示并且支持多種輸出格式(包括XML/XSLT 和JSON等格式),它附帶了一個基于HTTP 的管理界面。Solr的特性包括:

  • 高級的全文搜索功能
  • 一個真正的擁有動態字段(Dynamic Field)和唯一鍵(Unique Key)的數據模式(Data Schema)
  • 專為高通量的網絡流量進行的優化
  • 基于開放接口(XML和HTTP)的標準
  • 綜合的HTML管理界面
  • 可伸縮性-能夠有效地復制到另外一個Solr搜索服務器
  • 使用XML配置達到靈活性和適配性
  • 可擴展的插件體系
  • 支持對結果進行動態的分組和過濾
  • 高度可配置和可擴展的緩存機制

因為 Solr 包裝并擴展了Lucene,所以它們使用很多相同的術語。

2. solr 配置

2.1 solrconfig.xml

定義solr的處理程序(handler)和一些擴展程序。其中的配置很多,其實很多都可以保持默認。

  1. dataDir:索引存放位置
  2. autoCommit:solr在建索引的時候收到請求并沒用立即寫入文件,而是先放到緩存中,等收到commit命令時才將緩存中得數據寫入索引文件。
  • maxDocs:
    Maximum number of documents to add since the last commit before automatically triggering a new commit.
  • maxTime:
    Maximum amount of time in ms that is allowed to pass since a document was added before automatically triggering a new commit.
  • openSearcher:
    if false, the commit causes recent index changes to be flushed to stable storage, but does not cause a new searcher to be opened to make those changes visible.
  1. autoSoftCommit:
    softAutoCommit is like autoCommit except it causes a 'soft' commit which only ensures that changes are visible but does not ensure that data is synced to disk. This is faster and more near-realtime friendly than a hard commit.

2.2 manage-schema

用于定義索引的字段和字段類型

2.2.1 fieldType:字段類型(int、float、string、ik...)

<fieldType name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
<fieldType name="text_general" class="solr.TextField" positionIncrementGap="100">
    <analyzer type="index">
      <tokenizer class="solr.StandardTokenizerFactory"/>
    </analyzer>
    <analyzer type="query">
      <tokenizer class="solr.StandardTokenizerFactory"/>
    </analyzer>
</fieldType>
<fieldType name="text_ik" class="solr.TextField" sortMissingLast="true"  omitNorms="true" autoGeneratePhraseQueries="false">
    <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
    <analyzer type="query" isMaxWordLength="true"  class="org.wltea.analyzer.lucene.IKAnalyzer"/>
</fieldType>

2.2.2 field:字段,定義需要的字段名和它的類型

  1. name 字段名
  2. type 字段類型
  3. indexed 是否進行索引
  4. stored 是否進行保存,如不保存,可以進行搜索,但不能顯示該字段的內容
  5. required 是否是必須字段,如若是,該字段必須有值,否則索引報錯
  6. multiValued 是否允許多值
  7. docValues
  8. sortMissingLast
<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />

2.2.3 dynamicFields

動態字段表示,如果字段的定義沒有在配置中找到,就在動態字段類型中進行查找

<dynamicField name="*_txt" type="text_general"    indexed="true"  stored="true" multiValued="true"/> 

2.2.4 copyField

復制源字段到目標字段,maxchars 限制復制的最大長度

<copyField source="body" dest="teaser" maxChars="300"/>  

2.2.5 uniqueKey

相當于數據庫中得主鍵,如建索引時遇到重復的,則會覆蓋掉以前的記錄

<uniqueKey>id</uniqueKey>

2.2.6 defaultSearchField

如果搜索參數中沒有指定具體的field,那么這是默認的域

<defaultSearchField>text</defaultSearchField>  

2.2.7 solrQueryParser

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

<solrQueryParser defaultOperator="OR" />  
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,106評論 6 542
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,441評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,211評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,736評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,475評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,834評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,829評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,009評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,559評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,306評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,516評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,038評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,728評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,132評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,443評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,249評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,484評論 2 379

推薦閱讀更多精彩內容