您的位置:首页 > 编程语言 > PHP开发

安装php-solr扩展

2016-04-08 15:33 651 查看

本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加)。

QQ群: 281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29LoD19)

安装php-solr扩展

1.下载安装:
libxml2 2.6.31或以上版本
libcurl 7.18.0 或以上版本
到php官网下载solr:http://pecl.php.net/package/solr
然后:
/usr/local/php5/bin/phpize
./configure --with-php-config=/usr/local/php5/bin/php-config
sudo make
sudo make test
sudo make install

安装完后会有类似这样的提示:

Installing shared extensions: /usr/local/lib/php/extensions/no-debug-non-zts-20090626/

把这个记住,然后修改php.ini(vim /usr/local/etc/php.ini ),把

extension_dir = "./"

修改为

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"

并添加一行

extension=solr.so

注意:/usr/local/php5/bin/phpize 可能会出现变化,具体根据php的安装位置而定。

2. 建立自定义索引模式

a) 打开E:\solr\conf\schema.xml 文件 找到
<fields>
……
<fields>
替换为
<fields>

1. <field name="id" type="string" indexed="true" stored="true" required="true" />

2. <field name="name" type=" string " indexed="true" stored="true" required="true" />

3. <field name="address" type="text" indexed="true" stored="true" multiValued="true" required="true" />

</fields>

<defaultSearchField>text</defaultSearchField>
替换为
<defaultSearchField>name</defaultSearchField>

删除所有< copyField …> 项

3. 建立PHP客户端
在wamp的www目录下建立solr目录。
将SolrPhpClient.zip解压,并将其中的Apache目录拷贝到www/solr目录下。

创建index.php文件,内容如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" lang="zh-CN">

<head>

<meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/>

<title></title>

</head>

<body>

<?php

require_once( 'Apache/Solr/Service.php' );

// 连接solr服务器

$solr = new Apache_Solr_Service( '127.0.0.1', '8080', '/solr' );

//测试是否联通

if ( ! $solr->ping() ) {

echo 'Solr service not responding.';

exit;

}

//

// 创建两条记录nuby 和 zhangyan

//

$parts = array(

'nuby' => array(

'id' => 1,

'name' => '张岩',

'address' => array( '天安门', '北京天安门' ),

),

'zhangyan' => array(

'partno' => 2,

'name' => '张岩',

'model' => '北京五道口',

)

);

$documents = array();

foreach ( $parts as $item => $fields ) {

$part = new Apache_Solr_Document();

foreach ( $fields as $key => $value ) {

if ( is_array( $value ) ) {

foreach ( $value as $datum ) {

$part->setMultiValue( $key, $datum );

}

}

else {

$part->$key = $value;

}

}

$documents[] = $part;

}

//

// 创建索引

//

try {

$solr->addDocuments( $documents );

$solr->commit();

$solr->optimize();

}

catch ( Exception $e ) {

echo $e->getMessage();

}

//

// 查询

//

$offset = 0;

$limit = 10;

$queries = array(

'id: 1 OR id: 2',

'name: 张岩',

'name: 天安门'

);

foreach ( $queries as $query ) {

$response = $solr->search( $query, $offset, $limit );

if ( $response->getHttpStatus() == 200 ) {

if ( $response->response->numFound > 0 ) {

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

echo "$doc->partno $doc->name <br />";

}

echo '<br />';

}

}

else {

echo $response->getHttpStatusMessage();

}

}

?>

</body>

</html>

本人qq群也有许多的技术文档,希望可以为你提供一些帮助(非技术的勿加)。

QQ群: 281442983 (点击链接加入群:http://jq.qq.com/?_wv=1027&k=29LoD19)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: