zoukankan      html  css  js  c++  java
  • Solr数据库连接之多表关联

    Solr环境配置好后,有很多时候我们需要把数据库里的数据添加到索引里,这时就需要配置跟数据库的连接,下面我们看配置的步骤。

    1. 配置 solrconfig.xml  (在slor 主目录 core conf下面 我的地址 :D:SolrHomecollection1confsolrconfig.xml )

        在根目录下加入如下配置,其中 data-config.xml 是我们要配置的数据库连接文件路径。

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

    2. 创建 data-config.xml 文件

    <?xml version="1.0" encoding="UTF-8"?>  
    <dataConfig>
        <dataSource driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://127.0.0.1;DatabaseName=QuestionBank" user="sa" password="sa"/>
        <document name="Question-High">            
            <entity name="Question"  transformer="HTMLStripTransformer" pk="id"
                     query="select * from [dbo].site_HighSchoolQuestion  where IsEnabled=1"  
                     deletedPkQuery="SELECT id FROM [dbo].site_HighSchoolQuestion where IsEnabled=0" 
                     deltaImportQuery="select * from [dbo].site_HighSchoolQuestion where id='${dih.delta.id}'"    
                     deltaQuery="SELECT id FROM [dbo].site_HighSchoolQuestion where ModifyTime > '${dataimporter.last_index_time}'">             
                        <field column="Id"      name="Id"      />  
                        <field column="Title"      name="Title"      />     
                        <field column="TitleText"      name="TitleText" />
                        <field column="Content"      name="Content" stripHTML="false"/> 
    <field column="SubjectInfo" name="SubjectInfo" stripHTML="false"/> <entity name="Subject" pk="id" query="SELECT [Code] as SubjectCode FROM [dbo].[site_Subject] where Id ='${Question.SubjectInfo}'" deltaQuery="SELECT id FROM [dbo].site_Subject where ModifyTime > '${dataimporter.last_index_time}'" parentDeltaQuery="SELECT id FROM [dbo].site_HighSchoolQuestion where SubjectInfo ='$(Subject.id)'"> <field column="SubjectCode" name="SubjectCode" /> </entity> </entity> </document> </dataConfig>

    dataSource 节点 配置数据库 连接字符串http://i.cnblogs.com/EditPosts.aspx?opt=1

    entity 节点即Solr中对应的实体 其中属性

    transformer:转换器

    pk :使用的主键

    query : 是获取全部数据的SQL  相当于在solr dataimport 页面执行 full-imoport操作时调用的语句 我这里只需要 启用的数据所以加了条件 where IsEnabled=1

    deltaImportQuery: 获取增量数据时使用的SQL 语句 指定要获取哪些列 注意后面的条件  where id='${dih.delta.id}' 这里的 ${dih.delta.id} 是内置变量 它的值跟下面的属性关系密切 它是 deltaQuery 里符合条件的ID

    deltaQuery :增量更新时获取PK的SQL语句,${dataimporter.last_index_time}  的值是solr 最后创建索引时间,增量更新是根据这个时间跟数据库更新时间进行对比,选择数据库更新时间在solr更新时间之后的进行更新。

    deletedPkQuery : 在某些情况下,可能删除数据并不是物理删除,而只是改变数据库某个值,做个标记,在数据库做了删除标记后,记录还是存在的。这时我们是不需要这些数据进入索引的,

                             那么我们可以通过这个语句对做过删除标记的索引删除(增量更新时起作用)。

             注意:这个要小心性能问题,它会针对每个 deltaQuery 查询语句里的 id 循环判断 是否在已删除列表里,所以,如果已删除的列表有 200 个

                             已更新的 ID列表有 10000 个,那么 要进行 200 * 10000 次循环。

    field 节点是对应的数据库中的列  其中 column 属性 查询语句中对应的列名, name 属性 solr实体首页的列名。

    如果有表关联就出现了下面的配置

    <entity name="Subject"  pk="id" query="SELECT [Code] as SubjectCode FROM [dbo].[site_Subject] where  Id ='${Question.SubjectInfo}'"
                                deltaQuery="SELECT id FROM [dbo].site_Subject where ModifyTime > '${dataimporter.last_index_time}'"
                                parentDeltaQuery="SELECT id FROM [dbo].site_HighSchoolQuestion where SubjectInfo ='$(Subject.id)'">                          
                             <field column="SubjectCode" name="SubjectCode" />
                        </entity>

    这里 subject 是 site_HighSchoolQuestion 表的关联表,我这里要用到它的 SubjectCode 列,所以在它里面配置了 SubjectCode的 field 节点。

    entity 节点 name 节点同上,给这个实体取个名字,方便下面引用。

    query : 查询语句这里的条件是 id = QuestionSubjectInfo的值。

    deltaQuery: 增量更新时获取PK的SQL语句 同上

    parentDEltaQuery: 获取父实体对象PK的语句 如果 Subject 更新了会通知  Question 更新,而更新就是通过PK值进行的,注意我这里的条件 where SubjectInfo ='$(Subject.id)' 因为我这里两张表是通过 SubjectInfo 关联的

    这里有2点要注意的:

    1. 子entiyt 必须要设置 PK 不然的话是没法更新的

    2. 性能问题, 这里如果 deltaQuery 语句查出的是 10000 条记录   parentDeltaQuery 语句就要执行 10000次 。

    3. 如果还不能更新请检查你的 SQL 语句

    下面就要配置  同目录下的 schema.xml 了。这里是简化的

    <?xml version="1.0" encoding="UTF-8" ?>
    <schema name="question-heigh" version="1.5">  
       <!-- If you remove this field, you must _also_ disable the update log in solrconfig.xml
          or Solr won't start. _version_ and update log are required for SolrCloud
       --> 
       <field name="_version_" type="long" indexed="true" stored="true"/>
       
       <!-- points to the root document of a block of nested documents. Required for nested
          document support, may be removed otherwise
       -->
       <field name="_root_" type="string" indexed="true" stored="false"/>
        
        <!-- 分词器配置 -->
        <fieldType name="text_ik" class="solr.TextField">
            <analyzer type="index" isMaxWordLength="false" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
            <analyzer type="query" isMaxWordLength="true" class="org.wltea.analyzer.lucene.IKAnalyzer"/>
        </fieldType>
        <!--  HTML标签过滤  -->
        <charFilter class="solr.HTMLStripCharFilterFactory"/>
    
        <!-- 数据库字段 -->
       <field name="Id" type="string" indexed="true" stored="true" required="true" multiValued="false" />
       <!-- 启用分词 -->
       <field name="Title" type="text_ik" indexed="true" stored="true" />   
       <field name="TitleText" type="string" indexed="true" stored="false" />   
       <field name="Content" type="text_ik" indexed="true" stored="true" />
       <!-- 这里subjectInfo 如果是1对多的关系 multiValued 的值要设置为 true,我这里是 1对1 -->
       <field name="SubjectInfo" type="string" indexed="true" stored="true" />   
       <field name="SubjectCode" type="string" indexed="true" stored="true" multiValued="false"/>
       <!-- 注意主键设置 -->
       <uniqueKey>Id</uniqueKey> 
       <fieldType name="string" class="solr.StrField" sortMissingLast="true" /> 
       <!-- 随机排序用的 -->
       <fieldType name="random" class="solr.RandomSortField" indexed="true" />  
       <!-- 默认查询域 -->
        <defaultSearchField>Title</defaultSearchField>
        <solrQueryParser defaultOperator="AND"/>
    </schema>

    配置就做好了,别忘了添加连接数据库的 jar 包,下载后复制到部署目录下

    以我自己的为例: D:apachetomcatapache-tomcat-7.0.59webappssolrWEB-INFlib

    同时还需要 solr安装目录里面 D:solr-4.10.4dist 下 solr-dataimporthandler-4.10.4.jar 、solr-dataimporthandler-extras-4.10.4.jar 这两个jar包一起拷过去。

    试试,是不是可以跑索引了。

  • 相关阅读:
    如何迅速的修改个人信息
    Workflow History List中的字段含义
    Maximum File Size for Crawling Search Services
    SPUtility
    介紹好用元件:Microsoft Chart Controls for .NET 3.5 (转)
    使用Reflector.FileDisassembler反编译DLL
    Webpart开发时注意事项
    SharePoint中的时间问题(转)
    MOSS添加删除导航结点代码示例(SPNavigationNode)
    DbHelper SQL数据操作类【DBHelper.CS】
  • 原文地址:https://www.cnblogs.com/fengh/p/4810879.html
Copyright © 2011-2022 走看看