W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎勵
Config API可以使用類似REST的API調(diào)用來處理您的solrconfig.xml的各個方面。
此功能默認(rèn)啟用,并且在SolrCloud和獨(dú)立模式下的工作方式類似。許多通常編輯的屬性(如緩存大小和提交設(shè)置)和請求處理程序定義可以使用此API進(jìn)行更改。
使用此API時,solrconfig.xml不會更改。相反,所有編輯的配置都存儲在一個名為configoverlay.json的文件中。該configoverlay.json中值覆蓋solrconfig.xml中的值。
所有配置項,都可以通過向/config端點(diǎn)發(fā)送GET請求被檢索 - 其結(jié)果將是configoverlay.json 與在 solrconfig.xml 中合并設(shè)置而產(chǎn)生的有效配置:
curl http://localhost:8983/solr/techproducts/config
如果要將返回的結(jié)果限制到頂級部分,例如query,requestHandler或者updateHandler,那么請將該節(jié)的名稱追加到斜線之后的/config端點(diǎn)。例如,檢索所有請求處理程序的配置:
curl http://localhost:8983/solr/techproducts/config/requestHandler
為了進(jìn)一步限制返回的結(jié)果為頂層部分中的單個組件,請使用componentName請求參數(shù),例如返回/select請求處理程序的配置:
curl http://localhost:8983/solr/techproducts/config/requestHandler?componentName=/select
此API使用特定的命令來告訴Solr要添加到configoverlay.json的屬性或類型的屬性。這些命令作為與請求一起發(fā)送的數(shù)據(jù)的一部分傳遞。
配置命令分為3個不同的部分,它們在 solrconfig. xml 中操作各種數(shù)據(jù)結(jié)構(gòu),這些都在下面的內(nèi)容中進(jìn)行描述。
常見的屬性是那些經(jīng)常需要在Solr實(shí)例中自定義的屬性。它們使用兩個命令進(jìn)行操作:
使用這些命令配置的屬性是預(yù)定義的,并在下面列出。這些屬性的名稱是從它們在 solrconfig. xml 中找到的 xml 路徑派生的。
自定義請求處理程序、搜索組件和其他類型的本地化Solr組件(例如自定義查詢解析器、更新處理器等)可以使用特定命令添加、更新和刪除,以便修改組件。
語法在每種情況下都是類似的:add-<component-name>,update-<component-name>,和delete-<component-name>。命令名不區(qū)分大小寫,因此Add-RequestHandler,ADD-REQUESTHANDLER和add-requesthandler都是等效的。
在每種情況下,add- 命令都會將新配置添加到configoverlay.json,這將覆蓋solrconfig.xml組件中的任何其他設(shè)置;update- 命令覆蓋configoverlay.json中的現(xiàn)有設(shè)置;delete-命令從configoverlay.json中刪除設(shè)置。
從configoverlay.json刪除的設(shè)置不會從solrconfig.xml中刪除。
可用命令的完整列表如下所示:
這些命令是最常用的:
有關(guān)使用這些命令的示例,請參見下面的“創(chuàng)建和更新請求處理程序”一節(jié)。
配置API不允許您創(chuàng)建或編輯updateRequestProcessorChain元素。但是,可以創(chuàng)建updateProcessor條目并按名稱使用它們來創(chuàng)建鏈。
例如:
curl http://localhost:8983/solr/techproducts/config -H 'Content-type:application/json' -d '{
"add-updateprocessor" : { "name" : "firstFld",
"class": "solr.FirstFieldValueUpdateProcessorFactory",
"fieldName":"test_s"}}'
您可以直接在您的請求中使用此功能,方法是在updateRequestProcessorChain中的特定更新處理器中添加一個名為processor=firstFld的參數(shù)。
Solr允許用戶使用占位符格式:${variable_name:default_val}對solrconfig.xml進(jìn)行模板化。例如,您可以使用系統(tǒng)屬性,如:-Dvariable_name= my_customvalue來設(shè)置這些值。使用這些命令可以在運(yùn)行時實(shí)現(xiàn)相同的功能:
請求的結(jié)構(gòu)類似于使用其他命令的請求的結(jié)構(gòu),格式為"command":{"variable_name":"property_value"}。如有需要,您可以一次添加多個變量。
有關(guān)用戶定義屬性的更多信息,請參閱core.properties中的“用戶定義屬性”部分。
有關(guān)如何使用此類型命令的示例,另請參閱下面的“創(chuàng)建和更新用戶定義的屬性”部分。
通過使用此API,您將生成在solrconfig.xml中定義的屬性的JSON表示。為了理解API如何表示屬性,我們來看幾個例子。
以下是一個請求處理程序在 solrconfig 中的樣子:
<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 如下所示:
<searchComponent name="elevator" class="solr.QueryElevationComponent" >
<str name="queryFieldType">string</str>
<str name="config-file">elevate.xml</str>
</searchComponent>
與Config API相同的 searchComponent:
{
"add-searchcomponent":{
"name":"elevator",
"class":"QueryElevationComponent",
"queryFieldType":"string",
"config-file":"elevate.xml"
}
}
使用Config API刪除searchComponent:
{
"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>
與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以下位置設(shè)置autoCommit屬性:
<autoCommit>
<maxTime>15000</maxTime>
<openSearcher>false</openSearcher>
</autoCommit>
使用Config API定義相同的屬性:
{
"set-property": {
"updateHandler.autoCommit.maxTime":15000,
"updateHandler.autoCommit.openSearcher":false
}
}
Config API始終允許通過名稱更改任何組件的配置。然而,一些配置,如listener或initParams不需要 solrconfig. xml 中的名稱。為了能夠update和delete在configoverlay.json中相同的項目,必須使用 name 屬性。
此更改將 query.filterCache.autowarmCount 設(shè)置為1000項,并取消設(shè)置 query.filterCache.size。
curl http://localhost:8983/solr/techproducts/config -H 'Content-type:application/json' -d'{
"set-property" : {"query.filterCache.autowarmCount":1000},
"unset-property" :"query.filterCache.size"}'
使用/config/overlay端點(diǎn),您可以使用如下請求驗(yàn)證更改:
curl http://localhost:8983/solr/gettingstarted/config/overlay?omitHeader=true
您應(yīng)該會得到這樣的回應(yīng):
{
"overlay":{
"znodeVersion":1,
"props":{"query":{"filterCache":{
"autowarmCount":1000,
"size":25}}}}}
要創(chuàng)建請求處理程序,我們可以使用以下add-requesthandler命令:
curl http://localhost:8983/solr/techproducts/config -H 'Content-type:application/json' -d '{
"add-requesthandler" : {
"name": "/mypath",
"class":"solr.DumpRequestHandler",
"defaults":{ "x":"y" ,"a":"b", "rows":10 },
"useParams":"x"
}
}'
調(diào)用新的請求處理程序來檢查它是否被注冊:
curl http://localhost:8983/solr/techproducts/mypath?omitHeader=true
您應(yīng)該會看到下面的輸出:
{
"params":{
"indent":"true",
"a":"b",
"x":"y",
"rows":"10"},
"context":{
"webapp":"/solr",
"path":"/mypath",
"httpMethod":"GET"}}
要更新請求處理程序,您應(yīng)該使用以下update-requesthandler命令:
curl http://localhost:8983/solr/techproducts/config -H 'Content-type:application/json' -d '{
"update-requesthandler": {
"name": "/mypath",
"class":"solr.DumpRequestHandler",
"defaults": {"x":"new value for X", "rows":"20"},
"useParams":"x"
}
}'
作為另一個例子,我們將創(chuàng)建另一個請求處理程序,這次將“terms”組件添加為定義的一部分:
curl http://localhost:8983/solr/techproducts/config -H 'Content-type:application/json' -d '{
"add-requesthandler": {
"name": "/myterms",
"class":"solr.SearchHandler",
"defaults": {"terms":true, "distrib":false},
"components": [ "terms" ]
}
}'
這個命令設(shè)置一個用戶屬性。
curl http://localhost:8983/solr/techproducts/config -H'Content-type:application/json' -d '{
"set-user-property" : {"variable_name":"some_value"}}'
我們依然可以使用/config/overlay端點(diǎn)來驗(yàn)證所做的更改:
curl http://localhost:8983/solr/techproducts/config/overlay?omitHeader=true
我們希望看到這樣的輸出:
{"overlay":{
"znodeVersion":5,
"userProps":{
"variable_name":"some_value"}}
}
要取消設(shè)置變量,請執(zhí)行如下命令:
curl http://localhost:8983/solr/techproducts/config -H'Content-type:application/json' -d '{"unset-user-property" : "variable_name"}'
每個內(nèi)核都監(jiān)視與該內(nèi)核一起使用的配置集的ZooKeeper目錄。然而,在獨(dú)立模式下,沒有監(jiān)視(因?yàn)閆ooKeeper沒有運(yùn)行)。如果同一個節(jié)點(diǎn)中有多個核心使用相同的配置集,則只使用一個ZooKeeper監(jiān)視。例如,如果一個核心使用了configset'myconf',那么節(jié)點(diǎn)就會監(jiān)視/configs/myconf。每個通過API執(zhí)行的寫入操作都會“觸摸”目錄(設(shè)置一個空字節(jié)[]來觸發(fā)監(jiān)視),并通知所有監(jiān)視器。每個內(nèi)核會檢查Schema文件,solrconfig.xml或者configoverlay.json通過比較znode版本進(jìn)行修改,如果修改,則重新加載內(nèi)核。
如果params.json修改,則params對象只是在沒有核心重新加載的情況下更新(請參閱請求參數(shù)API部分了解更多有關(guān)params.json的信息)。
如果一個空的命令發(fā)送到/config端點(diǎn),那么使用這個配置集在所有內(nèi)核上觸發(fā)監(jiān)視。例如:
curl http://localhost:8983/solr/techproducts/config -H'Content-type:application/json' -d '{}'
直接編輯任何文件而不“接觸”該目錄將不會使其對所有節(jié)點(diǎn)可見。
通過SolrCore#registerConfListener()注冊監(jiān)聽器,組件可以監(jiān)視configset “touch”事件。
任何組件都可以使用以下方法注冊偵聽器
SolrCore#addConfListener(Runnable listener)
通知配置更改。如果修改的文件導(dǎo)致核心重新加載(即configoverlay.xml或架構(gòu)),這不是非常有用。組件可以使用它來重新加載他們感興趣的文件。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: