Jul 152014
Article PHP

This post explains the procedure to follow to load the records in a MySQL table as documents of a collection in the solr search engine, by means of a PHP script that uses the Solarium library.

1. Pre-requisites

The system where data will be imported must already have a working installation of solr and the Solarium library.

The procedures to setup solr and Solarium can be found in our previous posts:

2. Create a solr collection to hold the documents

In the import procedure explained in this post, every record in the source table will become a document: Every field of the table will be converted into a corresponding field in the destination document.

The first step is to create a solr collection, whose “schema.xml” configuration file will define the fields in the destination documents, assigning them a field type compatible with the data type of the corresponding field in the origin table.

The details of the procedure to create a solr collection can be consulted in our previous post “How to create a solr collection

The correspondence between MySQL data types and solr field types is mostly straightforward, as there are in solr field types string, boolean, int, float, double, date, etc. that closely match the MySQL data types.

3. PHP import script

The PHP script used to load the table into solr starts creating a connection to solr:

Next, it creates a connection to the database, and executes a query to retrieve the records from the table. For each record, a solr document is created, filled with the data read, and inserted in the collection:

 Posted by at 10:34 am

 Leave a Reply