Solr is a open source standalone enterprise search server. It comes with great features like full-text search, auto suggest, faceted search, database integration and many more. To know more you can visit http://lucene.apache.org/solr/.

Solr extension is a light-weight, very fast and feature-rich library which allows to communicate with the Apache Solr Server in PHP. To know more solr-php methods visit http://php.net/manual/en/book.solr.php

We have been using Apache Solr in one of our service project. The project is a website which is basically collection of artefacts, posters and other memorabilia from across the globe, importantly India. It currently has about 75,000 objects in our database. In addition, there are roughly 100,000 items in ‘masterlists’. Within the masterlists we have the list of people, films etc, which are the related data for the objects.

 

Joomla + Solr – Integration

We have used Zoo component and RI Pro extension for data management. We have also developed parametric search component that allows to select the zoo fields & also takes care of solr update/delete documents. During this we found the various ways to delete the records from Solr.

b2ap3_thumbnail_Parametric_search_manager-.png

Solr delete queries

We used delete queries to remove data from solr on after the record gets deleted from Zoo. We have also removed entire index while reindexing. Below is the list of Php-Solr methods to delete the records from Solr.

$options = array
(
        'hostname'        => SOLR_SERVER_HOSTNAME,
        'login'                => SOLR_SERVER_USERNAME,
        'password'        => SOLR_SERVER_PASSWORD,
        'port'                => SOLR_SERVER_PORT,
);
$solrClient = new SolrClient($options)

 deleteByQuery

 Using deleteByQuery you can deletes all documents matching the given query. This removes the entire index if we pass ':'.

$solrClient->deleteByQuery("*:*");

Single record can be removed using this method. You just need to pass one of the field name defined in schema.xml followed by collon and the value of the field. You can read about basic solr query syntax here. You can also use deleteById method if you want to remove record based on id.

$solrClient->deleteByQuery('title:'.$title);
$solrClient->deleteByQuery('field name: field value');

 deleteById

This deletes the document with the passed ids. Id should be the uniqueKey field declared in schema.xml. We need to commit after delete query otherwise you see the record in solr.

$solrClient->deleteById('id:'.$recordId);
$solrClient->deleteById("*:* -id:(1 OR 12 OR 123)")

 Above solr delete query deletes all the records, except 1, 12 or 123.

deleteByIds

You can use this query to remove multiple records from Solr. This deletes the documents of passed array of ids. Array should be in an indexed array. Use following solr query to delete multiple documents. Using this you can avoid by writing deleteById query in a loop.

$docIds = array(120, 121,122,10202,12002);
$this->solrClient->deleteByIds($docIds);
$solr->solrClient->deleteById("*:* -id:(1 OR 12 OR 123)")

 After this you will be able to see 3 records are getting removed from your solr core.

Solr is very powerful and easy to use search engine which can be used for any search project. We could also consider solr as database replacement where we can get great page speed. Using Php-Solr library, it fits very easily into any PHP project. This blog post covers multiple ways to delete the index/documents from Solr.

Note: This Blog is purely for developers!