Solr4.2.0的配置以及与MySQL连接创建document

来源:互联网 发布:闲鱼与淘宝的区别 编辑:程序博客网 时间:2024/06/17 20:50

1)      安装配置:

使用版本:4.2.0   可以到http://lucene.apache.org/solr/下载

配置:将文件解压,

将dis目录下的war文件放入到tomcat  webapp目录下,改名为solr.war; 

第一次启动Tomcat,会报错,因为我们还没有配置solr/home,打开webapp/solr下的web.xml,配置如下:

  <env-entry>
       <env-entry-name>solr/home</env-entry-name>
       <env-entry-value>D:\solr-4.2.0\example\solr</env-entry-value>
       <env-entry-type>java.lang.String</env-entry-type>
    </env-entry>

打开:localhost:8080/slor, 正确显示。

2)      连接数据库

要建立自己的全文检索,一般都需要从数据库导入数据,在原来配置的基础上,增加导入的功能 。

l D:\solr-4.2.0\example\solr\collection1\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>  

l  data-config.xml,内容为数据库的连接信息 (文件dataconfig.xml建在solrconfig所在目录下)

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

<dataConfig> 

<dataSource type="JdbcDataSource"driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test"  user="root" password="123"/> 

 <document name="test">          

   

 <entity name="post" query="select * from post"  deltaImportQuery="SELECT * FROM post WHERE id='${dataimporter.delta.id}'"
 deltaQuery="SELECT id FROM post WHERE time > '${dataimporter.last_index_time}'">
        <field column="id"      name="id"      />               
<field column="name"      name="name"      />  
<field column="post"      name="post"      />  
<field column="time"      name="time"   />                 
    </entity>  

</document> 

</dataConfig> 

l  schema.xml文件中增加 

<field name="name"type="string" indexed="true" stored="true"/>

<field name="post"        type="string"indexed="true" stored="true" />  

<field name="time"        type="date"indexed="true" stored="true" /> 

这里会存在的问题是: <uniqueKey>name</uniqueKey>的处理;

l 添加jar包: 添加到webapps\solr\WEB-INF\lib 目录下

mysql-connector-java-5.1.6.jar 

solr-dataimporthandler-4.2.0.jar

solr-dataimporthandler-extras-4.2.0.jar

后两个jar包在D:\solr-4.2.0\dist 下

l http://localhost:8080/solr/dataimport?command=full-import即可以进行导入数据 
l  http://localhost:8080/solr/#/collection1/query可以进行查询

  当数据表中有新的数据插入时,访问

 http://localhost:8080/solr/dataimport?command=delta-import  可以建立对新数据的索引。但是这里的问题是:1)在数据表里必须有一个字段是维护记录的修改时间的,因为在Solr中会根据这个时间与dataimporter.last_index_time比较来决定是否建立索引。是否可以用其他的字段来完成这一功能我还没有找到。2):当数据库中是删除的操作时,Solr这边怎么知道呢? 3)如何在数据库变化时,自动访问

 http://localhost:8080/solr/dataimport?command=delta-import  来维护Solr对DB的索引?

原创粉丝点击