Update specific field on SOLR index

后端 未结 6 1467
-上瘾入骨i
-上瘾入骨i 2020-12-03 05:40

I want to using solr for search on articles

I have 3 table:

  1. Group (id , group name)
  2. ArticleBase (id, groupId, some other field)
  3. Arti
6条回答
  •  执笔经年
    2020-12-03 05:56

    SolrPHP doesn't provide any method to update a specific field in Solr.

    However, you can make a Curl call in PHP to update a specific field:

     $docId,
        $solrFieldName => array(
            'set' => $solrFieldValue
        )
    );
    $update = json_encode(array($update));
    
    // Create curl resource and URL
    $ch = curl_init('http://'.SOLR_HOSTNAME.':'.SOLR_PORT.'/'.SOLR_COLLECTION.'/update?commit=true');
    
    // Set Login/Password auth (if required)
    curl_setopt($ch, CURLOPT_USERPWD, SOLR_LOGIN.':'.SOLR_PASSWORD);
    
    // Set POST fields
    curl_setopt($ch, CURLOPT_POST,true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $update);
    
    // Return transfert
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    // Set type of data sent
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    
    // Get response result
    $output = json_decode(curl_exec($ch));
    
    // Get response code
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
    // Close Curl resource
    curl_close($ch);
    
    if ($responseCode == 200)
    {
        echo 'SOLR: Updated successfully field '.$solrFieldName.' for id:'.$docId.' (query time: '.$output->responseHeader->QTime.'ms).';
    }
    else
    {
        echo ('SOLR: Can\'t update field '.$solrFieldName.' for id:'.$docId.', response ('.$responseCode.') is: '.print_r($output,true));
    }
    

    I use this code to update in JSON, you can also provide data in XML.

提交回复
热议问题