部署 Apache Nutch Indexer 外掛程式

您可以部署 Apache Nutch (開放原始碼網頁檢索器) 的 Cloud Search 索引外掛程式,設定 Google Cloud Search 為使用者提供網頁內容。

開始網頁檢索後,Apache Nutch 會檢索網頁,並使用索引外掛程式將文件內容的原始二進位 (或文字) 版本上傳至 Google Cloud Search API。Cloud Search API 會為內容建立索引,並將結果提供給使用者。

重要注意事項

部署索引外掛程式前,請注意下列事項。

系統需求

系統需求
作業系統 僅適用於 Linux:
  • Ubuntu
  • Red Hat Enterprise Linux 5.0
  • SUSE Enterprise Linux 10 (64 位元)
軟體
  • Apache Nutch 1.15 版。索引器外掛程式軟體包含這個版本的 Nutch。
  • 在執行索引外掛程式的電腦上安裝 Java JRE 1.8
Apache Tika 文件類型 Apache Tika 1.18 支援的文件格式

部署索引外掛程式

這些步驟說明如何安裝索引器外掛程式,以及如何設定其元件,以便檢索網址並將結果傳回 Cloud Search。

必要條件

部署索引外掛程式前,請先收集連線至 Cloud Search 和資料來源所需的資訊:

步驟 1:建構及安裝外掛程式軟體和 Apache Nutch

  1. 從 GitHub 複製索引外掛程式存放區。

    $ git clone https://github.com/google-cloudsearch/apache-nutch-indexer-plugin.git
    $ cd apache-nutch-indexer-plugin
  2. 查看所需的索引外掛程式版本:

    $ git checkout tags/v1-0.0.5
  3. 建構索引器外掛程式。

    $ mvn package

    如要在建構外掛程式時略過測試,請使用 mvn package -DskipTests

  4. 下載 Apache Nutch 1.15,然後按照 Apache Nutch 安裝說明操作。

  5. target/google-cloudsearch-apache-nutch-indexer-plugin-v1.0.0.5.zip 解壓縮至資料夾。將 plugins/indexer-google-cloudsearch 資料夾複製到 Apache Nutch plugins 資料夾 (apache-nutch-1.15/plugins)。

步驟 2:設定索引外掛程式

如要設定外掛程式,請建立名為 plugin-configuration.properties 的檔案。 設定檔必須指定下列參數,才能存取 Cloud Search 資料來源。

設定 參數
資料來源 ID api.sourceId = 1234567890abcdef
這是必要欄位。Google Workspace 管理員為索引外掛程式設定的 Cloud Search 來源 ID。
服務帳戶 api.serviceAccountPrivateKeyFile = ./PrivateKey.json
這是必要欄位。Google Workspace 管理員為索引外掛程式存取權建立的 Cloud Search 服務帳戶金鑰檔案。

以下範例顯示設定檔範例:

# data source access
api.sourceId=1234567890abcdef
api.serviceAccountPrivateKeyFile=./PrivateKey.json

設定檔也可以包含控制外掛程式行為的參數,例如外掛程式如何將資料推送至 Cloud Search API,以及如何填入中繼資料和結構化資料。如需這些參數的說明,請參閱「Google 提供的連結器參數」。

