PHP solr服务器搭建,搜索方案 solr+php如何安装配置?

钱钊
2023-12-01

问题

solr+php如何安装配置使用

解决方法1:

solr提供http请求查询接口。客户端通过触发http请求获取json、xml等数据格式数据,并对数据进行解析显示。一般情况下各种语言都会有封装好的客户端插件,如java的solrj、python的solrpy,php的php-solr-client,根据提供的api进行索引和查询。至于solr的安装配置可以去看看solr cookbook 这本书.

解决方法2:

要让php程序用上solr搜索,刚开始心里犯怵

我不会java怎么办?

搜素系统的索引怎么建立?

php怎么写代码?

solr是java写的,是一个服务器程序,有一个管理界面,提供http接口

除了配置文件不用写java代码、php有solr的扩展

所以,还算简单

1,安装solr和验证你安装上了

参考 CentOS下安装Solr5.3

solr5.3已经是单独的服务器,包括web管理后台,所以挺好的

只需要保证你的linux 有jdk

直接下载 solr5.3的包,Apache Download Mirrors

一定要下载solr5.3.tgz 这样的,不要下src的包,然后解压得到类似solr5.3这样的目录

安装最关键的两点 1 不是解压就能用 solr目录下有个 install脚本要运行,并且要指定data目录(要存储索引)2 solr 有一个core的概念,理解成数据库里的db吧,可以建立一个core,这个也有脚本,但一定保证data目录是solr用户的权限

# solr-5.3.0/bin/install_solr_service.sh solr-5.3.0.tgz -d /data/solr -i /usr/local

-i表示你把solr服务器装在哪

# su - solr -c "/usr/local/solr/solr/bin/solr create -c corename

创建一个你自己的core

service solr start 启动服务

http://你的ip::8983/solr 这个是管理界面

2,设计你自己的搜索索引

http://你的ip::8983/solr 这个是管理界面,选择你创建的core

只关注 add Documents 和 query 即可,一个事加索引一个事查询,特别容易理解

add Documents界面,已经有例子,索引了两个字段 id和title ,你改下数据提交

Query 界面 q=*:* 查询所有

q=title:a* 查询title为a开头的

简单玩几次很容易明白

现在有id,title 我想添加一个自己的字段,type怎么办?

去data目录grep "title", 找到id和title的配置项,类似:

参考下填写上type即可

service solr stop/start 重启服务

回到管理界面,添加数据,假设你要联合搜索title和type

那么 q= title:xx fq= type:xx, 多加一个fq明白了吧

3,写一个php程序

经过1,2个研究,基本能做到建立数据,查出数据现在就写一个php程序完成这些功能

参考:PHP: Solr - Manual

SolrInputDocument 输入文档

SolrClient 这个是用户连接solr服务器

SolrQuery 这个是构建搜索条件

直接上程序好了:class Search

{

private $client;

public function __construct() {

$options =

[

'hostname' => 'localhost', //solr服务器的ip

'path' => 'solr/corename', //这个解决core的问题,corename就是你的core

'wt' => 'json',

];

$this->client = new \SolrClient($options);

}

public function addIndex($id, $type, $content) {//加索引的函数

$doc = new \SolrInputDocument();

$doc->addField('id', $type . "." . $id); //保证id唯一我把type加上了

$doc->addField('type', $type);

$doc->addField('title', $content);

$client = $this->client;

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

$client->commit(); //一定要commit才能立即生效

$ret = ($updateResponse->getResponse());

if ( isset($ret->responseHeader['status']) ) {

return $ret->responseHeader['status'] == 0?true:false;

}

return false;

}

public function search($key, $type=0, $page=0, $limit=15) {

$query = new \SolrQuery();

$query->setQuery('title:' . $key . "*"); //这个是设置keyword

$query->setStart($page); //分页的

$query->setRows($limit);

if($type) {

$query->addFilterQuery('type:' . $type); //用到fq了

}

$query->addField('id')->addField('title')->addField('type'); //这里是你要查哪些字段

$client = $this->client;

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

$response = $query_response->getResponse();

$response = ($response->response);

if ( is_array($response->docs) ) {

foreach($response->docs as &$doc) {

$id = explode(".", $doc->id);

if( isset($id[1]) ) {

$doc->id = $id[1];

}

$doc->type = isset($doc->type[0])?$doc->type[0]:'';

}

}

return ($response);

}

}

相关文章:

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

 类似资料: