欢迎投稿

今日深度:

solr连接数据库配置,solr连接数据库

solr连接数据库配置,solr连接数据库


一般要搜索的信息都是被存储在数据库里面的,但是我们不能直接搜数据库,所以只有借助Solr将要搜索的信息在搜索服务器上进行索引,然后在客户端供客户使用。

一、链接数据库

1. SQL配置

拿SQL Server 为例,需要先下载Sql Server的jar包,下载地址:

http://msdn.microsoft.com/en-us/data/aa937724.aspx

解压缩之后将sqljdbc4.jar复制到webapps\solr\WEB-INF\lib下,也就是本例的:

D:\apache-tomcat-7.0.57\webapps\solr\WEB-INF\lib

2. 建立查询

然后在D:\apache-tomcat-7.0.57\webapps\solr\solr_home\collection1\conf下面新建一个配置文件:data-config.xml

然后在本目录下编辑配置文件:solrconfig.xml

找到很多个requestHandler节点,在最下面增加:

<requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> 
    <lst name="defaults"> 
          <str name="config">D:\apache-tomcat-7.0.57\webapps\solr\solr_home\collection1\conf\data-config.xml</str> 
    </lst> 
</requestHandler>

要配置的内容就是刚才新建文件的路径。

3. 将初次下载的solr-4.10.2文件夹下的dist, contrib文件夹复制到TomCat根目录下:

<lib dir="D:\apache-tomcat\contrib\extraction\lib" regex=".*\.jar" /> <lib dir="D:\apache-tomcat\dist\" regex="solr-cell-\d.*\.jar" /> <lib dir="D:\apache-tomcat\contrib\clustering\lib\" regex=".*\.jar" /> <lib dir="D:\apache-tomcat\dist\" regex="solr-clustering-\d.*\.jar" /> <lib dir="D:\apache-tomcat\contrib\langid\lib\" regex=".*\.jar" /> <lib dir="D:\apache-tomcat\dist\" regex="solr-langid-\d.*\.jar" /> <lib dir="D:\apache-tomcat\contrib\velocity\lib" regex=".*\.jar" /> <lib dir="D:\apache-tomcat\dist\" regex="solr-velocity-\d.*\.jar" /> <lib dir="D:\apache-tomcat\dist\" regex="solr-dataimporthandler-\d.*\.jar" />

这个配置是根据本机推导出来的,大家根据自己的实际情况去修改。

5. 将dist文件夹下的

<?xml version="1.0" encoding="UTF8"?> <dataConfig> <dataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost:1433;DatabaseName=MyBookShop" user="sa" password="111"/> <document name="Info" pk="id"> <entity name="zpxx" transformer="ClobTransformer" pk="id" query="SELECT [ID],[Title] as name FROM [MyBookShop].[dbo].[Books]" deltaImportQuery="SELECT [Id],[Title] as name FROM [MyBookShop].[dbo].[Books] where [PublishDate] > '${dataimporter.last_index_time}'" deltaQuery="SELECT id FROM [MyBookShop].[dbo].[Books] where [PublishDate] > '${dataimporter.last_index_time}'"> <field column="id" name="id" /> <field column="name" name="name" /> </entity> </document> </dataConfig>

此配置千万注意。

上述配置说明如下:

     query是获取全部数据的SQL(solr从sql中获取那些数据),多列

     deltaImportQuery是获取增量数据时使用的SQL(数据库新增数据追加到solr的数据),多列

     deltaQuery是获取pk的SQL(数据库新增数据是,追加到solr的数据时的条件,根据id ,条件是最后一次获取的时间,${dataimporter.last_index_time,最后获取的时间}),一列

如需配置多列,步骤如:

1.先配置数据库查询信息data config:

        <entity name="zpxx"  transformer="ClobTransformer" pk="id"
                 query="SELECT [ProductID] as ID,[ShopProductTitle] as title1,[SecondTitle] as title2,[ShopProductDescription] as descr  FROM [dbo].[ShopProduct]"       
                 deltaImportQuery="SELECT [ProductID] as ID,[ShopProductTitle] as title1,[SecondTitle] as title2,[ShopProductDescription] as descr  FROM [dbo].[ShopProduct] where [PublishDate] > '${dataimporter.last_index_time}'"   
                 deltaQuery="SELECT [ProductID] as ID FROM [dbo].[ShopProduct] where [PublishDate] > '${dataimporter.last_index_time}'">            
                <field column="id"      name="id"      /> 
		<field column="title1"      name="title1"      /> 
		<field column="title2"      name="title2"      /> 
                <field column="descr"      name="descr"      /> 
         </entity>

 2. 配置schema信息(约120多行处):

   <field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> 
        
   <field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/>
   <field name="title1" type="text_ik" indexed="true" stored="true"/>
   <field name="title2" type="text_ik" indexed="true" stored="true"/>
   <field name="descr" type="text_ik" indexed="true" stored="true"/>
   <field name="cat" type="string" indexed="true" stored="true" multiValued="true"/>

 

 

 这样查处的结果就如:

    "docs": [
      {
        "title1": "樱桃",
        "title2": "樱桃樱桃",
        "id": "1004",
        "descr": "<img alt=\"\" src=\"9f531.jpg\"><img alt=\"\" src=\"5.jpg\">",
        "_version_": 1503960451691577300
      },

 

保证SQL SERVER配置了外网访问即可。

配置到此基本结束。

二、建立索引

启动Solr,删除全部索引数据:

http://localhost:8080/solr/update/?stream.body=<delete><query>*:*</query></delete>&stream.contentType=text/xml;charset=utf-8&commit=true

停掉Solr,检查下是不是清空了:

insert into [MyBookShop].[dbo].[Books] ( [Title] ,[Author] ,[PublishDate] ,[WordsCount] ,[UnitPrice] ,[ContentDescription] ,[AurhorDescription] ,[EditorComment] ,[TOC] ) SELECT '论苹果的艺术' ,[Author] ,[PublishDate] ,[WordsCount] ,[UnitPrice] ,[ContentDescription] ,[AurhorDescription] ,[EditorComment] ,[TOC] FROM [MyBookShop].[dbo].[Books] where id=4942

新增结果如下: 

wpsE765.tmp

然后执行增量索引:

http://localhost:8080/solr/dataimport?command=delta-import&clean=false&commit=true

再查询:

wpsE766.tmp

发现新结果被查询出来了。

3. 删除数据

  将苹果这条数据删除掉,仍然能够查询出来,执行单条索引删除:

7168&stream.contentType=text/xml;charset=utf-8&commit=true">http://localhost:8080/solr/update/?stream.body=<delete><id>7168</id></delete>&stream.contentType=text/xml;charset=utf-8&commit=true

再次查询就查不到了:

wpsE776.tmp

 

数据库下载

www.htsjk.Com true http://www.htsjk.com/solr/10702.html NewsArticle solr连接数据库配置,solr连接数据库 一般要搜索的信息都是被存储在数据库里面的,但是我们不能直接搜数据库,所以只有借助Solr将要搜索的信息在搜索服务器上进行索引,然后在客户...
相关文章
    暂无相关文章
评论暂时关闭