solr進階三:從數據庫中導入數據到solr

要建立自己的全文檢索,一般都需要從數據庫導入數據,在原來配置的基礎上,增加導入的功能,這裏以mysql爲例子:

solr的工作目錄中選擇一個core,我這裏選擇core1。進入配置文件夾:solr_tomcat\solr\core1\conf 。在solrconfig.xml中添加如下代碼:


<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">  
   <lst name="defaults">  
    <str name="config">data-config.xml</str>  
   </lst>  
 </requestHandler>


在同一目錄下(配置文件夾)下新建data-config.xml,添加以下代碼:

<?xml version="1.0" encoding="utf-8"?>  
<dataConfig>
 <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver"  
   url="jdbc:mysql://localhost:3306/li"  
   user="root"  
   password="465864"/>
   <document name="lhx">
      <entity name="student" pk="sid" query="select sid,sname,sage,saddress,sdescript from student">  
          <field column="sid" name="id" />  
          <field column="sname" name="name" />  
          <field column="sage" name="age" />
          <field column="saddress" name="address" />
          <field column="sdescript" name="descript" /> 
      </entity>
 </document> 
</dataConfig>

修改相應的urluserpassword<document name="lhx">這個隨便取名。

<entity name="student" pk="sid" query="select sid,sname,sage,saddress,sdescript from student">  

name指表名,pk是主鍵名,query是查詢SQL語句。

<field column="sid" name="id" />


column是列名,對應數據庫中字段的名稱,name就是solr這邊對應的名稱。接下來就是配置name了,這要到schema.xml裏面配置,現在打開這個文件,原來有的,可以保留,沒有的就添加,最後的內容爲:

<?xml version="1.0" ?>
<!--
 Licensed to the Apache Software Foundation (ASF) under one or more
 contributor license agreements.  See the NOTICE file distributed with
 this work for additional information regarding copyright ownership.
 The ASF licenses this file to You under the Apache License, Version 2.0
 (the "License"); you may not use this file except in compliance with
 the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<schema name="example core one" version="1.1">

   <fieldtype name="string"  class="solr.StrField" sortMissingLast="true" omitNorms="true"/>
   <fieldType name="long" class="solr.TrieLongField" precisionStep="0" positionIncrementGap="0"/>




  
   <fieldType name="float" class="solr.TrieFloatField" precisionStep="0" positionIncrementGap="0"/>
   <fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/> 
   <fieldType name="date" class="solr.TrieDateField"  precisionStep="0" positionIncrementGap="0"/>  
   <fieldType name="text_ik" class="solr.TextField">   
     <analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>   
   </fieldType>

 
   
  <field name="id" type="long" indexed="true"  stored="true" multiValued="false" required="true"/>  
  <field name="name" type="string" indexed="true"  stored="true"  multiValued="false" /> 
  <field name="age" type="int" indexed="true"  stored="true" multiValued="false" /> 
  <field name="address" type="string" indexed="true"  stored="true" multiValued="false" />
  <field name="descript" type="text_ik" indexed="true" stored="true" multiValued="false" />   
   


  <!-- general -->

  <field name="type"      type="string"    indexed="true"  stored="true"  multiValued="false" /> 
  <field name="core1"     type="string"    indexed="true"  stored="true"  multiValued="false" />
  <field name="_version_" type="long"      indexed="true"  stored="true"/>

 <!-- field to use to determine and enforce document uniqueness. -->
 <uniqueKey>id</uniqueKey>

 <!-- field for the QueryParser to use when an explicit fieldname is absent -->
 <defaultSearchField>name</defaultSearchField>

 <!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->
 <solrQueryParser defaultOperator="OR"/>
</schema>

text_ik是一箇中文分詞器ik-analyzer,專門處理中文分詞。

申明fieldfield的名字應該和sql的查詢結果集列名一致,如果不一致,需要在data-config.xmlentity標籤中用field指明列和field的對應關係。

如下field是必須的,用於標記版本信息,由solr內部自己維護。

<field name="_version_" type="long"      indexed="true"  stored="true"/>

這個原來就有,不用修改。

 

該配置的東西都配置好了,現在就是往MySQL數據庫裏面寫入內容,整個的SQL語句提供給大家:

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
  `sid` bigint(20) NOT NULL,
  `sname` varchar(255) DEFAULT NULL,
  `sage` int(11) DEFAULT NULL,
  `saddress` varchar(255) DEFAULT NULL,
  `sdescript` text,
  PRIMARY KEY (`sid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES ('1', '李四', '23', '中華路1號', '好學生');
INSERT INTO `student` VALUES ('2', '張三', '34', '華信路3號', '壞學生,經常做壞事!');

還要到solr的開發包裏面找相應的jar包,位置:solr-4.10.2\dist ,有兩個:

solr-dataimporthandler-extras-4.10.2

solr-dataimporthandler-4.10.2

還有一個jtds-1.2.4,自己到網上下載。

最後不忘了MySQL的連接包:mysql-connector-java-5.1.30

把這些都放到apache-tomcat-6.0.43\webapps\solr\WEB-INF\lib裏面去。

啓動tomcat,不報錯就可以了!


打開solr,選擇core1,下面的條目選擇“Dataimport”,右邊選擇full-import,接着就是Execute執行,右邊會顯示“Indexing……”

不要一直等待,你一直等待,它都是這個樣子的,要執行第4步:Refresh Status 。最後會出現:


到此大功告成!接着做以下查詢,用到了IK Analyzer分詞。


選擇“Query,接着在右邊的q裏面輸入“要查詢的字段”:“值”,查詢的字段對應在data-config.xml對應的name屬性名稱,而不是數據表的字段名稱。“wt”代表響應的數據,默認是json,不用修改,最後就是點擊“Execute Query”按鈕了。結果會馬上呈現在右邊的空白處。

 

後記:

一開始schema.xml裏面的int類型我寫成了以下的代碼,啓動solr後報出了錯誤。

<fieldType name="int"   class="solr.TrieIntegerField" precisionStep="0" positionIncrementGap="0"/> 

Tomcat裏面報出的錯誤提示就更容易理解了:

怎麼寫int類型才準確呢?我到collection1裏面查考官網的帶的schema.xml,裏面就有對int的定義:

<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>

修改後這個問題就解決了。

 

再重啓solrtomcat裏面報出如下錯誤:

可見是缺少jar包了,添加solr-dataimporthandler-extras-4.10.2solr-dataimporthandler-4.10.2jtds-1.2.4。解決問題!

 

接着就是導入數據分析的時候一直卡在“Indexing……”字樣,點擊刷新也沒反應,在tomcat裏面可以看到如下錯誤提示:

缺少數據庫驅動jar包,導入就沒問題了!

 

搜索的時候,字段名寫得不正確,是有紅色波浪線提示的:



發佈了57 篇原創文章 · 獲贊 13 · 訪問量 37萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章