电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

PHP擴展之針對搜索引擎的擴展(一)——Apache Solr

瀏覽:42日期:2022-09-15 18:09:28
簡介及安裝

Solr擴展允許你在PHP5中有效的和Apache Solr服務器通話。

Solr擴展快速, 輕量級, 有允許PHP開發者和Solr服務器實例有效通話的特性豐富的庫。

兼容APache Solr 1.3 和 1.4。

有內置工具添加文檔和更新到solr服務器,還有工具允許你在搜索文檔時構建高級查詢。

需要PHP5.2.11及以上版本。

需要安裝libxml及curl擴展。

使用示例

Example #1 啟動文件的內容

<?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);/*?File?name?to?a?PEM-formatted?private?key?+?private?certificate?*/define(’SOLR_SSL_CERT’,?’certs/combo.pem’);/*?File?name?to?a?PEM-formatted?private?certificate?only?*/define(’SOLR_SSL_CERT_ONLY’,?’certs/solr.crt’);/*?File?name?to?a?PEM-formatted?private?key?*/define(’SOLR_SSL_KEY’,?’certs/solr.key’);/*?Password?for?PEM-formatted?private?key?file?*/define(’SOLR_SSL_KEYPASSWORD’,?’StrongAndSecurePassword’);/*?Name?of?file?holding?one?or?more?CA?certificates?to?verify?peer?with*/define(’SOLR_SSL_CAINFO’,?’certs/cacert.crt’);/*?Name?of?directory?holding?multiple?CA?certificates?to?verify?peer?with?*/define(’SOLR_SSL_CAPATH’,?’certs/’);?>

Example #2 添加文檔到索引

<?phpinclude?'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))

Example #3 合并一個文檔到另一個文檔

<?phpinclude?'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))))

Example #4 搜索文檔 - SolrObject響應

<?phpinclude?'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))))

Example #5 搜索文檔 - SolrDocument響應

<?phpinclude?'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);$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] => SolrDocument Object( [_hashtable_index:SolrDocument:private] => 19740) [1] => SolrDocument Object( [_hashtable_index:SolrDocument:private] => 25485) [2] => SolrDocument Object( [_hashtable_index:SolrDocument:private] => 25052))))

Example #6 簡單 TermsComponent 實例 - 基本

<?phpinclude?'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);$query->setTermsField(’cat’);$updateResponse?=?$client->query($query);print_r($updateResponse->getResponse());?>

以上例程的輸出類似于:

SolrObject Object( [responseHeader] => SolrObject Object( [status] => 0 [QTime] => 2) [terms] => SolrObject Object( [cat] => SolrObject Object( [electronics] => 14 [Lucene] => 4 [Software] => 4 [memory] => 3 [card] => 2 [connector] => 2 [drive] => 2 [graphics] => 2 [hard] => 2 [monitor] => 2)))

Example #7 簡單 TermsComponent 實例 - 使用前綴

<?phpinclude?'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);/*?Return?only?terms?starting?with?$prefix?*/$prefix?=?’c’;$query->setTermsField(’cat’)->setTermsPrefix($prefix);$updateResponse?=?$client->query($query);print_r($updateResponse->getResponse());?>

以上例程的輸出類似于:

SolrObject Object( [responseHeader] => SolrObject Object( [status] => 0 [QTime] => 1) [terms] => SolrObject Object( [cat] => SolrObject Object( [card] => 2 [connector] => 2 [camera] => 1 [copier] => 1)))

Example #8 簡單 TermsComponent 實例 - 指定最小頻率

<?phpinclude?'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);/*?Return?only?terms?starting?with?$prefix?*/$prefix?=?’c’;/*?Return?only?terms?with?a?frequency?of?2?or?greater?*/$min_frequency?=?2;$query->setTermsField(’cat’)->setTermsPrefix($prefix)->setTermsMinCount($min_frequency);$updateResponse?=?$client->query($query);print_r($updateResponse->getResponse());?>

以上例程的輸出類似于:

SolrObject Object( [responseHeader] => SolrObject Object( [status] => 0 [QTime] => 0) [terms] => SolrObject Object( [cat] => SolrObject Object( [card] => 2 [connector] => 2)))

