新聞全文檢索服務
1.需要索引的字段
客戶端ID info_classify.app_id
客戶端名 app_info.name
欄目ID info_classify.columns_id
欄目名 columninfo.columnName
新聞內容 info_classify.content_text
創建時間 info_classify.create_time
ID info_classify.id
標籤 info_classify.info_label
新聞ID info_classify.information_id
新聞標題 info_classify.list_title
列表顯示類型 info_classify.list_view_type
上線時間 info_classify.online_time
狀態 info_classify.status
新聞摘要 information.synopsis
2.搭建solr搜索引擎服務
3.編寫數據同步服務
- 首次同步是全量同步,分頁同步,每頁1000條吧;
- 後續同步是最大ID同步,查詢出索引中最大的ID,再查詢比這個ID大的新聞進行索引
4.政務雲cms接入搜索引擎,對新聞進行檢索,操作按鈕需要進一步確認
solr版本:https://mirrors.tuna.tsinghua.edu.cn/apache/lucene/solr/8.5.0/solr-8.5.0.tgz
tomcat版本:https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.33/bin/apache-tomcat-9.0.33.tar.gz
solr文件地址:/var/www/file/images2/solr/solr-8.5.0/
程序地址:/usr/local/server/apache-tomcat-solr
solr-core-home:
/var/www/file/images2/solr/cores/info
/var/www/file/images2/solr/solr-8.5.0/server/solr/info/
5.配置solr-home
<env-entry>
<env-entry-name>solr/home</env-entry-name>
<env-entry-value>/var/www/file/images2/solr/cores</env-entry-value>
<env-entry-type>java.lang.String</env-entry-type>
</env-entry>
6.重啓solr服務
/var/www/file/images2/solr/solr-8.5.0/bin/solr restart -force -m 4g
7.刪除所有數據:
1)documents type 選擇 XML
2)documents 輸入下面語句
<delete><query>*:*</query></delete>
<commit/>
3)點擊Submit Document 即可
8.100%匹配關鍵字查詢
q : title:通遠門
Raw Query Parameters : defType=edismax&mm=100%
9.設置smartcn分詞器
9.1.複製自帶的jar包
cp /var/www/file/images2/solr/solr-8.5.0/contrib/analysis-extras/lucene-libs/lucene-analyzers-smartcn-8.5.0.jar /var/www/file/images2/solr/solr-8.5.0/server/solr-webapp/webapp/WEB-INF/lib/
9.2.修改managed-schema ,配置字段類型
<!-- 配置中文分詞器 -->
<fieldType name="text_smartcn" class="solr.TextField" positionIncrementGap="100">
<analyzer type="index">
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.apache.lucene.analysis.cn.smart.HMMChineseTokenizerFactory"/>
</analyzer>
</fieldType>
9.3.修改managed-schema ,指定字段類型
<field name="content" type="text_smartcn" indexed="true" stored="true"/>
10.jar包啓動腳本
nohup /usr/local/jdk1.8.0_181/bin/java \
-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=128m \
-Xms1024m -Xmx1024m -Xmn256m -Xss256k \
-XX:SurvivorRatio=8 -XX:+UseConcMarkSweepGC \
-XX:+PrintGCDateStamps -XX:+PrintGCDetails \
-verbose:gc -Xloggc:/var/www/logs/cqliving-cloud-solr/gc.log \
-XX:+HeapDumpOnOutOfMemoryError \
-XX:HeapDumpPath=/var/www/logs/cqliving-cloud-solr/oom.hprof \
-Dspring.profiles.active=prod \
-jar cqliving-cloud-solr-1.0.1-SNAPSHOT.jar 1>"/var/www/logs/cqliving-cloud-solr/console.log" 2>&1 </dev/null &
11.配置IK分詞器
1、下載ik分詞器:https://github.com/magese/ik-analyzer-solr
<dependency>
<groupId>com.github.magese</groupId>
<artifactId>ik-analyzer</artifactId>
<version>8.3.0</version>
</dependency>
2、將下載下來的jar包複製到solr\WEB-INF\lib中
2.1、 將resources目錄下的5個配置文件放入solr服務的Jetty或Tomcat的webapp/WEB-INF/classes/目錄下
① IKAnalyzer.cfg.xml
② ext.dic
③ stopword.dic
④ ik.conf
⑤ dynamicdic.txt
注意:
- 修改dynamicdic.txt,同步修改ik.conf的lastupdate字段,可以設置爲時間戳
- 修改詞典後需要重建索引才能應用新詞
3、配置Solr的managed-schema,添加ik分詞器:
<field name="id" type="pint" indexed="true" stored="true" required="true" multiValued="false" />
<!-- docValues are enabled by default for long type so we don't need to index the version field -->
<field name="_version_" type="plong" indexed="false" stored="false"/>
<!-- If you don't use child/nested documents, then you should remove the next two fields: -->
<!-- for nested documents (minimal; points to root document) -->
<field name="_root_" type="pint" indexed="true" stored="false" docValues="false" />
<!-- for nested documents (relationship tracking) -->
<field name="_nest_path_" type="_nest_path_" />
<field name="_text_" type="text_general" indexed="true" stored="false" multiValued="true"/>
<field name="appId" type="plong" indexed="true" stored="true"/>
<field name="appName" type="text_ik" indexed="true" stored="true"/>
<field name="columnName" type="text_ik" indexed="true" stored="true"/>
<field name="columnsId" type="plong" indexed="true" stored="true"/>
<field name="contentText" type="text_ik" indexed="true" stored="true"/>
<field name="createdTime" type="pdate" indexed="true" stored="true"/>
<field name="infoLabel" type="text_ik" indexed="true" stored="true"/>
<field name="informationId" type="plong" indexed="true" stored="true"/>
<field name="title" type="text_ik" indexed="true" stored="true"/>
<field name="listViewType" type="plong" indexed="true" stored="true"/>
<field name="onlineTime" type="pdate" indexed="true" stored="true"/>
<field name="status" type="pint" indexed="true" stored="true"/>
<field name="synopsis" type="text_ik" indexed="true" stored="true"/>
<!-- ik分詞器 -->
<fieldType name="text_ik" class="solr.TextField">
<analyzer type="index">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="false" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="org.wltea.analyzer.lucene.IKTokenizerFactory" useSmart="true" conf="ik.conf"/>
<filter class="solr.LowerCaseFilterFactory"/>
</analyzer>
</fieldType>
4、然後將field的type修改成text_ik
5、配置擴展詞典
5.1、 擴展詞 ext.dic
5.2、 停用詞 stopword.dic
5.3、 配置文件
vim /WEB-INF/classes/IKAnalyzer.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>IK Analyzer 擴展配置</comment>
<!-- 配置是否加載默認詞典 -->
<entry key="use_main_dict">true</entry>
<!--用戶可以在這裏配置自己的擴展字典,表示使用哪些詞來做索引,多個用分號分隔 -->
<entry key="ext_dict">ext.dic;</entry>
<!--用戶可以在這裏配置自己的擴展停止詞字典,表示不用哪些詞做索引,多個用分號分隔-->
<entry key="ext_stopwords">stopword.dic;</entry>
</properties>