solr全文检索(第二篇 solr的实例)--源自技术

来源:互联网 发布:html标签中使用js变量 编辑:程序博客网 时间:2024/05/18 03:09

这里我使用的数据库是MySQL,首先集成MySQL

1.创建表

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

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

[html] view plain copy
  1. <requestHandler name="/dataimport"   
  2.      class="org.apache.solr.handler.dataimport.DataImportHandler">  
  3.     <lst name="defaults">  
  4.       <str name="config">data-config.xml</str>  
  5.     </lst>  
  6. </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

[html] view plain copy
  1. <dataConfig>  
  2.   <dataSource type="JdbcDataSource"  
  3.    driver="com.mysql.jdbc.Driver"  
  4.    url="jdbc:mysql://localhost:3306/test"  
  5.    user="test"  
  6.    password="test"  
  7.    />  
  8.  <document name="documents1" >  
  9.         <entity name="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.             <field column="id" name="id" />  
  15.             <field column="title" name="title" />  
  16.             <field column="content" name="content" />  
  17.             <field column="date_added" name="date_added" />  
  18.         </entity>  
  19.   </document>  
  20. </dataConfig>  

上面指定了数据库连接路径。
query 用于初次导入到索引的sql语句。
deltaImportQuery 根据ID取得需要进入的索引的单条数据。
deltaQuery 用于增量索引的sql语句,用于取得需要增量索引的ID。
deletedPkQuery 用于取出需要从索引中删除文档的的ID。

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

[html] view plain copy
  1. <field name="id" type="string" indexed="true" stored="true" required="true" />  
  2. <field name="title" type="text" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>  
  3. <field name="content" type="text" indexed="true" stored="true" termVectors="true" termPositions="true" termOffsets="true"/>  
  4. <field name="date_added" type="date" indexed="false" stored="true"/>  
  5. <p> </p>  

(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
0 0
原创粉丝点击