Example #9 簡單 Facet 實例

<?phpinclude?'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->setFacet(true);$query->addFacetField(’cat’)->addFacetField(’name’)->setFacetMinCount(2);$updateResponse?=?$client->query($query);$response_array?=?$updateResponse->getResponse();$facet_data?=?$response_array->facet_counts->facet_fields;print_r($facet_data);?>

以上例程的輸出類似于:

SolrObject Object( [cat] => SolrObject Object( [electronics] => 14 [memory] => 3 [Lucene] => 2 [Software] => 2 [card] => 2 [connector] => 2 [drive] => 2 [graphics] => 2 [hard] => 2 [monitor] => 2 [search] => 2 [software] => 2) [name] => SolrObject Object( [gb] => 6 [1] => 3 [184] => 3 [2] => 3 [3200] => 3 [400] => 3 [500] => 3 [ddr] => 3 [i] => 3 [ipod] => 3 [memori] => 3 [pc] => 3 [pin] => 3 [pod] => 3 [sdram] => 3 [system] => 3 [unbuff] => 3 [canon] => 2 [corsair] => 2 [drive] => 2 [hard] => 2 [mb] => 2 [n] => 2 [power] => 2 [retail] => 2 [video] => 2 [x] => 2))

Example #10 簡單 Facet 實例 - 使用可選字段重寫 mincount

<?phpinclude?'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->setFacet(true);$query->addFacetField(’cat’)->addFacetField(’name’)->setFacetMinCount(2)->setFacetMinCount(4,?’name’);$updateResponse?=?$client->query($query);$response_array?=?$updateResponse->getResponse();$facet_data?=?$response_array->facet_counts->facet_fields;print_r($facet_data);?>

以上例程的輸出類似于:

SolrObject Object( [cat] => SolrObject Object( [electronics] => 14 [memory] => 3 [Lucene] => 2 [Software] => 2 [card] => 2 [connector] => 2 [drive] => 2 [graphics] => 2 [hard] => 2 [monitor] => 2 [search] => 2 [software] => 2) [name] => SolrObject Object( [gb] => 6))

Example #11 連接到 SSL-Enabled 服務器

<?phpinclude?'bootstrap.php';$options?=?array( ’hostname’?=>?SOLR_SERVER_HOSTNAME, ’login’????=>?SOLR_SERVER_USERNAME, ’password’?=>?SOLR_SERVER_PASSWORD, ’port’?????=>?SOLR_SERVER_PORT, ’timeout’??=>?SOLR_SERVER_TIMEOUT, ’secure’???=>?SOLR_SECURE, ’ssl_cert’?=>?SOLR_SSL_CERT_ONLY, ’ssl_key’??=>?SOLR_SSL_KEY, ’ssl_keypassword’?=>?SOLR_SSL_KEYPASSWORD, ’ssl_cainfo’?=>?SOLR_SSL_CAINFO,);$client?=?new?SolrClient($options);$query?=?new?SolrQuery(’*:*’);$query->setFacet(true);$query->addFacetField(’cat’)->addFacetField(’name’)->setFacetMinCount(2)->setFacetMinCount(4,?’name’);$updateResponse?=?$client->query($query);$response_array?=?$updateResponse->getResponse();$facet_data?=?$response_array->facet_counts->facet_fields;print_r($facet_data);?>

以上例程的輸出類似于:

SolrObject Object( [cat] => SolrObject Object( [electronics] => 14 [memory] => 3 [Lucene] => 2 [Software] => 2 [card] => 2 [connector] => 2 [drive] => 2 [graphics] => 2 [hard] => 2 [monitor] => 2 [search] => 2 [software] => 2) [name] => SolrObject Object( [gb] => 6))

更多類及方法的使用詳見http://www.php.net/manual/zh/book.solr.php

