Solr

1. Solr 概要

  • 被設計為功能強大的文檔檢索應用程序(或平台),以提供根據用戶查詢所需的數據。
  • 是基於Lucene所構建的搜索服務器(Solr底層的核心技術是使用Lucene來實現的)。是一個具備功能管理界面的獨立應用程式,提供給企業能夠應用。
  • Lucene:
    • 是一套Java的全文檢索和搜尋的開放原始碼程式庫(全文搜尋引擎),由Apache軟體基金會支援和提供。
    • 提供了一個簡單卻強大的應用程式介面,能夠做*全文檢索,但Lucene不是一個完整的應用程式,而是一個代碼庫和 API。

*全文檢索:從文字或資料庫中自由萃取出訊息的技術,能夠在大量資料中快速搜索。執行全文檢索任務的程式,一般稱作搜尋引擎。

圖:Solr如何與應用程序整合(同時運作)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

例 : 線上商店應用程序提供用戶界面"購物車",為最終用戶進行購買的方式。而庫存管理應用程序將允許商店員工編輯產品信息。產品的metadata(元資料)將保存在某種數據庫或Solr中。

  • 依據以下步驟,Solr將使得online store搜索功能變得容易新增資料:
    1. 定義一個schema。該schema告訴Solr有關將要建立索引的文檔的內容。Solr的架構強大而靈活,允許您根據應用程序訂制Solr的行為。(在商店的例子中,該schema將為產品名稱、描述、價格、製造商等定義字段。)
    2. 提供documents,讓最終用戶未來可搜索。
    3. 在應用程序中公開搜索功能。

2. Solr’s Schema File

  • schema file定義核心中field(字段),儲存有關field的類型和field的詳細信息(index of the field)。
  • 該文件的名稱和位置可能會有所不同,具體取決於最初配置Solr的方式或以後對其進行修改的方式。
  • Solr中提供了兩種方式來配置schema,兩者只能選一:
    1. 預設方式:通過Schema API 來進行配置及更改,資訊儲存在核心目錄的conf/managed-schema(Solr默認使用的檔案)中。可以用"顯式配置託管模式功能"以使用備用文件名,但是Solr仍會自動更新文件的內容。
    2. 傳統方式:需手動編輯conf/schema.xml,編輯完後需重新開啟核心才會生效。

3. Solr config

  • 許多文件都是XML格式,但與config進行設定的API傾向於用JSON格式進行programmatic access(編程訪問)。

  • 在Solr’s Home將找到以下文件:

    • solr.xml:指定Solr server instance的configuration options(配置選項)。(更多solr.xml的詳細信息:Solr Cores和solr.xml。)
    • 每個Solr核心包含:
      • core.properties:每個核心定義的屬性。例如:名稱、核心所屬集合、Schema的位置等等。
      • solrconfig.xml:控制high-level(高端?)行為,包含請求handler(處理程序)和其他配置選項。可定義url(需請求handler),及使用其默認配置以執行plugins。例如:指定data directory(數據目錄)的備用位置。
      • managed-schema(或schema.xml):描述向Solr要求搜索至index的文件。該模式將文件定義為field的集合。操作者可以定義field type和field本身。field type的定義功能強大,並包含有關Solr如何處理傳入field values(字段值)和query values(查詢值)的資料。
      • data/:包含low-level index files的目錄。
  • Solr index配置透過schema.xml和solrconfig.xml完成 :

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

WordPress : 為一套內容管理系統,能夠透過它來快速建立及管理網站。
Solarium : Solr的用戶端庫

From : https://deeplearning.lipingyang.org/2017/05/13/apache-solr-schema-explained/


4. Solr API

Schema API用HTTP API管理Schema的設定,可以讀、寫Schema,並通過API來更新Schema,solr會自動更新核心。用這種方式來動態的定義模式,就不用自己手動編寫config。

但是修改Schema並不會自動重新索引已索引的文件,只會對後續的文件起作用,如果必要的話則需手動重新索引(刪除原來的,再重新提交文件)。


solr 7.4.0 server 安裝

1. download java 14.0.2

Step 1. Installing OpenJDK version 14 on Ubuntu 20.04 LTS

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install openjdk-14-jdk
## for servers try headless version ##
$ sudo apt install openjdk-14-jdk-headless

Step 2. Verification(check version)

$ java --version

Step 3. Set up JAVA_HOME environment variable

update the environment variables config file

$ sudo nano /etc/environment
## OR ##
$ sudo vim /etc/environment

