Menu icon
RedCrackle
Menu icon
Services
01Design Transformation
About UsCase StudiesBlogContact Us

<

Blog post

Apache solr Installation and database indexing

Neerav Mehta

Founder & CEO

In this post, you will learn Apache solr Installation and database indexing.

Apache solr Installation

We learnt about integration of an existing xml file and configuration of solr. Now we will try database integration with MySQL.

For this, we need the MySQL connector which will establish connection between apache solr and mysql: http://www.mysql.com/downloads/connector/j/

  • Extract the zip file and copy the .jar file to apache-solr-3.6.2/example/lib directory

  • Now we need to add “data Import” request handler that will create a url to import data from the database. We need to add a request handler to solrconfig.xml file, and also need to define a file where we will define which table and fields needs to be indexed. The handler code is as follows:

Apache Solr installation

More details are available at http://wiki.apache.org/solr/DataImportHandler

How to integrate it with php?

require_once( 'SolrPHPClient/Apache/Solr/Service.php' );

$solr = new Apache_Solr_Service( 'localhost', '8983', 'solr/');
$ping = $solr->ping();

if ( ! $ping ) {
  echo 'Solr service not responding';
  exit;
}

$offset = 0;
$limit = 10;

$queries = array(
  'test string',
  'sort:post_date desc'
);

foreach ( $queries as $query ) {
  $response = $solr->search( $query, $offset, $limit );

  if ( $response->getHttpStatus() == 200 ) {
    // print_r( $response->getRawResponse() );

    if ( $response->response->numFound > 0 ) {
      echo "$query";

      foreach ( $response->response->docs as $doc ) {
        echo "$doc->id --- $doc->post_title";
      }
    } else {
      echo $response->getHttpStatusMessage();
    }
  }
}

This code will ping to apache server. If the ping is successful, it will try to search a string "test string" in your default search field that you set in solrconfig.xml. The $offset and $limit will define the total number of records. If it finds the result, it will print the id and title of record. Otherwise, it prints the error message.

Neerav Mehta

Neerav Mehta

Founder & CEO

Neerav Mehta is the Founder & CEO of Red Crackle. With sterling qualities, Neerav’s technological acumen is firing a generation of progressive companies on the digital path. With an undergraduate degree in Electrical Engineering from India's most prestigious institution IIT Bombay and having spent seven years developing and contributing to the launch of AMD's innovative line of computer products, Neerav founded Red Crackle where he is lauded for his dynamic and innovative genius.

View all posts

>

Read Next

10 Tips For Entrepreneurs In 2015

10 Tips For Entrepreneurs In 2015

Learn more

10 Ways To Increase Productivity At Work

10 Ways To Increase Productivity At Work

Learn more

30 best WordPress widgets for your site

30 best WordPress widgets for your site

Learn more

Let’s get you started!

Contact Us

>

RedCrackle

Explore

About Us

Services

Contact Us

Our address

5346 Gerine Blossom Dr,

San Jose, CA 95123

USA

Socials

Twitter
LinkedIn

© 2023 RedCrackle. All rights reserved.