einstein
einstein

Reputation: 13850

How do I update a document in Solr PHP?

Hi can I update a document in Solr PHP without deleting it first and adding a whole new document?

Upvotes: 3

Views: 3609

Answers (3)

Waqleh
Waqleh

Reputation: 10181

This is how I do it:

include "bootstrap.php";
$options = array
(
    'hostname' => SOLR_SERVER_HOSTNAME,
    'login' => SOLR_SERVER_USERNAME,
    'password' => SOLR_SERVER_PASSWORD,
    'port' => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
// Find old Document
$query->setQuery('id:1000012');
$query->setStart(0);
$query->setRows(1);
$query_response = $client->query($query);
// I had to set the parsemode to PARSE_SOLR_DOC
$query_response->setParseMode(SolrQueryResponse::PARSE_SOLR_DOC);
$response = $query_response->getResponse();
$doc = new SolrInputDocument();
// used the getInputDocument() to get the old document from the query
$doc = $response->response->docs[0]->getInputDocument();
if ($response->response->numFound) {
    $second_doc = new SolrInputDocument();
    $second_doc->addField('cat', "TESTCAT");
// Notice I removed the second parameter from the merge()
    $second_doc->merge($doc);
    $updateResponse = $client->addDocument($second_doc);
    $client->commit();
}

Upvotes: 3

Stefan
Stefan

Reputation: 2068

This is new in SOLR 4.0 -

As i stated in Update specific field on SOLR index it is now possible to update fields. This is my answer:

Please refer to this document about the "Partial Documents Update" Feature in Solr 4.0

Solr 4.0, being at BETA stage at the time of this posting, will be final and production-ready in about a month, according to the Road Map.

This feature makes it possible to update fields and even adding values to multiValued fields.

Mauricio was right with his answer back in 2010, but this is the way things are today.

Upvotes: 2

Sukumar
Sukumar

Reputation: 3587

When you want to update the document, you just call the function "addDocument" with the same set of compulsory fields. Solr will internally update the document.

Solr does not support updating individual fields in the document, if thats what you are looking for. Source: SOLR-139

Hope it helps!

Upvotes: 5

Related Questions