Solr(2)的實例

這裏我使用的數據庫是MySQL,首先集成MySQL

1.創建表

  1. -- ----------------------------
  2. -- Table structure for `documents`
  3. -- ----------------------------
  4. DROP TABLE IF EXISTS `documents`;
  5. CREATE TABLE `documents` (
  6. `id` int(11) NOTNULL auto_increment,
  7. `date_added` datetime NOTNULL,
  8. `title` varchar(255) NOTNULL,
  9. `content` text NOT NULL,
  10. PRIMARY KEY (`id`)
  11. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
  12. -- ----------------------------
  13. -- Records of documents
  14. -- ----------------------------
  15. INSERT INTO `documents`VALUES ('1','2012-01-11 23:15:59', 'world','test1');
  16. INSERT INTO `documents`VALUES ('2','2012-01-11 23:16:30', 'hello','test');
-- ----------------------------
-- Table structure for `documents`
-- ----------------------------
DROP TABLE IF EXISTS `documents`;
CREATE TABLE `documents` (
  `id` int(11) NOT NULL auto_increment,
  `date_added` datetime NOT NULL,
  `title` varchar(255) NOT NULL,
  `content` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of documents
-- ----------------------------
INSERT INTO `documents` VALUES ('1', '2012-01-11 23:15:59', 'world', 'test1');
INSERT INTO `documents` VALUES ('2', '2012-01-11 23:16:30', 'hello', 'test');

2. 加入DataImportHandler,在solr\conf\solrconfig.xml中

  1. <requestHandlername="/dataimport"
  2. class="org.apache.solr.handler.dataimport.DataImportHandler">
  3. <lstname="defaults">
  4. <strname="config">data-config.xml</str>
  5. </lst>
  6. </requestHandler>
<requestHandler name="/dataimport" 
     class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">data-config.xml</str>
    </lst>
</requestHandler>

(1)如果出現如下錯誤的時候,

嚴重: org.apache.solr.common.SolrException: Error loading class 'org.apache.solr .handler.dataimport.DataImportHandler'

需要在前面添加一段類似這樣子的配置:

<lib dir="E:/WindRiver/Solr/apache-solr-3.6.0/dist/" regex="apache-solr-dataimporthandler-\d.*\.jar" />

3. 同時在solr/conf目錄下面新建data-config.xml

  1. <dataConfig>
  2. <dataSourcetype="JdbcDataSource"
  3. driver="com.mysql.jdbc.Driver"
  4. url="jdbc:mysql://localhost:3306/test"
  5. user="test"
  6. password="test"
  7. />
  8. <documentname="documents1">
  9. <entityname="documents"
  10. query="select id,title,content,date_added from documents"
  11. deltaImportQuery="select id,title,content,date_added from documents where ID='${dataimporter.delta.id}'"
  12. deltaQuery="select id from documents where date_added > '${dataimporter.last_index_time}'"
  13. deletedPkQuery="select id from documents where id=0">
  14. <fieldcolumn="id"name="id"/>
  15. <fieldcolumn="title"name="title"/>
  16. <fieldcolumn="content"name="content"/>
  17. <fieldcolumn="date_added"name="date_added"/>
  18. </entity>
  19. </document>
  20. </dataConfig>
<dataConfig>
  <dataSource type="JdbcDataSource"
   driver="com.mysql.jdbc.Driver"
   url="jdbc:mysql://localhost:3306/test"
   user="test"
   password="test"
   />
 <document name="documents1" >
        <entity name="documents"
          query="select id,title,content,date_added from documents"
          deltaImportQuery="select  id,title,content,date_added  from documents where ID='${dataimporter.delta.id}'"
          deltaQuery="select id  from documents where date_added > '${dataimporter.last_index_time}'"
          deletedPkQuery="select id  from documents where id=0">
            <field column="id" name="id" />
            <field column="title" name="title" />
            <field column="content" name="content" />
            <field column="date_added" name="date_added" />
        </entity>
  </document>
</dataConfig>

上面指定了數據庫連接路徑。
query 用於初次導入到索引的sql語句。
deltaImportQuery 根據ID取得需要進入的索引的單條數據。
deltaQuery 用於增量索引的sql語句,用於取得需要增量索引的ID。
deletedPkQuery 用於取出需要從索引中刪除文檔的的ID。

4.在schema.xml中指定索引類型

  1. <fieldname="id"type="string"indexed="true"stored="true"required="true"/>
  2. <fieldname="title"type="text"indexed="true"stored="true"termVectors="true"termPositions="true"termOffsets="true"/>
  3. <fieldname="content"type="text"indexed="true"stored="true"termVectors="true"termPositions="true"termOffsets="true"/>
  4. <fieldname="date_added"type="date"indexed="false"stored="true"/>
  5. <P></P>
<field name="id" type="string" indexed="true" stored="true" required="true" />
<field name="title" type="text" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>
<field name="content" type="text" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>
<field name="date_added" type="date" indexed="false" stored="true"/>

(1)遇到如下錯誤

嚴重: org.apache.solr.common.SolrException: undefined field text

需要添加一段

<field name="text" type="text_general" stored="false" indexed="true" multiValued="true"/>

<defaultSearchField>text</defaultSearchField>

<copyField source="title" dest="text"/>
<copyField source="content" dest="text"/>

5.執行URL

http://localhost:8080/solr/dataimport?command=full-import

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章