Drupal solr: Update specific document directly to solr index.

Problem: How to update specific solr index document.

Requirement:

  1. Drupal 7.x
  2. Search API Solr Search
  3. Solr server should enabled and running(Version 4.9 or more)
  4. CURL

Solution:


$update = array(
    'id' => '',// Document id
    "field_name" => array(
        'set' => ''//Value
    )
);
$update = json_encode(array($update));

$ch = curl_init('http://localhost:8983/solr/collection1/update?commit=true');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $update);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
$output = json_decode(curl_exec($ch));
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($responseCode == 200) {
    echo 'SOLR: Updated successfully for id:' . $doc_id . ' (query time: ' . $output->responseHeader->QTime . 'ms).<br>';
} else {
    echo ('SOLR: Can\'t update field for id:' . $doc_id . ', response (' . $responseCode . ') is: ' . print_r($output, true)) . '<br>';
}


Comments

Popular posts from this blog

Install PHP 7.x on Linux Mint 18

Install Redis on ubuntu 14.04, 14.10, 15.04, 16.04 etc

Disable Drupal 8 caching during development