and set JAVA_HOME as follows:

JAVA_HOME="/usr/lib/jvm/java-14-openjdk-amd64"

2. solr 7.4.0 server installation

Step 1. 取得Solr的下載地址,然後使用wget命令下載
https://archive.apache.org/dist/lucene/solr/

wget https://archive.apache.org/dist/lucene/solr/7.4.0/solr-7.4.0.tgz

Step 2. 下載完成後解壓縮檔案

tar -zxvf solr-7.4.0.tgz

Step 3. 啟動

cd solr-7.4.0
bin/solr start

Then~~~Happy searching!


創建solr core

  1. 先在solr-7.4.0/server/solr/中創建一個資料夾(例:test1)

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. 在test1中複製一個configsets中的conf (提示有說明需存在instanceDir和dataDir
    才能create core)
cp -r configsets/_default/conf/ test1/
  1. 到Solr Core Admin中點選"Add Core", 將name及instanceDir輸入一開始建立的資料夾名(test1), then the new core(test1) will be created.

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  1. success!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

其他方式: https://wbkuo.pixnet.net/blog/post/156101696


配置schema

  1. solr-7.4.0/server/solr/test1/conf/ 中打開 managed-schemaschema.xml
  2. solr7.4 with ckan 2.9 schema 貼上
  3. Save it!

有關solr的連結們:
https://wbkuo.pixnet.net/blog/category/2123399
https://lucene.apache.org/solr/8_7_0/changes/Changes.html


Solr upgrade

Solr 7.4 to 7.5

1. Schema Changes
  • 從Solr 7.0開始,Solr的schema field-guessing 已為所有的 _txt fields 創建 _str fields, 並在query(查詢)過後 return 這些 fields (by default). 從7.5開始, _str fields 在 default 不會再 return fields, 但仍可以用, 只是需要透過fl(某個參數)來進行查詢.
  • 從Solr v4開始不再運作的Standard Filter 已被 removed.
2. Index Merge Policy
  • 使用 TieredMergePolicy (Solr 的default merge policy ) 時, optimize 以及 expungeDeletes 將遵循 maxMergedSegmentMB 的配置參數, 參數的 default 值為 5000 (5GB)。
  • 如果絕對有必要控制優化後 segment 的數量, 須把 maxSegments設為正整數。 (盡量把maxSegments 設為高於 1 )。
  • TieredMergePolicy 會比以前更積極 reclaim(收回) 超出 maxMergedSegmentMB 部分的resources。
3. UIMA(Unstructured Information Management Application) Removed
  • The UIMA contrib 已被永久 removed from Solr

UIMA官網: https://uima.apache.org/

4. Logging
  • server/resources/log4j2.xml 的default位置位於Solr的 logging configuration file 中
  • Windows users 的bug已修正: Solr的範例log files(bin/solr start -e) 會被放到正確位置 example/ 而不是 server

Solr 7.5 to 7.6

1. Collections
  • 使用CLUSTERPROP命令以設定 cluster-wide default cluster properties 的 JSON 參數已更改。

    舊的語法將 defaults nest(崁套)至 clusterDefaults中,而新的語法只使用 defaults. The command to use is still set-obj-property

nest(崁套): 在已有的表格、圖像或圖層中再加一個或多個表格、圖像或圖層,亦或兩個物體有裝配(可以互相組合)關係時,將一個物體嵌入另一物體的方法。

An example of the new syntax is: (JSON)

{
  "set-obj-property": {
    "defaults" : {
      "collection": {
        "numShards": 2,
        "nrtReplicas": 1,
        "tlogReplicas": 1,
        "pullReplicas": 1
      }
    }
  }
}

到Solr 9之前仍支持舊語法,但建議用戶盡快使用新語法。

  • min_rf這個參數已被棄用,不再需要被提供去查看已實現的replication factor(複制因子)。replication factor 將會與 response 一起 return 給客戶端。
2. Autoscaling
  • 現在 autoscaling policy 被當作 default strategy, 可選擇創建 (new replicas)新副本 或 新collections副本 上的(node)節點。而所有的users都必須使用 default policy, 在default情況下, 會依據core的數量和可用的freedisk排序nodes, 這表示 "最少core數量和最高可用freedisk的node" 會被優先選擇去創建新的core。

  • 以上的更改對 maxShardsPerNode 參數有兩個影響:

    1. 實施 autoscaling policy 時,對maxShardsPerNode移除了使用限制, 現在創建collectiong時可設定此參數。
    2. 移除 autoscaling policy 的default設定: maxShardsPerNode=1。變成無論是否有設置autoscaling policy, 都會正確設置maxShardsPerNode(如果有需要的話)。
      maxShardsPerNode 的default值仍為 1, 但如果需要unlimited的舊行為則把 maxSharedsPerNode 設為-1即可。.
