接上一篇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.jar和hanlp-lucene-plugin.jar共兩個jar放入${tomcat}/webapps/solr/WEB-INF/lib
下
配置好之后混蔼,可以在solr admin界面查看分詞效果:
定義動態(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&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逃贝。
選擇1,然后2可以選擇全量索引或者創(chuàng)建增量索引迫摔。勾選clean會清楚上次的索引沐扳,點(diǎn)選commit創(chuàng)建索引進(jìn)行提交。點(diǎn)擊execute進(jìn)行執(zhí)行句占。
下一篇沪摄,更新spring boot 中集成solrJ對solr進(jìn)行操作。