Tuesday, December 23, 2014

Various ways to delete documents in Solr

Php-Solr provides following two methods to delete the record.
1. deleteByQuery : Using deleteByQuery you can deletes all documents matching the given query. This will erase the entire index if we pass ':'. You can remove a record having id 20 using following query. You just need to pass one of the field name defined in schema.xml followed by : followed by the value of the field.

$client->deleteByQuery("*:*");
$this->solrClient->deleteByQuery('id:'.$entryId);
$this->solrClient->deleteByQuery('field name: field value');
2. deleteById : This deletes the document with the passed id. Id should be the uniqueKey field which is declared in the schema.xml file. We need to commit after delete query otherwise you see the record in solr. Use following solr delete query to delete all the records, except 1, 12 or 123.

$this->solrClient->deleteById('id:'.$recordId);
$solr->solrClient->deleteById("*:* -id:(1 OR 12 OR 123)") After that you can see 3 records are getting removed from your solr core.
3. deleteByIds : This deletes the documents with the passed array of ids. Id should be the uniqueKey field which is declared in the schema.xml file. Use following solr delete query to delete multiple multiple documments. Array should be in an indexed array.

$docIds = array(120, 121,122,10202,12002);
$this->solrClient->deleteByIds($docIds);
$solr->solrClient->deleteById("*:* -id:(1 OR 12 OR 123)") After that you can see 3 records are getting removed from your solr core.

Using Curl
You can delete all the entire index using Curl as well.

curl http://mysolrdomain.com/solr/collection1/update?commit=true -H "Content-Type: text/xml" --data-binary '*:*'
Web interface
If you simply needt to delete the records from your Solr index using the web interface, below is the code snippet that allows you do so: This delete documents where the id field matches 29999. If you want to delete solr records which matches more than one field, just add another query:

http://hostname/solr/update?stream.body=
id:29999&commit=true

http://hostname/solr/update?stream.body=
id:29999
name:amol&commit=true
If you want to delete all items in the index, just use this query:

*:*
Empty data
Hostname/solr/update?stream.body=*:*&commit=true
Empty data on specific condition update?stream.body=(Fieldname : FieldValue)&commit=true