3. DirectoryFactory
  • Lucene 引入 ByteBuffersDirectoryFactory 以代替 RAMDirectoryFactory( RAMDirectoryFactory 在Solr 9中會被移除)。
  • 雖然現在仍然鼓勵users使用 NRTCachingDirectoryFactory(可允許 Lucene 選擇最佳的 directory factory 來使用), 但如果已明確使用 RAMDirectoryFactory 來配置 Solr 的話, 建議在Solr 9 發布前更改至新的替代方式。

Solr 7.6 to 7.7

1. Admin UI
  • Admin UI 現在會顯示一個 login 畫面讓在 cluster (集群)中的 users 進行身分驗證。
  • 有 Basic Authentication 的 clusters 會提示 users 輸入他們的 username and password. 在 clusters 使用 Kerberos Authentication  進行配置並指導 users 去配置它們的 browser 以提供適當的Kerberos ticket。
  • login 畫面是裝飾用的, Admin UI 主要目的在於觸發 Solr requests, 使 7.7 以前版本的user能夠進行身分驗證且確認至今仍在使用。
2. Distributed Requests
  • 由於安全考量, shards 這個參數接收 distributed requests 時需手動選擇 shards 和 replicas, 以根據可接受值的whitelist(白名單) 檢查(or核對?) nodes。
  • 在 SolrCloud mode 中, whitelist 會自動配置所有 live nodes。在 standalone mode(獨立模式)的 default 中, whitelist 是空的。
  • 在 standalone mode 中使用 shards 參數的 users 可透過 solrconfig.xml 中的任何 shardHandler 中的shardsWhitelistproperty 來更改此值。

Solr 7.4 升級至 7.7

直接安裝solr 7.7, 創建core(參考這裡)之後配置schema:

  1. solr-7.7.0/server/solr/test1/conf/ 中打開 managed-schemaschema.xml
  2. solr7.4 with ckan 2.9 schema 貼上。
  3. 存檔後即可和ckan連接使用。

目前ckan 2.9 - solr 7.7的問題

1. 依照格式或標籤進行篩選

20210416測試時可以正常篩選

(1) 一個資料集如果有兩(或以上)個檔案的話無法依格式分類,但只有一個檔案的話可以。

(2) 可以手動設定標籤,但無法進行篩選。

2. 模糊搜尋

(英文字不分大小寫皆可搜尋, 例: 搜尋"aids"等同於搜尋"AIDS")
(中文斷詞目前好像是依照字和字之間有沒有空白分辨的, 例: "檔 案"的"檔"與"案"是分開的兩個詞, "檔案"則是同一個詞)

(1) 資料集標題及敘述, 檔案名稱及敘述的搜尋:

"2016"

  • 2016腸病毒

"腸病毒"

  • 2016腸病毒

"16腸病毒"

  • 2016腸病毒

"area"

  • Area, Age, and Gender Statistical table - AIDS (中文敘述之間無空白)
  • Area, Age, and Gender Statistical table - AIDS - v2 (中文敘述之間有空白)

"AIDS"

  • aids_diagnose
  • Area, Age, and Gender Statistical table - AIDS
  • Area, Age, and Gender Statistical table - AIDS - v2
  • 愛滋病感染死亡人數統計

"愛滋病"

  • aids_diagnose
  • Area, Age, and Gender Statistical table - AIDS - v2
  • 愛滋病感染死亡人數統計

"die"

  • 愛滋病感染死亡人數統計 (在檔案標題中找到)

"統計表"

  • Area, Age, and Gender Statistical table - AIDS - v2

"資料集"

  • (沒有結果)

"資 料 集"

  • 2 0 1 6 腸 病 毒 (在資料集敘述中找到)
    "有"
  • 2 0 1 6 腸 病 毒 (在檔案敘述中找到)

"檔 案"

  • 2 0 1 6 腸 病 毒 (在檔案敘述中找到)
(2) 用副檔名搜尋:

"csv"

  • 可搜尋副檔名為"csv"的資料

"json"

  • 可搜尋副檔名為"json"的資料