1

I'm new in solr and I've been using this thread to do a atomic update

How do I update a document in Solr PHP?

Basically im doing an mysql query then update a document on solr

Question: How to do an atomic update where a specific field matches a field should be matched inside an if statement like this:

if(solr(username.field) == 'john'))
{
  //execute atomic update
} 

so far my code is messy like these:

$query = "SELECT * from User";

    $options = array
    (
        'hostname' => SOLR_SERVER_HOSTNAME,
        'login'    => SOLR_SERVER_USERNAME,
        'password' => SOLR_SERVER_PASSWORD,
        'port'     => SOLR_SERVER_PORT,
        'path'     => SOLR_SERVER_PATH,
    );

    $result   = $mysqli->query($query);
    if($result->num_rows > 0)
    {
        while($row=mysqli_fetch_assoc($result))
        {

            $querySearch = '+username:*'; //query all user that is on solr
            $query       = new SolrQuery();
            $query->setQuery($querySearch);
            $query->setStart(0);
            $query->setRows(10000);
            $client             = new SolrClient($options);
            $query_response     = $client->query($query);             
            $query_response->setParseMode(SolrQueryResponse::PARSE_SOLR_DOC);
            $response = $query_response->getResponse();
            $doc = new SolrInputDocument();
            $counter = $response->response->numFound;
            for($x = 0; $x < $counter; $x++)
            {
                $doc = $response->response->docs[$x]->getInputDocument(); //this gets the old value (refer to thread)
                $docs  = $query_response->getResponse()->response->docs[$x]->username->values; //how I get the value of users

                $second_doc = new SolrInputDocument();

                if($docs == get_product($row['USERNAME']))
                {
                    $second_doc->addField('points', $row['POINTS']); //this suppose to update my solr document with those username found
                }
                else
                {
                    $second_doc->addField('points', "0");
                }
                    $second_doc->merge($doc);
                    $updateResponse = $client->addDocument($second_doc);
                    $client->commit();
            }    
        }
Community
  • 1
  • 1
Liu Jin Long
  • 83
  • 1
  • 10

1 Answers1

0

It should have been

if(!empty($response->response->docs[$x]->username->values[$x]) == get_product($row['USERNAME']))
{
   $doc = $response->response->docs[$x]->getInputDocument();
   $second_doc->addField('point', $row['POINT']);
}
else
{
    //do other update here
}

//$response->response->docs[$x]->username->values[$x] => get the usernames in the document
Liu Jin Long
  • 83
  • 1
  • 10