首页 文章

更新SOLR索引的特定字段

提问于
浏览
18

我想使用solr搜索文章

我有3个表:

  • 组(id,组名)

  • ArticleBase(id,groupId,其他一些字段)

  • 文章(id,articleBaseId,title,date,...)

在solr schema.xml文件中我只定义与ArticleBase表混合的所有文章字段(在solr上使用一个索引),如下所示:(id,articleBaseId,groupId,...)

problem :管理员想要更改组(ArticleBase),因此我必须更新(或替换)solr中的所有索引文章 . 对 ?
我只能在solr索引中更新groupId吗?

有解决方案吗?

Note :文章表包含超过2亿篇文章,我只使用solr作为索引(不存储除文章ID之外的任何字段数据)

4 回答

  • 4

    有关Solr 4.0中的"Partial Documents Update"功能,请参阅this document

    Solr 4.0现已成为最终版并且已投入 生产环境 .

    此功能可以更新字段,甚至可以将值添加到multiValued字段 .

    毛里西奥在2010年的回答是正确的,但这就是今天的情况 .

  • 1

    SolrPHP不提供更新Solr中特定字段的任何方法 .

    但是,您可以在PHP中进行Curl调用以更新特定字段:

    <?php
    // Update array
    $update = array(
        'id' => $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));
    }
    

    我使用此代码在JSON中更新,您也可以使用XML提供数据 .

  • 34

    我的解决方案如下:

    $client = new SolrClient($options);
    $query = new SolrQuery();
    // Find old Document
    $query->setQuery('id:5458');
    $query->setStart(0);
    $query->setRows(1);
    $query_response = $client->query($query);
    // I had to set the parsemode to PARSE_SOLR_DOC
    $query_response->setParseMode(SolrQueryResponse::PARSE_SOLR_DOC);
    $response = $query_response->getResponse();
    $doc = new SolrInputDocument();
    // used the getInputDocument() to get the old document from the query
    $doc = $response->response->docs[0]->getInputDocument();
    if ($response->response->numFound) {
        $second_doc = new SolrInputDocument();
        $second_doc->addField('cat', "category123");
    // Notice I removed the second parameter from the merge()
        $second_doc->merge($doc);
        $updateResponse = $client->addDocument($second_doc);
        $client->commit();
    }
    
  • 15

    Solr does not support updating individual fields但是,有一个JIRA issue about this(截至本文撰写时差不多3岁) .

    在实施之前,您必须更新整个文档 .

    UPDATE :从Solr 4开始实现,here's the documentation .

相关问题