Using XML, you can send add
, commit
, optimize
, and delete
commands to Solr. Here is an XML sample for sending HTTP POST to Solr; this will add (or replace) documents:
<add overwrite="true"> <doc boost="2.0"> <field name="id">Artist:11650</field> <field name="type">Artist</field> <field name="a_name" boost="0.5">The Smashing Pumpkins</field> <!-- the date/time syntax MUST look just like this --> <field name="a_begin_date">2007-12-31T09:40:00Z</field> </doc> <doc> <field name="id">Artist:11651</field> <field name="type">Artist</field> <field name="a_begin_date">2007-12-31T09:40:00Z</field> </doc> <!-- more doc elements here as needed --> </add>
If you have a field in your schema defined as unique, and the overwrite
attribute is set to true
(the default), an incoming document will replace an existing document when both documents have the same unique field value.
The boost
attribute affects the scoring of search results at query time. Providing a boost value, whether at the document or field level, is optional. The default value is 1.0
, which is effectively a nonboost. Technically, documents are boosted at the field level. The effective boost value for a field is the document boost, multiplied by the field boost value.
Specifying boosts here is called index-time boosting, which is rarely done as compared to the more flexible query-time boosting. Index-time boosting is less flexible because such boosting decisions must be decided at index-time and will apply to all of the queries. You'll learn more about boosting and scoring in Chapter 6, Search Relevancy.
You can delete a document by its unique field and value. Here, we delete two documents:
<delete><id>Artist:11604</id><id>Artist:11603</id></delete>
A query can provide a more flexible way to specify which documents are to be deleted:
<delete><query>timestamp:[* TO NOW-12HOUR]</query></delete>
The previous delete query would delete all documents whose timestamps are older than 12 hours from the current time. More info on querying Solr can be found in Chapter 5, Searching.
The contents of the delete
tag can be any number of id
and query
tags, so you can batch many deletions into one message to Solr.