Solr配置:Config API详解

Solr配置: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

1
http://localhost:8983/solr/techproducts/config

V2 API

1
http://localhost:8983/api/collections/techproducts/config

响应将是通过合并configoverlay.jsonsolrconfig.xml中的设置产生的Solr配置。

可以将返回的配置限制为顶级部分,如queryrequestHandlerupdateHandler。为此,将部分名称附加到config端点。例如,要检索所有请求处理器的配置:

V1 API

1
http://localhost:8983/solr/techproducts/config/requestHandler

V2 API

1
http://localhost:8983/api/collections/techproducts/config/requestHandler

输出将是在solrconfig.xml中定义、由Solr隐式定义以及通过存储在configoverlay.json中的Config API定义的每个请求处理器的详细信息。要查看隐式请求处理器的配置,请将expandParams=true添加到请求中。有关使用此命令的示例,请参见上面链接的隐式请求处理器的文档。

可以作为路径参数添加的可用顶级部分包括:queryrequestHandlersearchComponentupdateHandlerqueryResponseWriterinitParamsznodeVersionlistenerdirectoryFactoryindexConfigcodecFactory

要进一步将请求限制为顶级部分中的单个组件,请使用componentName请求参数。

例如,要返回/select请求处理器的配置:

V1 API

1
http://localhost:8983/solr/techproducts/config/requestHandler?componentName=/select

V2 API

1
http://localhost:8983/api/collections/techproducts/config/requestHandler?componentName=/select

此命令的输出将类似于:

1
2
3
4
5
6
7
8
9
{
"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-propertyunset-property
  • 用于<<处理器和组件命令,自定义处理器和本地组件>>的特定于组件的add-update-delete-命令
  • 用于<<用户定义属性命令,用户定义属性>>的set-user-propertyunset-user-property

通用属性命令

通用属性是Solr实例中经常自定义的属性。它们通过两个命令操作:

  • set-property:设置已知属性。属性名称是预定义和固定的。如果属性已经设置,此命令将覆盖以前的设置。
  • unset-property:删除使用set-property命令设置的属性。

可以使用set-propertyunset-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.handleSelect
  • requestDispatcher.requestParsers.multipartUploadLimitInKB
  • requestDispatcher.requestParsers.formdataUploadLimitInKB
  • requestDispatcher.requestParsers.addHttpRequestToContext

通用属性示例

构造修改或添加这些属性之一的命令遵循此模式:

1
{"set-property":{"<property>": "<value>"}}

增加updateHandler.autoCommit.maxTime的请求如下:

V1 API

1
curl -X POST -H 'Content-type: application/json' -d '{"set-property":{"updateHandler.autoCommit.maxTime":15000}}' http://localhost:8983/solr/techproducts/config

V2 API

1
curl -X POST -H 'Content-type: application/json' -d '{"set-property":{"updateHandler.autoCommit.maxTime":15000}}' http://localhost:8983/api/collections/techproducts/config

您可以使用config/overlay端点验证属性已添加到configoverlay.json

V1 API

1
curl http://localhost:8983/solr/techproducts/config/overlay?omitHeader=true

V2 API

1
curl http://localhost:8983/api/collections/techproducts/config/overlay?omitHeader=true

输出:

1
2
3
4
5
6
7
8
{
"overlay": {
"znodeVersion": 1,
"props": {
"updateHandler": {
"autoCommit": {"maxTime": 15000}
}
}}}

要取消设置属性:

V1 API

1
curl -X POST -H 'Content-type: application/json' -d '{"unset-property": "updateHandler.autoCommit.maxTime"}' http://localhost:8983/solr/techproducts/config

V2 API

1
curl -X POST -H 'Content-type: application/json' -d '{"unset-property": "updateHandler.autoCommit.maxTime"}' http://localhost:8983/api/collections/techproducts/config

处理器和组件命令

请求处理器、搜索组件和其他类型的本地化Solr组件(如查询解析器、更新处理器等)可以使用特定于正在修改的组件类型的特定命令进行添加、更新和删除。

语法在每种情况下都类似:add-<component-name>update-<component-name>delete-<component-name>。命令名称不区分大小写,因此Add-RequestHandlerADD-REQUESTHANDLERadd-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命令:

V1 API

1
2
3
4
5
6
7
8
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"
}
}' http://localhost:8983/solr/techproducts/config

V2 API

1
2
3
4
5
6
7
8
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"
}
}' http://localhost:8983/api/collections/techproducts/config

调用新请求处理器以检查是否已注册:

1
$ curl http://localhost:8983/solr/techproducts/mypath?omitHeader=true

您应该看到以下输出:

1
2
3
4
5
6
7
8
9
10
{
"params":{
"indent": "true",
"a": "b",
"x": "y",
"rows": "10"},
"context":{
"webapp": "/solr",
"path": "/mypath",
"httpMethod": "GET"}}

