1

我的问题是我无法将数组发送到 solr 机器以进行更新。我使用 codeigniter 作为框架,这是我的代码:

            $solrData = array();

            $solrData['id']                 = $this->data['profil_data']['id'];
            $solrData['site']               = $this->data['profil_data']['site'];
            $solrData['url_Domain']         = $this->data['profil_data']['url_Domain'];
            $solrData['url_Page']           = $this->data['profil_data']['url_Page'];
            $solrData['url_Profil']         = $this->data['profil_data']['url_Profil'];
            $solrData['scr_Kobi_Rank']      = $this->data['profil_data']['scr_Kobi_Rank'];
            $solrData['scr_A']              = $this->data['profil_data']['scr_A'];
            $solrData['scr_B']              = $this->data['profil_data']['scr_B'];
            $solrData['scr_C']              = $this->data['profil_data']['scr_C'];
            $solrData['scr_D']              = $this->data['profil_data']['scr_D'];
            $solrData['loc_City']           = $this->input->post('plakano');
            $solrData['loc_Lat_Lon']        = $this->input->post('loc_Lat_Lon');
            $solrData['com_Category']       = explode(',', $this->input->post('category'));         

            $urunData   = $this->input->post('urun_list');

            foreach($urunData as $row)
            {
                $ontoData = $this->m_onto->getOntoDataByOntoDataId($row);
                $solrData['com_Products'][] = $ontoData['baslik'];
            }

            $hizmetData = $this->input->post('hizmet_list');

            foreach($hizmetData as $row)
            {
                $ontoData = $this->m_onto->getOntoDataByOntoDataId($row);
                $solrData['com_Services'][] = $ontoData['baslik'];
            }

            $solrData['com_Type']           = $this->input->post('sirketturu');
            $solrData['com_Description']    = $this->input->post('description');
            $solrData['com_Title_Selected'] = $this->input->post('title');
            $solrData['com_Title_Long']     = $this->data['profil_data']['com_Title_Long'];
            $solrData['crm_Tel']            = $this->input->post('tel');
            $solrData['crm_Fax']            = $this->input->post('fax');
            $solrData['crm_Email']          = $this->input->post('email');

            $this->solr->updateSolrProfilData($solrData);

和solr过程:

public function updateSolrProfilData($arrData)
{
    if(count($arrData) == 0)
        return FALSE;

    $solrClientOptions          = $this->solrClientOptionsYazProfil;    
    $solrClientOptionsCommit    = $this->solrClientOptionsYazProfilCommit;

    $solrClient = new SolrClient($solrClientOptions);       
    $solrDoc    = new SolrInputDocument();

    foreach($arrData as $firmaField => $firmaValue)
    {
        if(! is_array($firmaValue))
        {
            $solrDoc->addField($firmaField, $firmaValue);
        }
        else
        {
            foreach($firmaValue as $firmaField2 => $firmaValue2)
            {
                if($firmaValue2 != '')
                {
                    $solrDoc->addField($firmaField, $firmaValue2);
                }
            }
        }
    }

    try {
        $this->_solrCommit($solrClientOptionsCommit);
    } catch (Exception $e) {
        echo $e->getMessage(); 
    }           
}

Solr 提交功能:

private function _solrCommit($solrArr)
{
    $urlCommit  = 'http://' . $solrArr['hostname'] . ":" . $solrArr['port'] . '/' . $solrArr['path'] . "/update?stream.body=%3Ccommit/%3E&wt=json";

    $output     = file_get_contents($urlCommit);
    $outputArr  = json_decode($output, TRUE);

    if ($outputArr['responseHeader']['status'] ===  0)
        return TRUE;
    else
        return FALSE;
}

这就是选项:

private $solrClientOptionsYazProfilCommit = array(
                                'hostname' => SOLR_HOST_YAZ,
                                'login'    => '',
                                'password' => '',
                                'port'     => SOLR_PORT,
                                'path'     => 'solr/collection1'
);

尽管 try-catch 没有返回错误,但数据无法更新。此外,代码成功发送 solr commit。我检查了网址,但格式正确。这里有什么问题?

4

2 回答 2

0

您的问题是您从未向 Solr 发出命令以将您构建的文档添加到索引中。您只是发出成功执行的提交命令。

由于您使用的是 PHP,我建议您使用PHP SolrClient。这将使您不必自己手动编写所有函数(添加、删除、提交等)。在这种情况下,您需要调用addDocument函数。

于 2012-10-10T12:07:23.870 回答
0

不要使用 PHP/Pecl solr 库。如果您可以通过 URL 访问 solr,那么您应该只使用 PHP 和 CURL:

static function doCurl($url, $username = null, $password = null) {
    if (!function_exists('curl_init')) {
        // throw error
    }

    $ch = curl_init();
    $opts = array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_POST => 1,
        CURLOPT_TIMEOUT => 120,
        CURLOPT_FAILONERROR => 1,
        CURLOPT_HTTPAUTH => CURLAUTH_ANY
    );

    if ($password != null && $username != null) {
        $opts[CURLOPT_USERPWD] = "$username:$password";
    }

    curl_setopt_array($ch, $opts);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

用法是:
doCurl("http://hostNameHere:8983/solr/select/?q=solr&start=0&rows=10&indent=on", "user", "pass");

于 2012-10-13T02:50:34.917 回答