Solr配置文件淺析

接上一篇Linux下安裝solr7.4隅要,來談?wù)剆olr的配置文件schema.xml和db-data-config.xml


首先看schema.xml:

 <!-- If you remove this field, you must _also_ disable the update log in solrconfig.xml
      or Solr won't start. _version_ and update log are required for SolrCloud
   -->
   <field name="_version_" type="plong" indexed="true" stored="true"/>

   <!-- points to the root document of a block of nested documents. Required for nested
      document support, may be removed otherwise
   -->
   <field name="_root_" type="string" indexed="true" stored="false"/>

   <!-- Only remove the "id" field if you have a very good reason to. While not strictly
     required, it is highly recommended. A <uniqueKey> is present in almost all Solr 
     installations. See the <uniqueKey> declaration below where <uniqueKey> is set to "id".
   -->
   <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" />

<!-- Field to use to determine and enforce document uniqueness. 
      Unless this field is marked with required="false", it will be a required field
   -->
 <uniqueKey>id</uniqueKey>

field標(biāo)簽用來定義solr core中的字段。這里列出的三個字段如果沒有特殊原因盡量保留董济。字段id被聲明為uniqueKey,是讓id來唯一標(biāo)明一個solrdocument步清。通過這個id來對solrdocument進(jìn)行操作。

type對應(yīng)的是字段的屬性虏肾,solr在schema中定義了很多屬性廓啊,當(dāng)然也可以自己定義屬性。這里常見的屬性有pint,pdate,string,boolean等封豪。

  <!-- The StrField type is not analyzed, but indexed/stored verbatim. -->
    <fieldType name="string" class="solr.StrField" sortMissingLast="true" />

    <!-- boolean type: "true" or "false" -->
    <fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
    <fieldType name="pint" class="solr.IntPointField" docValues="true"/>
    <fieldType name="pfloat" class="solr.FloatPointField" docValues="true"/>
    <fieldType name="plong" class="solr.LongPointField" docValues="true"/>
    <fieldType name="pdouble" class="solr.DoublePointField" docValues="true"/>
    <!-- KD-tree versions of date fields -->
    <fieldType name="pdate" class="solr.DatePointField" docValues="true"/>

不常見或者自定義的屬性:

  <!-- A text field that only splits on whitespace for exact matching of words -->
    <fieldType name="text_ws" class="solr.TextField" positionIncrementGap="100">
      <analyzer>
        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
      </analyzer>
    </fieldType>

該屬性type="text_ws"定義的字段通過空格去分割文本變成一個一個的詞谴轮,然后可以通過被分割的詞去查找該document。
這里用到的逆向索引是solr的精髓吹埠,將分好的詞作為key第步,文檔標(biāo)簽作為value,對key建索引缘琅,去查詢文檔粘都。

indexed屬性如果為true則說明該字段將被建索引。

stored屬性如果為true刷袍,則將該字段內(nèi)容進(jìn)行存儲翩隧。

  <field name="title" type="string" indexed="false" stored="true" required="true" multiValued="false" />
   <field name="content" type="string" indexed="false" stored="true" required="true" multiValued="false"/>
<field name="text" type="text_hanlp" indexed="true" stored="false" required="true" multiValued="true"/>
   <copyField source="content" dest="text" />
   <copyField source="title" dest="text" />

multiValued如果設(shè)置為true,則表明該字段是由多個字段值組成的做个。比如上面例子中的text字段鸽心,它是由content和title字段組成滚局。對text字段的操作就是對content和title字段進(jìn)行操作居暖。
上面這一段配置的意思是:有兩個字段title和content,他們是自定義的text_hanlp屬性藤肢,含有這屬性的字段都接受hanlp的分詞太闺。這兩個字段不創(chuàng)建索引,只做存儲嘁圈。text字段負(fù)責(zé)組合title和content字段省骂,并創(chuàng)建索引用來檢索。

required屬性表明該字段值是否必須最住。

自定義屬性text_hanlp來達(dá)到中文分詞效果

 <!-- text_cn字段類型: 指定使用HanLP分詞器钞澳,同時開啟索引模式。通過solr自帶的停用詞過濾器涨缚,使用"stopwords.txt"(默認(rèn)空白)過濾轧粟。
         在搜索的時候,還支持solr自帶的同義詞詞典。-->
    <fieldType name="text_hanlp" class="solr.TextField" positionIncrementGap="100">
      <analyzer type="index">
        <tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" enableIndexMode="true"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
        <!-- 取消注釋可以啟用索引期間的同義詞詞典-->
       <!-- <filter class="solr.SynonymFilterFactory" synonyms="index_synonyms.txt" ignoreCase="true" expand="false"/>-->
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
      <analyzer type="query">
        <tokenizer class="com.hankcs.lucene.HanLPTokenizerFactory" enableIndexMode="false"/>
        <filter class="solr.StopFilterFactory" ignoreCase="true" words="stopwords.txt" />
        <filter class="solr.SynonymFilterFactory" synonyms="synonyms.txt" ignoreCase="true" expand="true"/>
        <filter class="solr.LowerCaseFilterFactory"/>
      </analyzer>
    </fieldType>

