Brian Powell
Brian Powell

Reputation: 3411

solr atomic updating via URL

How do I perform an update to my solr table using a basic http request? I can pull data using something like this:

http://localhost:8983/solr/database/select?q=id:6686 

But what if I want to change a record that is already in the system? I have found some documentation here, that claims I should be able to do something that's equivalent to this:

update solr set name = 'brian' where id = 6686, but I cannot find anything on a URL to call to do this... something like:

http://localhost:8983/solr/database/update?q=id:6686&set=name:brian

I'm using solr 5.4.1.

Upvotes: 0

Views: 1058

Answers (2)

Brian Powell
Brian Powell

Reputation: 3411

I wrote a script that solves this. I hope this is useful for someone else in the future...

function solr_update( $id, $value ) {
  $ch = curl_init("http://localhost:8983/solr/core/update?commit=true");

  $data = array(
            "id" => $id,
            "user_name" => array(
              "set" => $value),
            );

  $data_string = json_encode(array($data));          

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  curl_setopt($ch, CURLOPT_POST, TRUE);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); 
  curl_exec($ch);

}


solr_update($id, $value);

In this example, if I set $id = 20522 and $value = "Brian", the solr_update function will set the user_name field to Brian where the id is 20522.

Upvotes: 1

AR1
AR1

Reputation: 5005

Solr is able to perform atomic updates since its 5.1 version I believe. A complete example covering both update over http and SolrJ can be found here. Moreover, please notice that:

  1. The field named as id in the example, must be replaced with your unique key (check your schema.xml) and
  2. If you're looking for some more specific use case, dig more stackoverflow questions as there are many relevant.

Upvotes: 0

Related Questions