Solr與資料庫結合實現全文檢索

2021-08-27 10:24:46 字數 2303 閱讀 8385

一下給出了乙個簡單的例項展示了如何將solr與資料庫結合實現全文檢索。生產環境下,會有很大差別,這裡的配置僅作為除錯或是開發所用。

第一步驟就是安裝jdk 和server 伺服器tomcat,這個也挺簡單,這裡就省略了。

將solr自帶的乙個例子複製乙份,我這裡演示的版本是solr最新版本solr 4.2.0.

修改需要與db融合的shema.xml 和 solrconfig檔案,修改細節如下:

假設目標表的結構如下所示:

可以這樣來配置的schema.xml :

修改uniquekeys配置:

customer_id

<?xml version="1.0" encoding="utf-8" ?>

customer_id

tokenizes with standardtokenizer, removes english stop words

(lang/stopwords_en.txt), down cases, protects words from protwords.txt, and

aggressive word-splitting and autophrase features enabled.

this field is just like text_en, except it adds

worddelimiterfilter to enable splitting and matching of

words on case-change, alpha numeric boundaries, and

non-alphanumeric chars. this means certain compound word

cases will work, for example query "wi fi" will match

document "wifi" or "wi-fi".

-->

possible with worddelimiterfilter in conjuncton with stemming. -->

solrconfig.xml配置如下:

指定載入的jar包:

將修要用到的jar包,"

mysql-connection>*\.jar" 和匯入依賴的「apache-solr-dataimporthandler.*\.jar」放在與config同級的目錄下

db-data-dev-config.xml

接下來,配置資料原始檔

db-data-dev-config.xml(與solrconfig.xml同一目錄下)。

在tomcat下建立乙個指向solr core例項的context,具體路徑「conf\catalina\localhost」下。比如,這裡建立的檔名稱是solr.xml。配置資訊如下:

<?xml version="1.0" encoding="utf-8"?>

必須將要用到的war包放到指定的位置,這個路徑指定對了就好。

4. 執行服務

dos環境下,進入到tomcat的bin目錄下,輸入命令:

startup.bat

接著道瀏覽器中具體的路徑,得到如下結果證明配置基本正確。

最後,匯入資料庫中的資料,即根據資料庫表中的資料建立索引。

直接在瀏覽器中輸入:http://localhost:8088/solr/customer/dev/dataimport?command=full-import。

到控制太檢視具體的結果,有如下結果證明配置成功。

資料庫中的資料如下所示:

更多

solr系列二 solr匯入資料庫資料

2.找到d solr home catalog conf schema.xml來配置solr和資料庫的對應字段。uniquekey是文件的唯一標識,必須標識對應的字段,否則solr建立索引報錯。此處的field的name屬性最好和資料庫的字段保持一致,這樣我們可以減少一些對映配置。具體配置如下 ca...

快取與資料庫的結合使用

場景 很多時候,我們希望避開資料庫,更多的使用快取來代替直接從資料庫的讀取,以此來提供程式的效能。為此,我們進行了一些比較常見的封裝 例如 什麼配置資訊,常見的活動配置 基礎配置 系統配置 營銷配置等 配置相關的快取服務基類 public abstract class baseconfigcache...

Oracle資料庫建某字段全文全文檢索

一 建表 create table myindextest tid varchar2 50 primary key,btext blob 二 建索引及引數 1 中文分詞及分詞屬性設定 begin ctx ddl.create preference cnlex chinese lexer ctx dd...