標簽: PHP
相關文章:
主站蜘蛛池模板: 罗茨真空机组,立式无油往复真空泵,2BV水环真空泵-力侨真空科技 | 空气能暖气片,暖气片厂家,山东暖气片,临沂暖气片-临沂永超暖通设备有限公司 | 宜兴紫砂壶知识分享 - 宜兴壶人 医用空气消毒机-医用管路消毒机-工作服消毒柜-成都三康王 | 标准光源箱|对色灯箱|色差仪|光泽度仪|涂层测厚仪_HRC大品牌生产厂家 | [品牌官网]贵州遵义双宁口腔连锁_贵州遵义牙科医院哪家好_种植牙_牙齿矫正_原华美口腔 | 佛山市钱丰金属不锈钢蜂窝板定制厂家|不锈钢装饰线条|不锈钢屏风| 电梯装饰板|不锈钢蜂窝板不锈钢工艺板材厂家佛山市钱丰金属制品有限公司 | 耐破强度测试仪-纸箱破裂强度试验机-济南三泉中石单品站 | 糖衣机,除尘式糖衣机,全自动糖衣机,泰州市长江制药机械有限公司 体感VRAR全息沉浸式3D投影多媒体展厅展会游戏互动-万展互动 | 艺术生文化课培训|艺术生文化课辅导冲刺-济南启迪学校 | 武汉高低温试验机-现货恒温恒湿试验箱-高低温湿热交变箱价格-湖北高天试验设备 | 济南轻型钢结构/济南铁艺护栏/济南铁艺大门-济南燕翔铁艺制品有限公司 | 广州/东莞小字符喷码机-热转印打码机-喷码机厂家-广州瑞润科技 | 东莞猎头公司_深圳猎头公司_广州猎头公司-广东万诚猎头提供企业中高端人才招聘服务 | 深圳公司注册-工商注册代理-注册公司流程和费用_护航财税 | 岩石钻裂机-液压凿岩机-劈裂机-挖改钻_湖南烈岩科技有限公司 | 国产频谱分析仪-国产网络分析仪-上海坚融实业有限公司 | 生物风-销售载体,基因,质粒,ATCC细胞,ATCC菌株等,欢迎购买-百风生物 | 河南新乡德诚生产厂家主营震动筛,振动筛设备,筛机,塑料震动筛选机 | 诗词大全-古诗名句 - 古诗词赏析 | 衬氟止回阀_衬氟闸阀_衬氟三通球阀_衬四氟阀门_衬氟阀门厂-浙江利尔多阀门有限公司 | 皮带机_移动皮带机_大倾角皮带机_皮带机厂家 - 新乡市国盛机械设备有限公司 | 无锡不干胶标签,卷筒标签,无锡瑞彩包装材料有限公司 | 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 长沙网站建设制作「网站优化推广」-网页设计公司-速马科技官网 | 膏方加工_丸剂贴牌_膏滋代加工_湖北康瑞生物科技有限公司 | 移动厕所租赁|移动卫生间|上海移动厕所租赁-家瑞租赁 | 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 | 精密冲床,高速冲床等冲压设备生产商-常州晋志德压力机厂 | 东莞画册设计_logo/vi设计_品牌包装设计 - 华略品牌设计公司 | 阴离子_阳离子聚丙烯酰胺厂家_聚合氯化铝价格_水处理絮凝剂_巩义市江源净水材料有限公司 | 振动筛-交叉筛-螺旋筛-滚轴筛-正弦筛-方形摇摆筛「新乡振动筛厂家」 | 代写标书-专业代做标书-商业计划书代写「深圳卓越创兴公司」 | 振动台-振动试验台-振动冲击台-广东剑乔试验设备有限公司 | 锌合金压铸-铝合金压铸厂-压铸模具-冷挤压-誉格精密压铸 | 对辊破碎机-液压双辊式,强力双齿辊,四辊破碎机价格_巩义市金联机械设备生产厂家 | 菲希尔X射线测厚仪-菲希尔库伦法测厚仪-无锡骏展仪器有限责任公司 | 篷房|仓储篷房|铝合金篷房|体育篷房|篷房厂家-华烨建筑科技官网 知名电动蝶阀,电动球阀,气动蝶阀,气动球阀生产厂家|价格透明-【固菲阀门官网】 | 北京乾茂兴业科技发展有限公司| 开云(中国)Kaiyun·官方网站-登录入口 | 防火阀、排烟防火阀、电动防火阀产品生产销售商-德州凯亿空调设备有限公司 |