步驟 3:設定 Apache Nutch

  1. 開啟 conf/nutch-site.xml 並新增下列參數:

    設定 參數
    外掛程式包含 plugin.includes = text

    必填。要使用的外掛程式清單。其中必須包括至少:

    • index-basic
    • index-more
    • indexer-google-cloudsearch
    conf/nutch-default.xml 提供預設值,但您必須手動將 indexer-google-cloudsearch 加入其中。
    中繼標記名稱 metatags.names = text

    (選用步驟) 以逗號分隔的標記清單,對應至相應資料來源結構定義中的屬性。詳情請參閱「Nutch-parse metatags」。

    以下範例顯示 nutch-site.xml 的必要修改內容:

    <property>
      <name>plugin.includes</name>
      <value>protocol-(http|httpclient)|urlfilter-regex|index-(basic|more|metadata)|query-(basic|site|url|lang)|indexer-google-cloudsearch|nutch-extensionpoints|parse-(text|html|msexcel|msword|mspowerpoint|pdf|metatags)|summary-basic|scoring-opic|urlnormalizer-(pass|regex|basic)|parse-(html|tika|metatags)|index-(basic|anchor|more|metadata)</value>
    </property>
    
  2. 開啟 conf/index-writers.xml 並新增下列區段:

    <writer id="indexer_google_cloud_search_1" class="org.apache.nutch.indexwriter.gcs.GoogleCloudSearchIndexWriter">
      <parameters>
        <param name="gcs.config.file" value="path/to/sdk-configuration.properties"/>
      </parameters>
      <mapping>
        <copy />
        <rename />
        <remove />
      </mapping>
    </writer>
    

    <writer>」部分包含下列參數:

    設定 參數
    Cloud Search 設定檔的路徑 gcs.config.file = path

    必填。Cloud Search 設定檔的完整 (絕對) 路徑。

    上傳格式 gcs.uploadFormat = text

    (選用步驟) 外掛程式用來將文件內容推送至 Cloud Search API 的格式。有效值如下:

    • raw:推送原始未轉換的內容。
    • text:推送擷取的文字內容。預設值為 raw

步驟 4:設定網頁檢索

開始檢索網站前,請先設定檢索器,只納入貴機構想提供的資訊。詳情請參閱 Nutch 教學課程

  1. 設定啟動網址。

    起始網址可控管網頁檢索器開始檢索內容的位置。 檢索器必須能夠追蹤連結,存取您想納入的所有內容。

    如要設定起始網址,請按照下列步驟操作:

    1. 切換至 Nutch 安裝目錄:
      $ cd ~/nutch/apache-nutch-X.Y/
    2. 建立網址目錄:
      $ mkdir urls
    3. 建立名為 seed.txt 的檔案,並在每行列出一個網址。
  2. 設定追蹤和不追蹤規則。

    網址規則可控管檢索器建立索引的網址。不追蹤規則會排除網址,避免遭到檢索。

    如要設定這些規則:

    1. 切換至 Nutch 安裝目錄。
    2. 編輯「conf/regex-urlfilter.txt」:
      $ nano conf/regex-urlfilter.txt
    3. 輸入規則運算式,並加上「+」或「-」前置字串:

      # skip file extensions
      -\.(gif|GIF|jpg|JPG|png|PNG|ico)
      
      # skip protocols (file: ftp: and mailto:)
      -^(file|ftp|mailto):
      
      # allow urls starting with https://support.google.com/gsa/
      +^https://support.google.com/gsa/
      
      # accept anything else
      #+.
      
  3. 編輯檢索指令碼。

    如果缺少 gcs.uploadFormat 參數或設為「raw」,請務必在 nutch index 指令中加入 -addBinaryContent -base64 引數。這些引數會告知 Nutch Indexer 模組,在 Base64 中加入二進位內容。

    1. apache-nutch-1.15/bin 中開啟 crawl 指令碼。
    2. 如以下範例所示新增選項:

            if $INDEXFLAG; then
                echo "Indexing $SEGMENT to index"
                __bin_nutch index $JAVA_PROPERTIES "$CRAWL_PATH"/crawldb -addBinaryContent -base64 -linkdb "$CRAWL_PATH"/linkdb "$CRAWL_PATH"/segments/$SEGMENT
      
                echo "Cleaning up index if possible"
                __bin_nutch clean $JAVA_PROPERTIES "$CRAWL_PATH"/crawldb
            else
                echo "Skipping indexing ..."
      

步驟 5:開始網頁檢索和內容上傳

設定索引外掛程式後,即可在本機模式下執行。使用 ./bin 中的指令碼執行檢索工作。

以下範例假設元件位於本機目錄中。從 apache-nutch-1.15 目錄執行 Nutch:

$ bin/crawl -i -s urls/ crawl-test/ 5

您可以在終端機或 logs/ 目錄中找到檢索記錄。如要直接記錄輸出內容,請編輯 conf/log4j.properties