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.