PHP中使用 Solr扩展的示例(一)

文档地址:https://www.php.net/manual/zh/solr.examples.php

一、


<?php
/* Solr服务器的域名*/
define('SOLR_SERVER_HOSTNAME', 'solr.example.com');

/*是否在安全模式下运行*/
define('SOLR_SECURE', true);
/* HTTP端口到连接*/
define('SOLR_SERVER_PORT', ((SOLR_SECURE) ? 8443 : 8983));

/* HTTP基本认证用户名*/
define('SOLR_SERVER_USERNAME', 'admin');

/* HTTP基本认证密码*/
define('SOLR_SERVER_PASSWORD', 'changeit');

/* HTTP连接超时*/
/*这是http数据传输操作所允许的最大时间(以秒为单位)。默认值是30秒*/
define('SOLR_SERVER_TIMEOUT', 10);

/*文件名到一个pemo格式的私钥+私证书(按该顺序连接)*/
define('SOLR_SSL_CERT', 'certs/combo.pem');

/*文件名到一个peme格式的私有证书只有*/
define('SOLR_SSL_CERT_ONLY', 'certs/solr.crt');

/*将文件名转换为一个peme格式的私钥*/
define('SOLR_SSL_KEY', 'certs/solr.key');

/* peme格式私钥文件的密码*/
define('SOLR_SSL_KEYPASSWORD', 'StrongAndSecurePassword');

/*持有一个或多个CA证书的文件的名称,以便使用*/
define('SOLR_SSL_CAINFO', 'certs/cacert.crt');

/*持有多个CA证书的目录名,用验证peer*/
define('SOLR_SSL_CAPATH', 'certs/');

?>
二、将文档添加到索引


<?php

include "bootstrap.php";

$options = array
(
    'hostname' => SOLR_SERVER_HOSTNAME,  
    'login'    => SOLR_SERVER_USERNAME,      //登录账号
    'password' => SOLR_SERVER_PASSWORD,  //密码
    'port'     => SOLR_SERVER_PORT,      //端口
);

$client = new SolrClient($options);

$doc = new SolrInputDocument();
//添加字段
$doc->addField('id', 334455);
$doc->addField('cat', 'Software');
$doc->addField('cat', 'Lucene');

$updateResponse = $client->addDocument($doc);

print_r($updateResponse->getResponse());

?>
以上例程的输出类似于:


SolrObject Object
(
    [responseHeader] => SolrObject Object
        (
            [status] => 0
            [QTime] => 446
        )
)
三、合并一个文档到另一个文档


<?php
include "bootstrap.php";

$doc = new SolrDocument();
$second_doc = new SolrDocument();
$doc->addField('id', 1123);
$doc->features = "PHP Client Side";
$doc->features = "Fast development cycles";
$doc['cat'] = 'Software';
$doc['cat'] = 'Custom Search';
$doc->cat   = 'Information Technology';
$second_doc->addField('cat', 'Lucene Search');
$second_doc->merge($doc, true);
print_r($second_doc->toArray());
?>
以上例程的输出类似于:


Array
(
    [document_boost] => 0
    [field_count] => 3
    [fields] => Array
        (
            [0] => SolrDocumentField Object
                (
                    [name] => cat
                    [boost] => 0
                    [values] => Array
                        (
                            [0] => Software
                            [1] => Custom Search
                            [2] => Information Technology
                        )
                )

            [1] => SolrDocumentField Object
                (
                    [name] => id
                    [boost] => 0
                    [values] => Array
                        (
                            [0] => 1123
                        )
                )
            [2] => SolrDocumentField Object
                (
                    [name] => features
                    [boost] => 0
                    [values] => Array
                        (
                            [0] => PHP Client Side
                            [1] => Fast development cycles
                        )
                )
        )
)
四、搜索文档-SolrObject响应


<?php
include "bootstrap.php";
$options = array
(
    'hostname' => SOLR_SERVER_HOSTNAME,
    'login'    => SOLR_SERVER_USERNAME,
    'password' => SOLR_SERVER_PASSWORD,
    'port'     => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('lucene');
$query->setStart(0);
$query->setRows(50);
$query->addField('cat')->addField('features')->addField('id')->addField('timestamp');
$query_response = $client->query($query);
$response = $query_response->getResponse();
print_r($response);
?>
输出:


SolrObject Object
(
    [responseHeader] => SolrObject Object
        (
            [status] => 0
            [QTime] => 1
            [params] => SolrObject Object
                (
                    [wt] => xml
                    [rows] => 50
                    [start] => 0
                    [indent] => on
                    [q] => lucene
                    [fl] => cat,features,id,timestamp
                    [version] => 2.2
                )
        )

    [response] => SolrObject Object
        (
            [numFound] => 3
            [start] => 0
            [docs] => Array
                (
                    [0] => SolrObject Object
                        (
                            [cat] => Array
                                (
                                    [0] => Software
                                    [1] => Lucene
                                )

                            [id] => 334456
                        )

                    [1] => SolrObject Object
                        (
                            [cat] => Array
                                (
                                    [0] => Software
                                    [1] => Lucene
                                )

                            [id] => 334455
                        )

                    [2] => SolrObject Object
                        (
                            [cat] => Array
                                (
                                    [0] => software
                                    [1] => search
                                )
                            [features] => Array
                                (
                                    [0] => Advanced Full-Text Search Capabilities using Lucene
                                    [1] => Optimized for High Volume Web Traffic
                                    [2] => Standards Based Open Interfaces - XML and HTTP
                                    [3] => Comprehensive HTML Administration Interfaces
                                    [4] => Scalability - Efficient Replication to other Solr Search Servers
                                    [5] => Flexible and Adaptable with XML configuration and Schema
                                    [6] => Good unicode support: héllo (hello with an accent over the e)
                                )
                            [id] => SOLR1000
                            [timestamp] => 2009-09-04T20:38:55.906
                        )
                )
        )
)
五、搜索文档-SolrDocument响应


<?php
include "bootstrap.php";
$options = array
(
    'hostname' => SOLR_SERVER_HOSTNAME,
    'login'    => SOLR_SERVER_USERNAME,
    'password' => SOLR_SERVER_PASSWORD,
    'port'     => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setQuery('lucene');
$query->setStart(0);
$query->setRows(50);
$query->addField('cat')->addField('features')->addField('id')->addField('timestamp');
$query_response = $client->query($query);
$query_response->setParseMode(SolrQueryResponse::PARSE_SOLR_DOC);/*设置解析模式,SolrResponse :: PARSE_SOLR_DOC解析SolrDocument实例中的文档。SolrResponse :: PARSE_SOLR_OBJ将文档解析为SolrObjects。*/
$response = $query_response->getResponse();
print_r($response);
?>


六、简单的TermsComponent示例-基本

<?php
include "bootstrap.php";
$options = array
(
    'hostname' => SOLR_SERVER_HOSTNAME,
    'login'    => SOLR_SERVER_USERNAME,
    'password' => SOLR_SERVER_PASSWORD,
    'port'     => SOLR_SERVER_PORT,
);
$client = new SolrClient($options);
$query = new SolrQuery();
$query->setTerms(true);//启用或禁用TermsComponent
$query->setTermsField('cat');
$updateResponse = $client->query($query);
print_r($updateResponse->getResponse());

?>










分享到: