How to update the document on solr 6.0?

2019-09-06 06:59发布

问题:

I am using solr 6.0 version.

This is my data

{
    "id" : "14",
    "solrid" : "solr|school|14",
    "name" : "test update solr 14",
    "status" : "pending",
    "state" : "Andhra Pradesh",
    "board" : "CISCE",
    "updated" : "2016-05-26T02:24:25Z",
    "pincode" : "0"
}

I want to update the data on document as per id. example i want to change the name

$doc = $update->createDocument();


   $doc["id"] =$id;
            $doc["name"]="school";
            $update->addDocument($doc);
            $update->addCommit();
            $client->update($update);

This code is correct? Or i want to use other flow. PHP Solarium code.

回答1:

Yes, basically the flow is correct. You can always check the solarim examples gist at https://gist.github.com/basdenooijer/894286.

Alternatively you can do something like this:

$update = $client->createUpdate();
$document = $update->createDocument();
$document->setKey('id', $id);
foreach ($data as $field => $value) {
    if ($field == 'id') {
        continue;
    }
    $document->setField($field, $value, null, 'set');
}
$update->addDocument($document, true, 1500);
$result = $client->update($update);


标签: solr solarium