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
C:solrexamplesolrconfC:solrexamplesolrconfsolrconfig.xml with following content
<requestHandlername="/dataimport" class="org.apache.solr.handler.dataimport.DataImportHandler"> <lst name="defaults"> <str name="config">data-config.xml</str> </lst> </requestHandler>
C:solrexamplesolrlib<field name="catids" type="int" indexed="true" stored="true" multiValued="true"/> <field name="manuids" type="int" indexed="true" stored="true" multiValued="true"/>
Step2: Add following Apache Solr configuration in C:wampwwwopencartconfig.php
// SOLR
define('SOLR_HOSTNAME', 'localhost');
define('SOLR_PORT', '8983');
define('SOLR_INDEXPATH', '/solr');
Step3: Download “solr-opencart-v1.0” library and update the following files.
C:wampwwwopencartsystemlibrarysolrC:wampwwwopencartsetupstartup.phprequire_once(DIR_SYSTEM . 'library/solr/solrsearch.php');C:wampwwwopencartindex.php
//SOLR
$solrSearch = new SolrSearch(SOLR_HOSTNAME,SOLR_PORT,SOLR_INDEXPATH);
$registry->set('solrSearch', $solrSearch);
Step4: Update following file to get search result
C:wampwwwopencartcatalogmodelcatalogproduct.php
getProducts()
/*foreach ($query->rows as $result) {
$product_data[$result['product_id']] = $this->getProduct($result['product_id']);
}*/
///// SOLR CODE START /////////
//search by category
if(!empty($data['filter_category_id'])) {
$solrQry = 'catids:'.$data['filter_category_id'];
}
//search by manu
if(!empty($data['filter_manufacturer_id'])){
$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
C:wampwwwopencartcatalogviewjavascriptautocomplete
$().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.
C:\wampwwwopencart
<?php
$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";
}
exit;
?>
To run suggest you need to configure Solr with following settings.
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>
-->
</lst>
</searchComponent>
<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>
</lst>
<arr name="components">
<str>suggest</str>
</arr>
</requestHandler>
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" />
Reference: