QQ羣: 281442983 (點擊連接加入羣:http://jq.qq.com/?_wv=1027&k=29LoD19) php
安裝php-solr擴展html
安裝完後會有相似這樣的提示:
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/"vim
2. 創建自定義索引模式服務器
a) 打開E:\solr\conf\schema.xml 文件 找到
<fields>
……
<fields>
替換爲
<fields>curl
1. <field name="id" type="string" indexed="true" stored="true" required="true" />測試
2. <field name="name" type=" string " indexed="true" stored="true" required="true" />ui
3. <field name="address" type="text" indexed="true" stored="true" multiValued="true" required="true" /> url
</fields>
<defaultSearchField>text</defaultSearchField>
替換爲
<defaultSearchField>name</defaultSearchField>
刪除全部< copyField …> 項spa
3. 創建PHP客戶端
在wamp的www目錄下創建solr目錄。
將SolrPhpClient.zip解壓,並將其中的Apache目錄拷貝到www/solr目錄下。.net
建立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羣: 281442983 (點擊連接加入羣:http://jq.qq.com/?_wv=1027&k=29LoD19)