Solr: How to update a document with unique ID

2019-08-13 18:27发布

问题:

Could you please help. I have to update a Solr document by its unique identifier. Say, I have a document like:

    {
      "text": "qwe",
      "id": "01a3aa6db06d39e8",
      "_version_": 1471599607983112200
    }

I want to update the field "phrase", so I POST the following to 127.0.0.1:8983/solr/update/?commit=true:

    [
      {
        "id" : "01a3aa6db06d39e8",
        "text" : {"set":"qwe. updated"}
      }
    ]

Solr says 400 Bad Request and returns the following:

    {
      "responseHeader":
        {
          "status":400,"QTime":0},
          "error":{"msg":"Document contains multiple values for uniqueKey field: id=[01a3aa6db06d39e8, 0000000000000000]","code":400}
        }
    }

How the document containing a unique key can be properly updated?

回答1:

Try this one.

{"id":"01a3aa6db06d39e8","text":"hello world , this is an update"}

If you want to make text as a multidimensional array like mongodb, that cannot be accommodate in solr but there are ways to it, a bit messy.

If you are using a script to update a doc and I think you are using a loop include the commit and the instantiate inside the while loop like this one on PHP:

while($row    = mysqli_fetch_assoc($result))
{
    $client = new SolrClient($options);

    $doc = new SolrInputDocument();
    $doc->addField('id', $row['id']);       
    $doc->addField('date', '2015-06-01T00:00:0.0Z');
    $doc->addField('name', $row['name']);       
    $updateResponse = $client->addDocument($doc);
}   
$client->commit();


标签: solr