当前位置: 首页 > 工具软件 > php_solr > 使用案例 >

PHP使用Solr

郎曜文
2023-12-01

Service

​
<?php namespace App\Master\Services;

class SolrService
{
    private $curCollection;
    private $config;

    public function __construct($config, $collection)
    {
        $this->config = $config;

        $this->curCollection = $collection;
    }

    // 增加请求
    public function add($arrs)
    {
        $config = $this->config;
        // 导入到solr
        $url = "http://". $config['hostname'] .":". $config['port'] ."/solr/". $this->curCollection ."/update?wt=json&commit=true";
        
        $headers = [
            'Content-type:text/xml',
        ];

        $xml = '<add>';
        foreach ($arrs as $arr) {
            $index = new \SimpleXMLElement('<doc></doc>');
            foreach ($arr as $key => $value) {
                $element = $index->addChild('field', $value);
                $element->addAttribute('name', $key);
            }
            $dom = dom_import_simplexml($index);
            $xml .= $dom->ownerDocument->saveXML($dom->ownerDocument->documentElement);
        }
        $xml .= '</add>';
        
        $defaults = array( 
            CURLOPT_POST => 1, 
            CURLOPT_URL => $url, 
            CURLOPT_RETURNTRANSFER => 1, 
            CURLOPT_BINARYTRANSFER => 1, 
            CURLOPT_TIMEOUT => 30, 
            CURLOPT_POSTFIELDS => $xml,
            CURLOPT_HTTPHEADER => $headers,
        );

        $ch = curl_init(); 
        curl_setopt_array($ch, ($defaults)); 
        if(!$result = curl_exec($ch)) { 
            trigger_error(curl_error($ch)); 
        } 
        curl_close($ch);
    }

    // 查询删除
    public function deleteByQuery($query)
    {
        $config = $this->config;
        // 导入到solr
        $url = "http://". $config['hostname'] .":". $config['port'] ."/solr/". $this->curCollection ."/update?wt=json&commit=true";
        
        $headers = [
            'Content-type:text/xml',
        ];

        $xml = '<delete><query>'. $query .'</query></delete>';

        $defaults = array( 
            CURLOPT_POST => 1, 
            CURLOPT_URL => $url, 
            CURLOPT_RETURNTRANSFER => 1, 
            CURLOPT_BINARYTRANSFER => 1, 
            CURLOPT_TIMEOUT => 30, 
            CURLOPT_POSTFIELDS => $xml,
            CURLOPT_HTTPHEADER => $headers,
        );

        $ch = curl_init(); 
        curl_setopt_array($ch, ($defaults)); 
        if(!$result = curl_exec($ch)) { 
            trigger_error(curl_error($ch)); 
        } 
        curl_close($ch);
    }

    // 删除所有数据
    public function deleteAll()
    {
        $this->deleteByQuery('*:*');
    }

    // 进行查询操作
    public function query($params, $fqs = [])
    {
        $config = $this->config;
        $params['wt'] = 'json';

        // 导入到solr
        $url = "http://". $config['hostname'] .":". 
            $config['port'] ."/solr/". $this->curCollection ."/select?";

        $url .= http_build_query($params);

        foreach ($fqs as $fq) {
            $url .= "&fq=" . urlencode($fq);
        }

        $defaults = array( 
            CURLOPT_POST => 0, 
            CURLOPT_URL => $url, 
            CURLOPT_RETURNTRANSFER => 1, 
            CURLOPT_BINARYTRANSFER => 1, 
            CURLOPT_TIMEOUT => 30,
        );

        $ch = curl_init(); 
        curl_setopt_array($ch, ($defaults)); 
        if(!$result = curl_exec($ch)) { 
            trigger_error(curl_error($ch)); 
        } 
        curl_close($ch);

        return json_decode($result, true);
    }
}

​

Controller

 public static function search($word, $offset, $limit, $max = 0, $exfq = [])
    {
        $config = config('database.solr.questions');
        $collection = 'questions';
        $bsolr = new \MSolrService($config, $collection);
        try {
            $query = 'fq' => "content:".$word, 'start' => $offset, 'rows' => $limit, 'sort' => "created_at desc", 'q' => '*:*'];
            if ($max) {
                $max = $max - 1;
                $exfq[] = "created_at:[* TO {$max}]";
            }
            $data = $bsolr->query($query, $exfq);
            $response = $data['response'];
            $count = $response['numFound'];
            $docs = $response['docs'];
            $ids = array_column($docs, 'id');
        }catch(\Exception $e) {
            $count = 0;
            $ids = [];
        }

        return array_values(compact('count', 'ids'));
    }

 

 类似资料: