前面我配置好了solr,而且數據庫創建索引也完成了。php
首先下載solrphphtml
http://wiki.apache.org/solr/SolPHP
在solrphp裏面包括了一個/Apache/solr的文件夾。將solr這個文件夾拷貝到項目中並引用。算法
require_once('Solr/Service.php');
如今能夠開始使用進行搜索了,一下三段代碼創建個一個簡單的搜索。數據庫
require_once('Solr/Service.php'); $solr= new Apache_Solr_Service('192.168.10.11','8080','solr/'); $query= $solr->search($_GET['q'], 0, 10); //查詢q
Apache_Solr_Service實例化了一個鏈接到solr的服務,$solr->search表示查詢傳入的參數q,並查詢0開始的10條數據 。$query是一個Apache_Solr_Response對象,這個對象是Solr數據返回的對象。主要包括了5個函數 。apache
public function getHttpStatus() public function getHttpStatusMessage() public function getType() public function getEncoding() public function getRawResponse()
使用上面的函數來獲取須要的數據json
if ($query->getHttpStatus()==200){ $raw=$query->getRawResponse(); $rawobj=json_decode($raw); $response=$rawobj->response ; echo "All:".$response->numFound."</br>"; echo "start:".$response->start."</br>"; foreach ($response->docs as $value) { //$value爲在solr的schema.xml文件裏配置的 field ...... } }
在Lucene中搜索最終是調用了IndexSearcher的search方法,同時傳入了一個Query的實例。其中Lucene內置的Query類型包括了一下幾個:函數
TermQuery 經過項進行搜索ui
好比返回域content裏包含hello的文檔
Query query=new TermQuery(new Term("content","hello"));
code
TermRangeQuery 指定範圍搜索xml
好比搜索title裏面從a到d範圍內,包含a不包含d的文檔
Query query=new TermRangeQuery("title","a","d",ture,false);
NumericRangeQuery 指定數字範圍
搜索201401到201405範圍內的文檔Query query=NumericRangeQuery.newIntRange("month","201401","201405",ture,ture);
PrefixQuery 經過字符串搜索
搜索content中以hello開頭的文檔
Query query=new PrefixQuery(new Term("content","hello"));
BooleanQuery 組合搜索
BooleanQuery query=new BooleanQuery(); query.add(iquery,BooleanQuery.Occur.MUST); query.add(...); ......
其中BooleanQuery的add方法傳入的一個Query對象和一個Occur的枚舉。Occur枚舉包括了MUST(and),SHOULD(no),MUST_NOT(not).來和query之間作邏輯的組合。
PhraseQuery 短語搜索,用來查詢
WildcardQuery 通配符查詢
FuzzQuery 相似項搜索。使用Levenshtein算法,算法詳情能夠查看 http://www.cnblogs.com/ac1985482/p/Levenshtein.html
MatchAllDocsQuery 匹配全部文檔
當使用Lucene來作開發的時候咱們能夠使用以上的對象作,當使用solr的時候,就不能使用上面的對象來作了,這個時候解析表達式就發揮做用了
檢索運算符
[] 包含範圍檢索,如檢索某時間段記錄,包含頭尾,date:[200707 TO 200710]
" 轉義操做符,特殊字符包括+ - && || ! ( ) { } [ ] ^ " ~ * ? : "