Ashwin Hegde
Ashwin Hegde

Reputation: 867

How to connect Apache Solr with mysql

I want to connect Apache Solr Search Server with mysql server so that i can fetch filtered data from my database and convert it to JSON or XML.

Upvotes: 7

Views: 18044

Answers (4)

user2200180
user2200180

Reputation: 61

I also had the same issue and it is not easy to find simple tutorial for this. Anyway I found following tutorial and it was useful for me. http://lasithtechavenue.blogspot.com/2013/11/crawling-mysql-database-with-apache-solr.html

If I briefly explain steps.

  1. Make a config file on solr/collection1/conf and add database information and table column information on it.

  2. Point that config file from solr/collection1/conf/solrconfgig.xml

  3. Add unknown fields to schema

  4. Add dataimport and mysql related dependencies

Upvotes: 4

Shrey Shivam
Shrey Shivam

Reputation: 1127

Step 1: Make a new xml file db-data-config.xml and put the following content:

    <dataConfig>
    <dataSource type="JdbcDataSource" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://`localhost`/database_name" user="" password=""/>
    <document>
    <entity name="table_name" query="select * from table_name" >
    <field name="solr_field_map" column="table_column_name" />
    <field name="name" column="name" />
    </entity>
    <entity name="emp" query="select id from emp">
<field name="id" column="id" />
    </entity>
    </document>
    </dataConfig>

Step 2: In solrconfig.xml,add:

 <requestHandler name="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
      <str name="config">db-data-config.xml</str>
    </lst>
  </requestHandler>

Step 3: Use command to import data from your mysql source to solr:

http://localhost:8983/solr/db/dataimport?command=full-import

if you want to modify the fields before indexing to solr, then you can make your transformers on entity in db-data-config.xml.Transformers are explained here

Upvotes: 15

pigletfly
pigletfly

Reputation: 1081

You can refer to http://wiki.apache.org/solr/DataImportHandler#Oracle_Example , configre your datasource ,config your data-config.xml

Upvotes: 3

Leon Revill
Leon Revill

Reputation: 2010

The way I have done it in the past it to just create an indexing script. The script runs as a nightly process and extracts the mySQL data that you want to add to the search index and specifies all the information that solr will need to display the relevant search results.

Additionally you can add index items as the database is updated, for example if a new user is created, add some code into the register script which insert the new index item.

It is pretty quick and easy to do, the solr documentation is quite good.

http://lucene.apache.org/solr/4_1_0/tutorial.html

Hope this helps.

Upvotes: 2

Related Questions