solr学习笔记--从数据库中创建索引dataimport

solr4.10 

1、需要准备的jar包;数据库驱动jar包  mysql-connector-java-5.1.17-bin.jar,solr-dataimporthandler-4.10.0.jar;

solrconfig.xml 中引入

<lib dir="F:/SolrHome/multicore/lib" regex=".*\.jar" />

2、在配置文件F:\SolrHome\multicore\core1\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> 

3、配置数据源,新建文件 F:\SolrHome\multicore\core1\conf\data-config.xml

配置如下

-<dataConfig>

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

-<document>

<entity query="select aid,title,author from article" name="article"> </entity>

</document>

</dataConfig>

Index the fields in different names

Step: 1 Change the data-config as follows :

<dataConfig>  <dataSource type="JdbcDataSource" 
              driver="com.mysql.jdbc.Driver"              url="jdbc:mysql://localhost/dbname" 
              user="user-name" 
              password="password"/>  <document>    <entity name="id" 
            query="select id,name,desc from mytable">       <field column="id" name="solr_id"/>       <field column="name" name="solr_name"/>       <field column="desc" name="solr_desc"/>    </entity>  </document></dataConfig>

Step 2 : This time the fields will be written to the solr fields 'solr_id', 'solr_name', solr_desc'. You must have these fields in the schema.xml. Step 3 :

Index data from multiple tables into Solr

Step: 1 Change the data-config as follows :

<dataConfig>  <dataSource type="JdbcDataSource" 
              driver="com.mysql.jdbc.Driver"              url="jdbc:mysql://localhost/dbname" 
              user="user-name" 
              password="password"/>  <document>    <entity name="outer" 
            query="select id,name,desc from mytable">       <field column="id" name="solr_id"/>       <field column="name" name="solr_name"/>       <field column="desc" name="solr_desc"/>       <entity name="inner"               query="select details from another_table where id ='${outer.id}'">              <field column="details" name="solr_details"/> 
       </entity>    </entity>  </document></dataConfig>

Step 2: The schema.xml should have the solr_details field

 

4、配置各字段和中文分词器;在F:\SolrHome\multicore\core1\conf\schema.xml

<schema version="1.1" name="example core one">

<fieldtype name="string" omitNorms="true" sortMissingLast="true" class="solr.StrField"/>

<fieldType name="int" omitNorms="true" class="solr.TrieIntField" positionIncrementGap="0" precisionStep="0"/>

<fieldType name="long" class="solr.TrieLongField" positionIncrementGap="0" precisionStep="0"/>

<fieldType name="text" class="solr.TextField">

<analyzer class="org.wltea.analyzer.lucene.IKAnalyzer"/>

</fieldType>

<!-- general -->

<field name="aid" required="true" stored="true" indexed="true" type="int"/>

<field name="title" stored="true" indexed="true" type="text"/>

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

<field name="_version_" stored="true" indexed="true" type="long"/>

<!-- field to use to determine and enforce document uniqueness. -->

<uniqueKey>aid</uniqueKey>

<!-- field for the QueryParser to use when an explicit fieldname is absent -->

<defaultSearchField>title</defaultSearchField>

<!-- SolrQueryParser configuration: defaultOperator="AND|OR" -->

<solrQueryParser defaultOperator="OR"/>

</schema>

5、启动tomcat ,http://localhost:8983/solr/#/core1/dataimport//dataimport ,选择entity article  ,然后execute;结果如截图;


solr学习笔记--从数据库中创建索引dataimport
 

相关推荐