Config API
Config API 允許您使用類似 REST 的 API 呼叫來操作 `solrconfig.xml` 的各個方面。
此功能預設為啟用,並且在 SolrCloud、使用者管理的叢集和單節點安裝中運作方式類似。許多常用的編輯屬性(例如快取大小和提交設定)以及請求處理器定義都可以使用此 API 進行變更。
使用此 API 時,不會變更 `solrconfig.xml`。相反地,所有編輯的設定都會儲存在名為 `configoverlay.json` 的檔案中。`configoverlay.json` 中的值會覆寫 `solrconfig.xml` 中的值。
Config API 端點
所有 Config API 端點都是特定於集合的,這表示此 API 一次只能檢查或修改單個集合的設定。
-
`_collection_/config`:擷取完整生效的設定,或修改設定。使用 GET 擷取,使用 POST 執行命令。
-
`_collection_/config/overlay`:僅擷取 `configoverlay.json` 中的詳細資料,移除直接在 `solrconfig.xml` 中定義或透過預設隱含定義的任何選項。
-
`_collection_/config/params`:建立可以覆寫或取代 `solrconfig.xml` 中定義的參數的參數集。如需有關此端點的詳細資訊,請參閱 請求參數 API。
擷取設定
所有設定項目都可以透過向 `/config` 端點傳送 GET 請求來擷取。
-
V1 API
-
V2 API
https://127.0.0.1:8983/solr/techproducts/config
https://127.0.0.1:8983/api/collections/techproducts/config
回應將是將 `configoverlay.json` 中的設定與 `solrconfig.xml` 中的設定合併後產生的 Solr 設定。
可以將傳回的設定限制為頂層區段,例如 `query`、`requestHandler` 或 `updateHandler`。若要執行此操作,請將區段名稱附加到 `config` 端點。例如,若要擷取所有請求處理器的設定
-
V1 API
-
V2 API
https://127.0.0.1:8983/solr/techproducts/config/requestHandler
https://127.0.0.1:8983/api/collections/techproducts/config/requestHandler
輸出將會是在 `solrconfig.xml` 中定義的每個請求處理器的詳細資料、由 Solr 隱含定義,以及使用此 Config API 儲存在 `configoverlay.json` 中的定義。若要查看隱式請求處理器的設定,請在請求中新增 `expandParams=true`。如需使用此命令的範例,請參閱上面連結的隱式請求處理器文件。
可以新增為路徑參數的可用頂層區段為:`query`、`requestHandler`、`searchComponent`、`updateHandler`、`queryResponseWriter`、`initParams`、`znodeVersion`、`listener`、`directoryFactory`、`indexConfig` 和 `codecFactory`。
若要進一步將請求限制為頂層區段中的單個元件,請使用 `componentName` 請求參數。
例如,若要傳回 `/select` 請求處理器的設定
-
V1 API
-
V2 API
https://127.0.0.1:8983/solr/techproducts/config/requestHandler?componentName=/select
https://127.0.0.1:8983/api/collections/techproducts/config/requestHandler?componentName=/select
此命令的輸出將會類似於
{
"config":{"requestHandler":{"/select":{
"name": "/select",
"class": "solr.SearchHandler",
"defaults":{
"echoParams": "explicit",
"rows":10
}}}}
}
將物件限制在頂層區段中的功能僅限於請求處理器 (`requestHandler`)、搜尋元件 (`searchComponent`) 和回應寫入器 (`queryResponseWriter`)。
修改設定的指令
此 API 使用 POST 請求的特定指令,告知 Solr 要在 configoverlay.json
中新增或修改哪個屬性或屬性類型。這些指令會連同要新增或修改的屬性或元件的資料一起傳遞。
用於修改的 Config API 指令分為 3 種類型,每種類型都會操作 solrconfig.xml
中的特定資料結構。這些類型包括:
-
用於常用屬性的
set-property
和unset-property
-
用於自訂處理常式和本機元件的元件專屬
add-
、update-
和delete-
指令 -
用於使用者定義屬性的
set-user-property
和unset-user-property
常用屬性的指令
常用屬性是指 Solr 執行個體中經常自訂的屬性。它們透過兩個指令來操作:
-
set-property
:設定已知的屬性。屬性的名稱是預先定義且固定的。如果屬性已設定,此指令會覆寫先前的設定。 -
unset-property
:移除使用set-property
指令設定的屬性。
可以使用 set-property
和 unset-property
設定的屬性是預先定義的,並列於下方。這些屬性的名稱衍生自它們在 solrconfig.xml
中的 XML 路徑。
更新處理常式設定
請參閱提交和交易記錄,以了解這些設定的預設值和可接受的值。
-
updateHandler.autoCommit.maxDocs
-
updateHandler.autoCommit.maxTime
-
updateHandler.autoCommit.openSearcher
-
updateHandler.autoSoftCommit.maxDocs
-
updateHandler.autoSoftCommit.maxTime
-
updateHandler.commitWithin.softCommit
查詢設定
請參閱快取和查詢暖機,以了解這些設定的預設值和可接受的值。
快取和快取大小
-
query.filterCache.class
-
query.filterCache.size
-
query.filterCache.initialSize
-
query.filterCache.autowarmCount
-
query.filterCache.maxRamMB
-
query.filterCache.regenerator
-
query.queryResultCache.class
-
query.queryResultCache.size
-
query.queryResultCache.initialSize
-
query.queryResultCache.autowarmCount
-
query.queryResultCache.maxRamMB
-
query.queryResultCache.regenerator
-
query.documentCache.class
-
query.documentCache.size
-
query.documentCache.initialSize
-
query.documentCache.autowarmCount
-
query.documentCache.regenerator
-
query.fieldValueCache.class
-
query.fieldValueCache.size
-
query.fieldValueCache.initialSize
-
query.fieldValueCache.autowarmCount
-
query.fieldValueCache.regenerator
查詢大小和暖機
-
query.maxBooleanClauses
-
query.enableLazyFieldLoading
-
query.useFilterForSortedQuery
-
query.queryResultWindowSize
-
query.queryResultMaxDocCached
查詢斷路器
請參閱斷路器以了解更多詳細資訊
-
query.memEnabled
-
query.memThreshold
RequestDispatcher 設定
請參閱RequestDispatcher,以了解這些設定的預設值和可接受的值。
-
requestDispatcher.handleSelect
-
requestDispatcher.requestParsers.multipartUploadLimitInKB
-
requestDispatcher.requestParsers.formdataUploadLimitInKB
-
requestDispatcher.requestParsers.addHttpRequestToContext
常用屬性的範例
建構修改或新增這些屬性之一的指令,遵循此模式:
{"set-property":{"<property>": "<value>"}}
增加 updateHandler.autoCommit.maxTime
的請求看起來會像這樣:
-
V1 API
-
V2 API
curl -X POST -H 'Content-type: application/json' -d '{"set-property":{"updateHandler.autoCommit.maxTime":15000}}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type: application/json' -d '{"set-property":{"updateHandler.autoCommit.maxTime":15000}}' https://127.0.0.1:8983/api/collections/techproducts/config
您可以使用 config/overlay
端點來驗證屬性是否已新增至 configoverlay.json
。
-
V1 API
-
V2 API
curl https://127.0.0.1:8983/solr/techproducts/config/overlay?omitHeader=true
curl https://127.0.0.1:8983/api/collections/techproducts/config/overlay?omitHeader=true
輸出
{
"overlay": {
"znodeVersion": 1,
"props": {
"updateHandler": {
"autoCommit": {"maxTime": 15000}
}
}}}
若要取消設定屬性:
-
V1 API
-
V2 API
curl -X POST -H 'Content-type: application/json' -d '{"unset-property": "updateHandler.autoCommit.maxTime"}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type: application/json' -d '{"unset-property": "updateHandler.autoCommit.maxTime"}' https://127.0.0.1:8983/api/collections/techproducts/config
處理常式和元件的指令
可以使用針對要修改的元件類型而定的特定指令,新增、更新和刪除請求處理常式、搜尋元件和其他類型的本機化 Solr 元件(例如查詢剖析器、更新處理器等)。
每個案例中的語法都類似:add-<_component-name_>
、update-<_component-name_>
和 delete-<_component-name_>
。指令名稱不區分大小寫,因此 Add-RequestHandler
、ADD-REQUESTHANDLER
和 add-requesthandler
是等效的。
在每個案例中,add-
指令都會將新的組態新增至 configoverlay.json
,這會覆寫 solrconfig.xml
中元件的任何其他設定。
update-
指令會覆寫 configoverlay.json
中的現有設定。
delete-
指令會從 configoverlay.json
中移除設定。
如果從 configoverlay.json
中移除設定,它們不會從 solrconfig.xml
中移除(如果它們剛好重複在那裡)。
可用指令的完整清單如下:
元件的基本指令
這些指令是最常用的指令
-
add-requesthandler
-
update-requesthandler
-
delete-requesthandler
-
add-searchcomponent
-
update-searchcomponent
-
delete-searchcomponent
-
add-initparams
-
update-initparams
-
delete-initparams
-
add-queryresponsewriter
-
update-queryresponsewriter
-
delete-queryresponsewriter
元件的進階指令
這些指令允許將更進階的自訂設定註冊到 Solr
-
add-queryparser
-
update-queryparser
-
delete-queryparser
-
add-valuesourceparser
-
update-valuesourceparser
-
delete-valuesourceparser
-
add-transformer
-
update-transformer
-
delete-transformer
-
add-updateprocessor
-
update-updateprocessor
-
delete-updateprocessor
-
add-queryconverter
-
update-queryconverter
-
delete-queryconverter
-
add-listener
-
update-listener
-
delete-listener
-
add-expressible
-
update-expressible
-
delete-expressible
處理常式和元件指令的範例
若要建立請求處理常式,我們可以使用 add-requesthandler
指令
curl -X POST -H 'Content-type:application/json' -d '{
"add-requesthandler": {
"name": "/mypath",
"class": "solr.DumpRequestHandler",
"defaults":{ "x": "y" ,"a": "b", "rows":10 },
"useParams": "x"
}
}' https://127.0.0.1:8983/solr/techproducts/config
-
V1 API
-
V2 API
curl -X POST -H 'Content-type:application/json' -d '{
"add-requesthandler": {
"name": "/mypath",
"class": "solr.DumpRequestHandler",
"defaults": { "x": "y" ,"a": "b", "rows":10 },
"useParams": "x"
}
}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type:application/json' -d '{
"add-requesthandler": {
"name": "/mypath",
"class": "solr.DumpRequestHandler",
"defaults": { "x": "y" ,"a": "b", "rows":10 },
"useParams": "x"
}
}' https://127.0.0.1:8983/api/collections/techproducts/config
呼叫新的請求處理常式以檢查它是否已註冊
$ curl https://127.0.0.1:8983/solr/techproducts/mypath?omitHeader=true
您應該會看到以下輸出:
{
"params":{
"indent": "true",
"a": "b",
"x": "y",
"rows": "10"},
"context":{
"webapp": "/solr",
"path": "/mypath",
"httpMethod": "GET"}}
若要更新請求處理常式,您應該使用 update-requesthandler
指令
-
V1 API
-
V2 API
curl -X POST -H 'Content-type:application/json' -d '{
"update-requesthandler": {
"name": "/mypath",
"class": "solr.DumpRequestHandler",
"defaults": {"x": "new value for X", "rows": "20"},
"useParams": "x"
}
}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type:application/json' -d '{
"update-requesthandler": {
"name": "/mypath",
"class": "solr.DumpRequestHandler",
"defaults": {"x": "new value for X", "rows": "20"},
"useParams": "x"
}
}' https://127.0.0.1:8983/api/collections/techproducts/config
作為第二個範例,我們將建立另一個請求處理常式,這次將 'terms' 元件新增為定義的一部分
-
V1 API
-
V2 API
curl -X POST -H 'Content-type:application/json' -d '{
"add-requesthandler": {
"name": "/myterms",
"class": "solr.SearchHandler",
"defaults": {"terms": true, "distrib":false},
"components": ["terms"]
}
}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type:application/json' -d '{
"add-requesthandler": {
"name": "/myterms",
"class": "solr.SearchHandler",
"defaults": {"terms": true, "distrib":false},
"components": ["terms"]
}
}' https://127.0.0.1:8983/api/collections/techproducts/config
最後,我們將使用 delete-requesthandler
指令來移除請求處理常式
-
V1 API
-
V2 API
curl -X POST -H 'Content-type:application/json' -d '{
"delete-requesthandler": "/myterms"
}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type:application/json' -d '{
"delete-requesthandler": "/myterms"
}' https://127.0.0.1:8983/api/collections/techproducts/config
使用者定義屬性的指令
Solr 允許使用者使用預留位置格式 ${variable_name:default_val}
來範本化 solrconfig.xml
。您可以使用系統屬性來設定值,例如,-Dvariable_name= my_customvalue
。在執行階段,也可以使用這些指令來完成相同目的
-
set-user-property
:設定使用者定義的屬性。如果屬性已設定,此指令會覆寫先前的設定。 -
unset-user-property
:移除使用者定義的屬性。
請求的結構與使用其他指令的請求結構類似,格式為 "command":{"variable_name": "property_value"}
。如有必要,您可以一次新增多個變數。
如需有關使用者定義屬性的詳細資訊,請參閱core.properties 中的使用者定義屬性章節。
另請參閱下方的建立和更新使用者定義的屬性章節,以取得如何使用此類型指令的範例。
建立和更新使用者定義的屬性
此指令會設定使用者屬性。
-
V1 API
-
V2 API
curl -X POST -H 'Content-type:application/json' -d '{"set-user-property": {"variable_name": "some_value"}}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type:application/json' -d '{"set-user-property": {"variable_name": "some_value"}}' https://127.0.0.1:8983/api/collections/techproducts/config
同樣地,我們可以使用 /config/overlay
端點來驗證變更是否已完成
-
V1 API
-
V2 API
curl https://127.0.0.1:8983/solr/techproducts/config/overlay?omitHeader=true
curl https://127.0.0.1:8983/api/collections/techproducts/config/overlay?omitHeader=true
我們會預期看到類似這樣的輸出
{"overlay":{
"znodeVersion":5,
"userProps":{
"variable_name": "some_value"}}
}
若要取消設定變數,請發出類似這樣的指令
-
V1 API
-
V2 API
curl -X POST -H 'Content-type:application/json' -d '{"unset-user-property": "variable_name"}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type:application/json' -d '{"unset-user-property": "variable_name"}' https://127.0.0.1:8983/api/collections/techproducts/config
updateRequestProcessorChain 元素
Config API 不允許您建立或編輯 updateRequestProcessorChain
元素。但是,可以建立 updateProcessor
項目,並透過名稱使用它們來建立鏈。
例如:
-
V1 API
-
V2 API
curl -X POST -H 'Content-type:application/json' -d '{"add-updateprocessor":
{"name": "firstFld",
"class": "solr.FirstFieldValueUpdateProcessorFactory",
"fieldName": "test_s"}
}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type:application/json' -d '{"add-updateprocessor":
{"name": "firstFld",
"class": "solr.FirstFieldValueUpdateProcessorFactory",
"fieldName": "test_s"}
}' https://127.0.0.1:8983/api/collections/techproducts/config
您可以透過在 updateRequestProcessorChain
中為稱為 processor=firstFld
的特定更新處理器新增參數,直接在您的請求中使用此內容。
如何將 solrconfig.xml 屬性對應到 JSON
透過使用此 API,您將會產生 solrconfig.xml
中定義的屬性的 JSON 表示法。若要了解如何使用 API 表示屬性,讓我們看一下幾個範例。
以下是 solrconfig.xml
中的請求處理常式的外觀
<requestHandler name="/query" class="solr.SearchHandler">
<lst name="defaults">
<str name="echoParams">explicit</str>
<int name="rows">10</str>
</lst>
</requestHandler>
使用 Config API 定義的相同請求處理常式會像這樣:
{
"add-requesthandler":{
"name": "/query",
"class": "solr.SearchHandler",
"defaults":{
"echoParams": "explicit",
"rows": 10
}
}
}
solrconfig.xml
中的 QueryElevationComponent 搜尋元件看起來會像這樣:
<searchComponent name="elevator" class="solr.QueryElevationComponent" >
<str name="queryFieldType">string</str>
<str name="config-file">elevate.xml</str>
</searchComponent>
而使用 Config API 的相同搜尋元件如下:
{
"add-searchcomponent":{
"name": "elevator",
"class": "solr.QueryElevationComponent",
"queryFieldType": "string",
"config-file": "elevate.xml"
}
}
使用 Config API 移除搜尋元件:
{
"delete-searchcomponent": "elevator"
}
簡單的醒目提示器在 solrconfig.xml
中看起來會像這樣(為了節省空間,範例已截斷)
<searchComponent class="solr.HighlightComponent" name="highlight">
<highlighting>
<fragmenter name="gap"
default="true"
class="solr.highlight.GapFragmenter">
<lst name="defaults">
<int name="hl.fragsize">100</int>
</lst>
</fragmenter>
<formatter name="html"
default="true"
class="solr.highlight.HtmlFormatter">
<lst name="defaults">
<str name="hl.simple.pre"><![CDATA[<em>]]></str>
<str name="hl.simple.post"><![CDATA[</em>]]></str>
</lst>
</formatter>
<encoder name="html" class="solr.highlight.HtmlEncoder" />
...
</highlighting>
</searchComponent>
使用 Config API 的相同醒目提示器:
{
"add-searchcomponent": {
"name": "highlight",
"class": "solr.HighlightComponent",
"": {
"gap": {
"default": "true",
"name": "gap",
"class": "solr.highlight.GapFragmenter",
"defaults": {
"hl.fragsize": 100
}
}
},
"html": [{
"default": "true",
"name": "html",
"class": "solr.highlight.HtmlFormatter",
"defaults": {
"hl.simple.pre": "before-",
"hl.simple.post": "-after"
}
}, {
"name": "html",
"class": "solr.highlight.HtmlEncoder"
}]
}
}
在 solrconfig.xml
中設定 autoCommit 屬性
<autoCommit>
<maxTime>15000</maxTime>
<openSearcher>false</openSearcher>
</autoCommit>
使用 Config API 定義相同的屬性
{
"set-property": {
"updateHandler.autoCommit.maxTime":15000,
"updateHandler.autoCommit.openSearcher":false
}
}
Config API 的運作方式
當使用 SolrCloud 時,每個核心都會監看 ZooKeeper 目錄中用於該核心的 configset。如果同一節點中有多個核心使用相同的 configset,則只會使用一個 ZooKeeper 監看。
- 提示
-
在使用者管理的叢集或單一節點安裝中,沒有監看(因為 ZooKeeper 沒有執行)。
例如,如果核心使用 configset 'myconf',則節點會監看 /configs/myconf
。透過 API 執行的每個寫入作業都會「觸及」目錄,而且會通知所有監看者。每個核心都會檢查結構描述檔案、solrconfig.xml
或 configoverlay.json
是否已透過比較 znode
版本來修改。如果有任何已修改,則會重新載入核心。
如果修改了 params.json
,則只會更新 params 物件,而不會重新載入核心(如需有關 params.json
的詳細資訊,請參閱請求參數 API)。
空白指令
如果將空白指令傳送至 /config
端點,則會觸發使用此 configset 的所有核心上的監看。例如:
-
V1 API
-
V2 API
curl -X POST -H 'Content-type:application/json' -d '{}' https://127.0.0.1:8983/solr/techproducts/config
curl -X POST -H 'Content-type:application/json' -d '{}' https://127.0.0.1:8983/api/collections/techproducts/config
直接編輯任何檔案而不「觸及」目錄**不會**使其對所有節點可見。
元件可以透過使用 SolrCore#registerConfListener()
註冊監聽器來監看 configset 「觸及」事件。