因為solr常用的ik分詞兰吟、mmseg4j都已經(jīng)不維護(hù)了通惫。所以這里使用還有大神維護(hù)的Hanlp分詞器。
配置Hanlp分詞可以參考教程
hanlp-portable.jarhanlp-lucene-plugin.jar共兩個jar放入${tomcat}/webapps/solr/WEB-INF/lib

配置好之后混蔼,可以在solr admin界面查看分詞效果:


solr分詞

定義動態(tài)字段

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

dynamicField定義的就是動態(tài)字段履腋,只要符合_i結(jié)尾的字段都可以被這個字段所定義。同樣的惭嚣,schema.xml中已經(jīng)定義好了很多動態(tài)字段遵湖。可以直接拿來用晚吞。


db-data-config.xml配置文件
該文件主要配置數(shù)據(jù)庫連接和字段對應(yīng)關(guān)系奄侠。用來做全量和增量索引的創(chuàng)建,相對schema.xml簡單很多载矿。
下面看下主要配置:

<dataSource driver="com.mysql.jdbc.Driver"                   
                         url="jdbc:mysql://127.0.0.1:3306/database? 
                                           useUnicode=true&amp;characterEncoding=UTF-8" 
                         user="root" 
                         batchSize="-1"
                         password="123456"/>

dataSource用來定義數(shù)據(jù)庫連接垄潮,batchSize設(shè)為-1是為了避免查詢創(chuàng)建索引導(dǎo)致內(nèi)存溢出。

<document>
        <entity dataSource="jdbcDataSource" name="core" pk="id"  
        query="select * from tableName" >
            <field column="id" name="id"></field>
            <field column="title" name="title"></field>
            <field column="content" name="content"></field>
            <field column="author" name="author"></field>
        </entity>
    </document>

這里做了一個簡單的定義闷盔,看著很清楚弯洗。columen標(biāo)明的是數(shù)據(jù)庫查出的字段,name標(biāo)明的屬性和schema中定義的字段對應(yīng)逢勾。

<entity name="item" query="select * from item"
                deltaQuery="select id from item where last_modified > '${dataimporter.last_index_time}'">
            <field column="NAME" name="name" />

deltaQuery用來做增量索引的創(chuàng)建牡整。

當(dāng)文件配置好之后,重啟tomcat溺拱。訪問solr/index.html逃贝。


solr創(chuàng)建索引

選擇1,然后2可以選擇全量索引或者創(chuàng)建增量索引迫摔。勾選clean會清楚上次的索引沐扳,點(diǎn)選commit創(chuàng)建索引進(jìn)行提交。點(diǎn)擊execute進(jìn)行執(zhí)行句占。


下一篇沪摄,更新spring boot 中集成solrJ對solr進(jìn)行操作。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末纱烘,一起剝皮案震驚了整個濱河市杨拐,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌擂啥,老刑警劉巖哄陶,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異哺壶,居然都是意外死亡屋吨,警方通過查閱死者的電腦和手機(jī)舱痘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來离赫,“玉大人芭逝,你說我怎么就攤上這事≡ㄐ兀” “怎么了旬盯?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長翎猛。 經(jīng)常有香客問我胖翰,道長,這世上最難降的妖魔是什么切厘? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任萨咳,我火速辦了婚禮,結(jié)果婚禮上疫稿,老公的妹妹穿的比我還像新娘培他。我一直安慰自己,他們只是感情好遗座,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布舀凛。 她就那樣靜靜地躺著,像睡著了一般途蒋。 火紅的嫁衣襯著肌膚如雪猛遍。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天号坡,我揣著相機(jī)與錄音懊烤,去河邊找鬼。 笑死宽堆,一個胖子當(dāng)著我的面吹牛腌紧,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播日麸,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼寄啼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了代箭?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤涕刚,失蹤者是張志新(化名)和其女友劉穎嗡综,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體杜漠,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡极景,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年察净,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片盼樟。...
    茶點(diǎn)故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡氢卡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出晨缴,到底是詐尸還是另有隱情译秦,我是刑警寧澤,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布击碗,位于F島的核電站筑悴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏稍途。R本人自食惡果不足惜阁吝,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望械拍。 院中可真熱鬧突勇,春花似錦、人聲如沸坷虑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽猖吴。三九已至摔刁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間海蔽,已是汗流浹背共屈。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留党窜,地道東北人拗引。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像幌衣,于是被迫代替她去往敵國和親矾削。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,884評論 2 354