Integrate Apache Solr with OpenCart

Integrate Apache Solr with OpenCart


Nowadays the face of e-commerce is constantly changing. Hence to maintain strong online selling presence we have to offer better services to our end users. In most of the cases customers switch to other e-commerce sites due to poor implementation of site search, search result not being accurate, search result being slow and so on. We can overcome these problems by integrating Apache Solr with the existing e-commerce website.

I have listed some basic steps to integrate Apache Solr with OpenCart.

Assuming that Apache Solr and OpenCart are in place.

Solr Path: C:\solr
OpenCart: C:\wamp\www\opencart

Step1: Connect to OpenCart database and import product data into Apache Solr. For this we can use default Solr schema with some extra fields

  1. Download and add data-config.xml in C:solrexamplesolrconf
  2. Update C:solrexamplesolrconfsolrconfig.xml with following content
    <requestHandlername="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler">
    <lst name="defaults">
    <str name="config">data-config.xml</str>
  3. Download “mysql-connector-java-5.1.15-bin” and copy it into lib folder C:solrexamplesolrlib
  4. Add following extra fields in Solr schema.xml. Useful for search by category and manufacturer
    <field name="catids" type="int" indexed="true" stored="true" multiValued="true"/>
    <field name="manuids" type="int" indexed="true" stored="true" multiValued="true"/>
  5. Import Opencart products into Apache Solr
  6. After that you can set delta import to import updated product data periodically by setting cron job.

Step2: Add following Apache Solr configuration in C:wampwwwopencartconfig.php

define('SOLR_HOSTNAME', 'localhost');
define('SOLR_PORT', '8983');
define('SOLR_INDEXPATH', '/solr');

Step3: Download “solr-opencart-v1.0” library and update the following files.

  1. Put downloaded “phpSolrClient” library under C:wampwwwopencartsystemlibrarysolr
  2. Add following in C:wampwwwopencartsetupstartup.php
    require_once(DIR_SYSTEM . 'library/solr/solrsearch.php');
  3. Add following in C:wampwwwopencartindex.php
    $solrSearch = new SolrSearch(SOLR_HOSTNAME,SOLR_PORT,SOLR_INDEXPATH);
    $registry->set('solrSearch', $solrSearch);

Step4: Update following file to get search result

  1. As per my requirement I have commented following lines in getProducts()
    /*foreach ($query->rows as $result) {
    $product_data[$result['product_id']] = $this->getProduct($result['product_id']);
  2. And have added following lines of code before the above commented lines. You can customize as per your need.
///// SOLR CODE START /////////

//search by category
if(!empty($data['filter_category_id'])) {
	$solrQry = 'catids:'.$data['filter_category_id'];

//search by manu
	$solrQry = 'manuids:'.$data['filter_manufacturer_id'];

//search by query string
if(!empty($data['filter_name'])) {
	$solrQry = $data['filter_name'];

$solrObj = $this->solrSearch->get($solrQry,$data['start'],$data['limit']);
$solrArr = array();
foreach($solrObj->response->docs As $product){
	$solrArr[] = array('product_id'=>$product->id,'rating'=>'');

foreach ($solrArr as $result) {
	$product_data[$result['product_id']] = $this->getProduct($result['product_id']);

///// SOLR CODE END /////////

Step5: Add auto complete to main search box

  1. Download JQuery Auto complete library from JQuery site
  2. Copy jquery.autocomplete.css and jquery.autocomplete.js into C:wampwwwopencartcatalogviewjavascriptautocomplete
  3. Create new JavaScript file “autosuggest.js” under above path, and add following content into that
    $().ready(function() {
    	$("#filter_name").autocomplete("suggester.php", {
    	width: 295,
    	matchContains: true,
    	//mustMatch: true,
    	minChars: 1,
    	//multiple: true,
    	//highlight: false,
    	//multipleSeparator: ",",
    	selectFirst: false

    Where #filter_name is the id of search textbox.

  4. Create new “suggester.php” with following content, and put at C:\wampwwwopencart
    	$term = $_GET['q'];
    	$termQuery = 'http://localhost:8983/solr/suggest?wt=json&q='.$term;
    	$content = file_get_contents($termQuery);
    	$xml = json_decode($content);
    	foreach($xml->spellcheck->suggestions[1]->suggestion AS $suggestion)
    		echo "$suggestionn";

    To run suggest you need to configure Solr with following settings.

  5. Update your C:\solrexamplesolrconfsolrconfig.xml for suggest
    <searchComponent class="solr.SpellCheckComponent" name="suggest">
    <lst name="spellchecker">
    <str name="name">suggest</str>
    <str name="classname">org.apache.solr.spelling.suggest.Suggester</str>
    <str name="lookupImpl">org.apache.solr.spelling.suggest.tst.TSTLookup</str>
    <!-- Alternatives to lookupImpl:
    org.apache.solr.spelling.suggest.fst.FSTLookup   [finite state automaton]
    org.apache.solr.spelling.suggest.fst.WFSTLookupFactory [weighted finite state automaton]
    org.apache.solr.spelling.suggest.jaspell.JaspellLookup [default, jaspell-based]
    org.apache.solr.spelling.suggest.tst.TSTLookup   [ternary trees]
    <str name="field">name</str><!-- the indexed field to derive suggestions from -->
    <float name="threshold">0.005</float>
    <str name="buildOnCommit">true</str>
    <str name="sourceLocation">american-english</str>
    <requestHandler class="org.apache.solr.handler.component.SearchHandler" name="/suggest">
    <lst name="defaults">
    <str name="spellcheck">true</str>
    <str name="spellcheck.dictionary">suggest</str>
    <str name="spellcheck.onlyMorePopular">true</str>
    <str name="spellcheck.count">10</str>
    <str name="spellcheck.collate">true</str>
    <arr name="components">
  6. Include following files in C:\wampwwwopencartcatalogviewthemedefaulttemplatecommonheader.php
    <script type="text/javascript" src="catalog/view/javascript/autocomplete/jquery.autocomplete.js"></script>
    <script type="text/javascript" src="catalog/view/javascript/autocomplete/autosuggest.js"></script>
    <link rel="stylesheet" type="text/css" href="catalog/view/javascript/autocomplete/jquery.autocomplete.css" media="screen" />
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post

How can I resolve Foreign Key constraint ‘SQLSTATE[23000]: Integrity constraint violation: 1452

Next Post

How to Install Apache Solr for Magento Store?

Related Posts