要更新请求处理器,应使用update-requesthandler命令:

V1 API

1
2
3
4
5
6
7
8
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"
}
}' http://localhost:8983/solr/techproducts/config

V2 API

1
2
3
4
5
6
7
8
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"
}
}' http://localhost:8983/api/collections/techproducts/config

最后,通过delete-requesthandler命令删除请求处理器:

V1 API

1
2
3
curl -X POST -H 'Content-type:application/json' -d '{
"delete-requesthandler": "/mypath"
}' http://localhost:8983/solr/techproducts/config

V2 API

1
2
3
curl -X POST -H 'Content-type:application/json' -d '{
"delete-requesthandler": "/mypath"
}' http://localhost: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

1
curl -X POST -H 'Content-type:application/json' -d '{"set-user-property": {"variable_name": "some_value"}}' http://localhost:8983/solr/techproducts/config

V2 API

1
curl -X POST -H 'Content-type:application/json' -d '{"set-user-property": {"variable_name": "some_value"}}' http://localhost:8983/api/collections/techproducts/config

同样,我们可以使用/config/overlay端点验证更改已经完成:

V1 API

1
curl http://localhost:8983/solr/techproducts/config/overlay?omitHeader=true

V2 API

1
curl http://localhost:8983/api/collections/techproducts/config/overlay?omitHeader=true

我们期望看到如下输出:

1
2
3
4
5
{"overlay":{
"znodeVersion":5,
"userProps":{
"variable_name": "some_value"}}
}

要取消设置变量,发出如下命令:

V1 API

1
curl -X POST -H 'Content-type:application/json' -d '{"unset-user-property": "variable_name"}' http://localhost:8983/solr/techproducts/config

V2 API

1
curl -X POST -H 'Content-type:application/json' -d '{"unset-user-property": "variable_name"}' http://localhost:8983/api/collections/techproducts/config

如何将solrconfig.xml属性映射到JSON

通过使用此API,您将生成solrconfig.xml中定义的属性的JSON表示。要了解如何使用API表示属性,让我们看几个示例。

以下是请求处理器在solrconfig.xml中的样子:

1
2
3
4
5
6
<requestHandler name="/query" class="solr.SearchHandler">
<lst name="defaults">
<str name="echoParams">explicit</str>
<int name="rows">10</str>
</lst>
</requestHandler>

使用Config API定义的相同请求处理器如下所示:

1
2
3
4
5
6
7
8
9
10
{
"add-requesthandler":{
"name": "/query",
"class": "solr.SearchHandler",
"defaults":{
"echoParams": "explicit",
"rows": 10
}
}
}

Config API的工作原理

当使用SolrCloud时,每个核心都会监视用于该核心的配置集的ZooKeeper目录。如果同一节点中有多个核心使用相同的配置集,则只使用一个ZooKeeper监视。

提示: 在用户管理集群或单节点安装中,没有监视(因为ZooKeeper未运行)。

例如,如果配置集’myconf’被核心使用,节点将监视/configs/myconf。通过API执行的每个写操作都会’触碰’目录,所有监视器都会收到通知。每个核心都会通过比较znode版本检查模式文件、solrconfig.xmlconfigoverlay.json是否已被修改。如果有任何被修改,核心会重新加载。

如果修改了params.json,参数对象只是更新而不进行核心重新加载(有关params.json的更多信息,请参见请求参数API)。

空命令

如果向/config端点发送空命令,监视将在使用此配置集的所有核心上触发。例如:

V1 API

1
curl -X POST -H 'Content-type:application/json' -d '{}' http://localhost:8983/solr/techproducts/config

V2 API

1
curl -X POST -H 'Content-type:application/json' -d '{}' http://localhost:8983/api/collections/techproducts/config

直接编辑任何文件而不’触碰’目录不会使其对所有节点可见。

组件可以通过使用SolrCore#registerConfListener()注册监听器来监视配置集’触碰’事件。

监听配置更改

任何组件都可以使用以下方式注册监听器:

SolrCore#addConfListener(Runnable listener)

以获取配置更改的通知。如果修改的文件导致核心重新加载(即configoverlay.xml或模式),这不是很有用。组件可以使用此功能重新加载它们感兴趣的文件。

总结

Config API为Solr配置管理提供了强大而灵活的工具。通过REST风格的接口,管理员可以动态修改常用配置属性、管理请求处理器和组件,以及设置用户定义属性。配置覆盖机制确保原始配置文件的完整性,同时提供运行时配置修改的能力。合理使用Config API可以显著简化Solr集群的配置管理和维护工作。

© 2025 Solr Community of China All Rights Reserved. 本站访客数人次 本站总访问量
Theme by hiero