在solrcommit之前无法发送solr数据

时间:2012-10-10 10:27:26

标签: php codeigniter solr

我的问题是我无法将数组发送到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 Commit功能:

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'
);

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

2 个答案:

答案 0 :(得分:0)

您的问题是您永远不会向Solr发出命令,以将您构建的文档添加到索引中。您只发出正在执行的commit命令。

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

答案 1 :(得分: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”);