<field name="id" type="number" indexed="true" stored="true" required="true" />
<field name="title" type="text_en" indexed="true" stored="false"/>
<field name="submit_date" type="date" indexed="true" stored="false" />
<field name="views" type="number" indexed="true" stored="false" />
all
stored="false" attributes must be changed to
stored="true" so that your schema file looks like
this:
<field name="id" type="number" indexed="true" stored="true" required="true" />
<field name="title" type="text_en" indexed="true" stored="true"/>
<field name="submit_date" type="date" indexed="true" stored="true" />
<field name="views" type="number" indexed="true" stored="true" />
Out of the box, atomic updating should work correctly if your schema file is configured correctly. If atomic updating does not work, it might be helpful to view the
caveats and limitations section of
the Solr wiki for other helpful information.
public void incrementDocumentByValue(String solrServerUrl, String documentId,
String fieldToUpdate, int incrementValue)
{
// create the SolrJ client
HttpSolrServer client = new HttpSolrServer(solrServerUrl);
// create a new document
SolrInputDocument document = new SolrInputDocument();
// create a map of the operation to be performed
Map<String, Object> operation = new HashMap<>();
// insert the operation into the map
operation.put("inc", new Integer(incrementValue));
// unique identifier
SolrSearchUtil.addToDocument(document, "id", documentId);
// add the atomic update
SolrSearchUtil.addToDocument(document, fieldToUpdate, operation);
// send it to the solr server and shutdown
client.add( document );
client.shutdown();
}
The important part of the code above is the
operation variable, which is java.util.Map where the
key is the operation to be performed and the
value is the number you want to increment the document's field by. So if we wanted to increase the views of a document by 5, the code might look like:
incrementDocumentByValue("localhost:8983/solr", "unique-id-0001", "views", 5);
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)