Post 工具

Solr 包含一個簡單的命令列工具,用於將各種內容 POST 到 Solr 伺服器,它是 bin/solr CLI 的一部分。

此工具旨在供探索 Solr 功能的新用戶使用,並非旨在作為將文件索引到生產系統的穩健解決方案。
您可能熟悉 SimplePostTool 和 bin/post Unix Shell 腳本。雖然它仍然可用,但已被棄用,將在 Solr 10 中移除。

若要執行它,請開啟一個視窗並輸入

$ bin/solr post -url https://127.0.0.1:8983/gettingstarted/update example/films/films.json

這將會連線到 localhost:8983 上的伺服器。--help (或簡稱 -h) 選項會輸出其使用方式的相關資訊 (例如,bin/solr post -h))。

使用 bin/solr post 工具

使用 bin/solr post 時,您必須指定指向更新處理器的完整路徑的 url,或者提供 c 集合/核心名稱。

這指定相同的目標集合:-url https://127.0.0.1:8983/gettingstarted/update-c gettingstarted

bin/solr post 的基本用法是

usage: post
 -c,--name <NAME>                                 Name of the collection.
 -d,--delay <delay>                               If recursive then delay
                                                  will be the wait time
                                                  between posts.  default:
                                                  10 for web, 0 for files
    --dry-run                                     Performs a dry run of
                                                  the posting process
                                                  without actually sending
                                                  documents to Solr.  Only
                                                  works with files mode.
 -f,--format                                      sends application/json
                                                  content as Solr commands
                                                  to /update instead of
                                                  /update/json/docs.
 -ft,--filetypes <<type>[,<type>,...]>            default:
                                                  xml,json,jsonl,csv,pdf,d
                                                  oc,docx,ppt,pptx,xls,xls
                                                  x,odt,odp,ods,ott,otp,ot
                                                  s,rtf,htm,html,txt,log
 -h,--help                                        Print this message.
    --mode <mode>                                 Which mode the Post tool
                                                  is running in, 'files'
                                                  crawls local directory,
                                                  'web' crawls website,
                                                  'args' processes input
                                                  args, and 'stdin' reads
                                                  a command from standard
                                                  in. default: files.
 -o,--optimize                                    Issue an optimize at end
                                                  of posting documents.
    --out                                         sends Solr response
                                                  outputs to console.
 -p,--params <<key>=<value>[&<key>=<value>...]>   values must be
                                                  URL-encoded; these pass
                                                  through to Solr update
                                                  request.
 -r,--recursive <recursive>                       For web crawl, how deep
                                                  to go. default: 1
    --skip-commit                                 Do not 'commit', and
                                                  thus changes won't be
                                                  visible till a commit
                                                  occurs.
 -t,--type <content-type>                         Specify a specific
                                                  mimetype to use, such as
                                                  application/json.
 -u,--credentials <credentials>                   Credentials in the
                                                  format
                                                  username:password.
                                                  Example: --credentials
                                                  solr:SolrRocks
 -url,--solr-update-url <UPDATEURL>               Solr Update URL, the
                                                  full url to the update
                                                  handler, including the
                                                  /update.
 -v,--verbose                                     Enable more verbose
                                                  command output.

使用 bin/solr post 的範例

有多種方式可以使用 bin/solr post。本節將介紹幾個範例。

索引 JSON

將所有 JSON 檔案索引到 gettingstarted

$ bin/solr post -url https://127.0.0.1:8983/solr/gettingstarted/update *.json

索引 XML

將所有檔案副檔名為 .xml 的文件新增至名為 gettingstarted 的集合。

$ bin/solr post -url https://127.0.0.1:8983/solr/gettingstarted/update *.xml

將所有以 article 開頭,檔案副檔名為 .xml 的文件新增至在埠 8984 上執行的 Solr 的 gettingstarted 集合。

$ bin/solr post -url https://127.0.0.1:8984/solr/gettingstarted/update article*.xml

傳送 XML 引數以從 gettingstarted 刪除文件。

$ bin/solr post -url https://127.0.0.1:8983/solr/gettingstarted/update --mode args --type application/xml '<delete><id>42</id></delete>'

索引 CSV 和 JSON

從目前目錄將所有 CSV 和 JSON 檔案索引到 gettingstarted

$ bin/solr post -c gettingstarted --filetypes json,csv .

將 tab 分隔的檔案索引到 gettingstarted

$ bin/solr post -url https://127.0.0.1:8984/solr/signals/update --params "separator=%09" --type text/csv data.tsv

需要內容類型 (-type) 參數將檔案視為正確的類型,否則會忽略它並記錄警告,因為它不知道 .tsv 檔案的內容類型。CSV 處理器支援 separator 參數,並透過 -params 設定傳遞。

索引豐富文件 (PDF、Word、HTML 等)

將 PDF 檔案索引到 gettingstarted

$ bin/solr post -url https://127.0.0.1:8983/solr/gettingstarted/update a.pdf

自動偵測資料夾中的內容類型,並以遞迴方式掃描以將文件索引到 gettingstarted

$ bin/solr post -url https://127.0.0.1:8983/solr/gettingstarted/update afolder/

自動偵測資料夾中的內容類型,但將其限制為 PPT 和 HTML 檔案並索引到 gettingstarted

$ bin/solr post -url https://127.0.0.1:8983/solr/gettingstarted/update --filetypes ppt,html afolder/

索引到受密碼保護的 Solr (基本身份驗證)

以使用者 "solr" 和密碼 "SolrRocks" 的身分索引 PDF

$ bin/solr post -u solr:SolrRocks -url https://127.0.0.1:8983/solr/gettingstarted/update a.pdf

爬取網站以索引文件

爬取 Apache Solr 網站並深入一層,將頁面索引到 Solr。

請參閱試用 Solr Cell 以深入瞭解如何設定 Solr,以從網頁中擷取內容。

$ bin/solr post --mode web -c gettingstarted --recursive 1 --delay 1 https://solr.dev.org.tw/

標準輸入作為索引來源

您可以使用標準輸入作為要索引的資料來源。請注意,-out 提供來自 Solr 的原始回應。

$ echo '{commit: {}}' | bin/solr post --mode stdin -url https://127.0.0.1:8983/my_collection/update --out

原始資料作為索引來源

提供原始文件作為字串以進行索引。

$ bin/solr post -url https://127.0.0.1:8983/signals/update -mode args --type text/csv -out $'id,value\n1,0.47'