php使用solr基础代码类

<?php

class solr
{
    private $client;
    function __construct() {
        $options = array
        (
            'hostname' => "localhost",
            'path'     => 'solr/test',
            'port'     => '8983',
        );
        $this->client = new SolrClient($options);
    }
  
    //添加
    function add(){
        $data = array(
            array(
            'id' => '1',
            'name' => '男士打磨直筒休闲牛仔裤1',
            'brand' => 'ENERGIE',
            'cat' => '牛仔裤',
            'price' => '1870.00'
            ),
            array(
            'id' => '2',
            'name' => '品牌LOGO翻领拉链外套2',
            'brand' => 'ENERGIE',
            'cat' => '外套',
            'price' => '1680.00'
            ),
        );


        foreach($data as $key => $value) {
            $doc = new SolrInputDocument();
            foreach($value as $key2 =>$value2) {
              $doc->addField($key2,$value2);
            }
           $client->addDocument($doc); 

        }
        $res = $this->client->commit();

        var_dump($res);
    }
    
    //查询
    function query(){
        $query = new SolrQuery();

        $query->setQuery("打磨");

        $query->setStart(0);

        $query->setRows(50);

        $query->addField('name');

        $query_response = $this->client->query($query);

        $response = $query_response->getResponse();

        print_r($response);
    }
    //删除
    function deleteCcore(){
        $this->client->deleteByQuery('id:EN80922032'); 
        $result = $this->client->commit();
        print_r($result);
        
    }
}
$solr = new solr;

$solr->query();


?>

 

posted @ 2020-11-26 17:28  流浪2024  阅读(153)  评论(0编辑  收藏  举报