2

I am using Solr for searching my corpus of web page data. My solr-indexer will create several fields and corresponding values. However some of these fields I want to update more often, like for example the number of clicks on that page. These fields need not be indexable and I don't need to perform a search on these field values. However I do want to fetch them and update them often. I am a newbie in solr so a more descriptive answer with perhaps some running example/code would help me better.

Apurv
  • 4,458
  • 2
  • 21
  • 31

2 Answers2

3

If you are on Solr 4+, yes you can push a Partial Update to Solr index.

For partial update, all fields in your schema.xml need to be stored.

This is how your fields section should look like:

<fields>
  <field name="id" type="string" indexed="true" stored="true" required="true" />
  <field name="title" type="text_general" indexed="true" stored="true"/>
  <field name="description" type="text_general" indexed="true" stored="true" />
  <field name="body" type="text_general" indexed="true" stored="true"/>
  <field name="clicks" type="integer" indexed="true" stored="true" />
</fields>

Now when you send a partial update to one of the fields, eg: in your case the "clicks"; in the background Solr will go and fetch values for all other fields for that document, such as title, description, body, delete old document and will push new updated document to Solr index.

localhost:8080/solr/update?commit=true' -H 'Content-type:application/json' -d '[{"id":"1","clicks":{"set":100}}]

Here is a good documentation on partial updates: http://solr.pl/en/2012/07/09/solr-4-0-partial-documents-update/

Aujasvi Chitkara
  • 939
  • 1
  • 5
  • 13
  • Hi Apurv, I found this discussion which might help you. http://stackoverflow.com/questions/12183798/solrj-api-for-partial-document-update I am not a JAVA guy, I use Solr with PHP. – Aujasvi Chitkara Jun 11 '13 at 17:34
2

Sample SOLR- partial update code:

Prerequisites: The fields need to be stored.

You need to configure update log path under direct update handler

  <updateHandler class="solr.DirectUpdateHandler2">

    <!-- Enables a transaction log, used for real-time get, durability, and
         and solr cloud replica recovery.  The log can grow as big as
         uncommitted changes to the index, so use of a hard autoCommit
         is recommended (see below).
         "dir" - the target directory for transaction logs, defaults to the
                solr data directory.  --> 
    <updateLog>
      <str name="dir">${solr.ulog.dir:}</str>
    </updateLog>
  </updateHandler>

Code:

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.ConcurrentUpdateSolrServer;
import org.apache.solr.client.solrj.impl.HttpSolrServer;
import org.apache.solr.common.SolrInputDocument;
public class PartialUpdate {
    public static void main(String args[]) throws SolrServerException,
            IOException {
        SolrServer server = new HttpSolrServer("http://localhost:8080/solr");
        SolrInputDocument doc = new SolrInputDocument();
        Map<String, String> partialUpdate = new HashMap<String, String>();
        // set - to set a field.
        // add - to add to a multi-valued field.
        // inc - to increment a field.
        partialUpdate.put("set", "peter"); // value that need to be set
        doc.addField("id", "122344545"); // unique id
        doc.addField("fname", partialUpdate); // value of field fname corresponding to id 122344545 will be set to 'peter'
        server.add(doc);
    }
}
Learner
  • 2,303
  • 9
  • 46
  • 81