Lucene / Solr全站搜索快速部署

第一步:安裝Java 8 VM。由於Solr的官網上說,必需要安裝Java 8 VM。到這個頁面來下載:php

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.htmlhtml

下載rpm,在服務器上直接安裝:java

# rpm -ivh jdk-8u171-linux-x64.rpm

第二步:下載Solrlinux

http://www.apache.org/dyn/closer.lua/lucene/solr/7.3.1web

第三步:將Solr解壓縮chrome

#unzip solr-7.3.1.zip
#cd solr-7.3.1
#mv solr-7.3.1 ./solr   //改個名字,不然到搜索的時候老是要帶着很複雜的名字
#chown -R nobody:nobody ./solr  //將目錄受權給nobody
#/sbin/iptables -I INPUT -p tcp --dport 8983 -j ACCEPT //打開8983端口
#/etc/rc.d/init.d/iptables save

Linux 7則是這樣的:
#firewall-cmd --permanent --add-port=8983/tcp --zone=trusted
#systemctl restart firewalld

第四步:創建索引apache

#su nobody  //nobody是網站目錄的全部者
$cd bin
$./solr create -c mysite
$./post -c mysite http://www.mysite.com/product.php?id=1

這樣一條一條地太慢,編寫一個腳原本處理。固然,它還能夠索引pdf、doc、txt、rtf等等等等文件格式,不過,我只須要html的就行。json

#!/bin/bash
#myindexer.sh

for((i=1;i<2000;i++))
do
   ./post -c mysite http://www.mysite.com/product.php?id=$i
done

後面的處理雖然簡單,我也一併寫下來,供參考。bash

$chmod +x ./myindexer.sh   //讓它能夠執行
$./myindexer.sh  //運行起來

運行的時候是這樣的:服務器

COMMITting Solr index changes to http://localhost:8983/solr/mysite/update/extract...
Time spent: 0:00:10.473
java -classpath /home/www/www.mysite.com/www/solr/dist/solr-core-7.3.1.jar -Dauto=yes -Dc=mysite-Ddata=web org.apache.solr.util.SimplePostTool http://www.mysite.com/product.php?id=1
SimplePostTool version 5.0.0
Posting web pages to Solr url http://localhost:8983/solr/mysite/update/extract
Entering auto mode. Indexing pages with content-types corresponding to file endings xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log
Entering crawl at level 0 (1 links total, 1 new)
POSTed web resource http://www.mysite.com/product.php?id=1 (depth: 0)
1 web pages indexed.

第五步:設置一個搜索框

首先要說的是,這個Solr開動起來以後,就會以RESTFUL的形式提供服務,好比我在個人網站上搜索"小說"」的時候是這樣的。

http://www.ypgogo.com:8983/solr/mysite/select?q=小說

它就會返回一個JSON查詢結果回來,以下:

{
  "responseHeader":{
    "status":0,
    "QTime":4,
    "params":{
      "q":"小說"}},
  "response":{"numFound":157,"start":0,"docs":[
      {
        "url":["http://www.ypgogo.com/Read/info/id/87"],
        "id":"http://www.ypgogo.com/Read/info/id/87",
        "stream_size":["null"],
        "x_ua_compatible":["IE=9; IE=8; IE=7; IE=edge; chrome=1"],
        "x_parsed_by":["org.apache.tika.parser.DefaultParser",
          "org.apache.tika.parser.html.HtmlParser"],
        "stream_content_type":["text/html"],
        "cache_control":["no-transform"],
        "keywords":["閱讀,讀書,文學,書籍,書單,讀後感,好書,好書推薦,reading,books,小說"],
        "viewport":["width=device-width, initial-scale=1.0"],
        "dc_title":["你在高原 -  \n        \t雅朋網 - 分享閱讀的快樂!"],
        "content_encoding":["UTF-8"],
        "description":["雅朋網是一個分享閱讀書單的社區!有好書,請來這裏推薦給你們。"],
        "title":["你在高原 -  \n        \t雅朋網 - 分享閱讀的快樂!"],
        "content_type":["text/html; charset=UTF-8"],
        "stream_size_str":["null"],
        "url_str":["http://www.ypgogo.com/Read/info/id/87"],
        "cache_control_str":["no-transform"],
        "x_ua_compatible_str":["IE=9; IE=8; IE=7; IE=edge; chrome=1"],
        "dc_title_str":["你在高原 -  \n        \t雅朋網 - 分享閱讀的快樂!"],
        "x_parsed_by_str":["org.apache.tika.parser.DefaultParser",
          "org.apache.tika.parser.html.HtmlParser"],
        "description_str":["雅朋網是一個分享閱讀書單的社區!有好書,請來這裏推薦給你們。"],
        "content_type_str":["text/html; charset=UTF-8"],
        "stream_content_type_str":["text/html"],
        "viewport_str":["width=device-width, initial-scale=1.0"],
        "title_str":["你在高原 -  \n        \t雅朋網 - 分享閱讀的快樂!"],
        "keywords_str":["閱讀,讀書,文學,書籍,書單,讀後感,好書,好書推薦,reading,books,小說"],
        "_version_":1601524572017917952,
        "content_encoding_str":["UTF-8"]},
}

須要用PHP再處理一會兒,將結果展現在網頁上就能夠了。

後來,我發現,這個8983端口,誰均可以訪問,好像不妥,因而只容許本地程序訪問它。

不過,你前面要是在第三步設置過,還要把那一句刪除掉。再用上面這兩句,順序不能錯。

#vi /etc/sysconfig/iptables

找到第三步設置的那一句,把它刪除掉。而後再進行下面的操做。

#iptables -A INPUT -p tcp --dport 8983 -j DROP
#iptables -A INPUT -p tcp -s 127.0.0.1 --dport 8983 -j ACCEPT
#/etc/rc.d/init.d/iptables save
#service iptables restart

不過,後來我遇到了困難,尚未辦法訪問,因而,我新設了一個虛擬主機叫localhost

<VirtualHost *:80>
    DocumentRoot   /some/path
    ServerName     localhost

    <Directory /some/path >
        Require ip 127.0.0.1  ::1
    </Directory>

</VirtualHost>

我重啓Apache後,試了一下:

# curl http://localhost:8983/solr/mysite/select?q=小說

後來在程序裏出問題了,出錯信息以下:

Server Error
Caused by:
org.apache.solr.common.SolrException: URLDecoder: The query string contains a not-%-escaped byte &gt; 127 at position 2

 原來,查詢的文字須要處理一下。注意我用的開發語言是PHP。

//$q = '小說';//錯的
$q = urlencode('小說');
$url = 'http://localhost:8983/solr/mysite/select?q='.$q;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$results = curl_exec($ch);
curl_close($ch);
相關文章
相關標籤/搜索