Solr配置API:Config API

2018-12-15 14:20 更新

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中的值。

配置API入口點(diǎn)

  • /config:檢索或修改配置。GET檢索和POST執(zhí)行命令
  • /config/overlay:單獨(dú)檢索configoverlay.json細(xì)節(jié)
  • /config/params:允許創(chuàng)建參數(shù)集,可以覆蓋或取代在solrconfig.xml中定義的參數(shù)。請參閱請求參數(shù)API部分以獲取更多詳細(xì)信息。

檢索配置

所有配置項,都可以通過向/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)行操作:

  • set-property:設(shè)置一個眾所周知的屬性。屬性的名稱是預(yù)定義的并且是固定的。如果該屬性已經(jīng)設(shè)置,該命令將覆蓋以前的設(shè)置。
  • unset-property:使用該set-property命令刪除一個屬性集。

使用這些命令配置的屬性是預(yù)定義的,并在下面列出。這些屬性的名稱是從它們在 solrconfig. xml 中找到的 xml 路徑派生的。

  • updateHandler.autoCommit.maxDocs
  • updateHandler.autoCommit.maxTime
  • updateHandler.autoCommit.openSearcher
  • updateHandler.autoSoftCommit.maxDocs
  • updateHandler.autoSoftCommit.maxTime
  • updateHandler.commitWithin.softCommit
  • updateHandler.indexWriter.closeWaitsForMerges
  • query.filterCache.class
  • query.filterCache.size
  • query.filterCache.initialSize
  • query.filterCache.autowarmCount
  • query.filterCache.regenerator
  • query.queryResultCache.class
  • query.queryResultCache.size
  • query.queryResultCache.initialSize
  • query.queryResultCache.autowarmCount
  • 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.useFilterForSortedQuery
  • query.queryResultWindowSize
  • query.queryResultMaxDocCached
  • query.enableLazyFieldLoading
  • query.boolToFilterOptimizer
  • query.maxBooleanClauses
  • jmx.agentId
  • jmx.serviceUrl
  • jmx.rootName
  • requestDispatcher.handleSelect
  • requestDispatcher.requestParsers.multipartUploadLimitInKB
  • requestDispatcher.requestParsers.formdataUploadLimitInKB
  • requestDispatcher.requestParsers.enableRemoteStreaming
  • requestDispatcher.requestParsers.addHttpRequestToContext

自定義處理程序和本地組件的命令

自定義請求處理程序、搜索組件和其他類型的本地化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中刪除。

可用命令的完整列表如下所示:

這些命令是最常用的:

  • 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-runtimelib
  • update-runtimelib
  • delete-runtimelib

有關(guān)使用這些命令的示例,請參見下面的“創(chuàng)建和更新請求處理程序”一節(jié)。

什么是updateRequestProcessorChain?

配置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)相同的功能:

  • set-user-property:設(shè)置用戶定義的屬性。如果該屬性已經(jīng)設(shè)置,則該命令將覆蓋以前的設(shè)置。
  • unset-user-property:刪除用戶定義的屬性。

請求的結(jié)構(gòu)類似于使用其他命令的請求的結(jié)構(gòu),格式為"command":{"variable_name":"property_value"}。如有需要,您可以一次添加多個變量。

有關(guān)用戶定義屬性的更多信息,請參閱core.properties中的“用戶定義屬性”部分。

有關(guān)如何使用此類型命令的示例,另請參閱下面的“創(chuàng)建和更新用戶定義的屬性”部分。

如何將solrconfig.xml屬性映射到JSON

通過使用此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的組件命名

Config API始終允許通過名稱更改任何組件的配置。然而,一些配置,如listener或initParams不需要 solrconfig. xml 中的名稱。為了能夠update和delete在configoverlay.json中相同的項目,必須使用 name 屬性。

Config API示例

創(chuàng)建和更新通用屬性

此更改將 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)建和更新請求處理程序

要創(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" ]
  }
}'

創(chuàng)建和更新用戶定義的屬性

這個命令設(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"}'

Config API的工作原理

每個內(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)),這不是非常有用。組件可以使用它來重新加載他們感興趣的文件。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號