Import data from a database into the SOLR index library __ Database

Source: Internet
Author: User
Tags solr

Some of the configuration and features of SOLR are described in the previous article, and this document begins with how SOLR is used specifically. import data from a database into an index library

In most applications, the data in the database is mainly used, so this step is very important.
The directory structure is now shown in the figure:

In the SOLR background management interface

DataImport is responsible for importing data from the database into the index library, although some related configuration is required before importing.

1, the need for the jar package

Also need the MySQL driver package

Put these 3 jar packs under E:\solr\solrhome\collection1\lib

2. Add a RequestHandler node at the back of the Solrconfig.xml

<requesthandler name= "/dataimport" 
    class= "Org.apache.solr.handler.dataimport.DataImportHandler" >
    <lst name= "Defaults" >
      <str name= "config" >data-config.xml</str>
    </lst>
</ Requesthandler>  

Where Data-config.xml refers to the configuration information about the database to be imported.

2, create under the E:\solr\solrhome\collection1\conf
Data-config.xml file

<?xml version= "1.0" encoding= "UTF-8"?>  
<dataConfig>   
<datasource type= "Jdbcdatasource"   
          Driver= "Com.mysql.jdbc.Driver"   
          url= "Jdbc:mysql://localhost:3306/lucene"   
          user= "root"   
          password= "root" "/>   
<document>   
    <entity name=" Product "query=" select Pid,name,catalog_name,price,description, Picture from Products >
         <field column= "pid" name= "id"/> <field column= " 
         name" Name= "Product_Name" /> 
         <field column= "catalog_name" name= "Product_catalog_name"/> <field column= 
         "Price" name= " Product_price "/> <field column= description" name= "product_description"/> <field column= 
         " Picture "name=" Product_picture "/> 
    </entity>   
</document>   

</dataConfig>

Database and account password according to their own actual situation to write
You can see that in

query= "SELECT pid,name,catalog_name,price,description,picture from Products"

Specifies the index to be saved in the index library of your own.

<field column= "pid" name= "id"/> 
 

You can configure the mapping relationship between the domain name in the index and the database field, where column is the field name and name is the domain name.

3, configure the custom field in the Schema.xml
First of all to configure the IK word breaker, can not refer to the following article
Installation and use of SOLR (ii)

Then add the custom field later

<field name= "Product_Name" type= "Text_ik" indexed= "true" stored= "true"/> <field "name="  type= "float" indexed= "true" stored= "true"/>
   <field name= "product_description" type= "Text_ik" indexed = "true" stored= "false"/>
   <field name= "product_picture" type= "string" indexed= "false" stored= "true"/>
   <field name= "Product_catalog_name" type= "string" indexed= "true" stored= "true"/> <field name=
   " Product_keywords "type=" Text_ik "indexed=" "true" stored= "false" multivalued= "true"/> <copyfield "source=
   " Product_Name "dest=" Product_keywords "/> <copyfield source=" product_description "dest=" Product_keywords "
   />

This inside name is consistent with the name in Data-config.xml. Type using the IK word breaker definition

Type= "Text_ik"

4, restart Tomcat
Select Collection1 Click DataImport

5, click to execute, you can import data into the index library.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.