The integration of SOLR and MySQL databases enables the creation of full-scale indexes and incremental indexes.
The rationale is simple: Register SOLR's Dataimporthandler in the SOLR project and configure the MySQL data source and the data query SQL statement. When we use the SOLR background control page or directly access an address (given later), SOLR invokes Dataimporthandler, connects to the database, queries the data based on the SQL statement, and creates the index.
Example SOLR version: solr4.3.1
Contract the installation directory of SOLR, such as E:\environment\solr-4.3.1 for Solr-home.
1. Copy the jar package
Copy the jar containing the Dataimporthandler and the MySQL database driver package to the Solr-home/dist directory. By default, the two jar packages required for Dataimporthandler already exist in this directory. The name of the jar package for the SOLR version is: Solr-dataimporthandler-4.3.1.jar, Solr-dataimporthandler-extras-4.3.1.jar
2. Modify the Solr-home\example\solr\collection1\conf\solrconfig.xml file
A. Add the required jar-Package reference configuration:
HTML code
- <Lib dir=". /.. /.. /dist/" regex="solr-dataimporthandler-.*\.jar " />
- <Lib dir=". /.. /.. /dist/" regex="mysql-connector-java-.*\.jar " />
B. Add Resulthandler Configuration
HTML code
- <requesthandler name="/dataimport"
- class="Org.apache.solr.handler.dataimport.DataImportHandler">
- <lst name="Defaults">
- <str name="config">data-config.xml</str>
- </lst>
- </requesthandler>
3. Create the Data-config.xml file in the Solrconfig.xml's sibling directory, configure the database connection and SOLR's correspondence with MySQL data and query statements.
HTML code
- <dataconfig>
- <dataSource type="Jdbcdatasource" driver="Com.mysql.jdbc.Driver "
- url="Jdbc:mysql://127.0.0.1/dbname" user="root" password="root"/>
- <document name="Search_object">
- <entity name="talent"
- query= "SELECT ID, Gender, QQ, MSN, Anualsalary, UpdateTime, Adduserid
- From talent ">
- <field column= "anualsalary" name="Salary"/>
- <field column="Adduserid" name= "userId"/>
- <!--Talent names-->
- <entity name="Talent_name" query="Select value from Talentname where talentid= ' ${ Talent.id} ' ">
- <field column="value" name="name"/>
- </Entity>
- </Entity>
- </Document>
- </dataconfig>
4. Restart SOLR.
Bulk Import (Full-import):
Http://
Incremental import (Delta-import):
Http://
Import State Query (status):
Http://
Reload configuration file (Reload-config):
Http://
Terminate import (Abort):
Http://
SOLR simple integration with MySQL