# linux
bin/post -c techproducts example/exampledocs/*
#windows
java -jar -Dc=techproducts -Dauto example\exampledocs\post.jar example\exampledocs\*
# 导入json数据
bin/post -c films example/films/films.json
java -jar -Dc=films -Dauto example\exampledocs\post.jar example\films\*.json
# xml数据
bin/post -c films example/films/films.xml
java -jar -Dc=films -Dauto example\exampledocs\post.jar example\films\*.xml
# csv
bin/post -c films example/films/films.csv -params "f.genre.split=true&f.directed_by.split=true&f.genre.separator=|&f.directed_by.separator=|"
java -jar -Dc=films -Dparams=f.genre.split=true&f.directed_by.split=true&f.genre.separator=|&f.directed_by.separator=| -Dauto example\exampledocs\post.jar example\films\*.csv
/api/<collections|cores>/<name>/schema/ 端点发送一个 POST 请求。在请求体中以 JSON 格式包含一系列命令,即可执行所需的操作。以下是支持的命令列表:这些命令可以单独作为 POST 请求发送,也可以合并在同一 POST 请求中发送。命令将按照指定的顺序执行。
# add field
curl -X POST -H 'Content-type:application/json' --data-binary '{"add-field": {"name":"name", "type":"text_general", "multiValued":false, "stored":true}}' http://localhost:8983/solr/films/schema
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field":{
"name":"sell_by",
"type":"pdate",
"stored":true }
}' http://localhost:8983/solr/gettingstarted/schema
# v2 api
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field":{
"name":"sell_by",
"type":"pdate",
"stored":true }
}' http://localhost:8983/api/collections/gettingstarted/schema
# 删除字段,如果该字段在模式中不存在,或者该字段是复制字段规则的来源或目标,则会引发错误
curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete-field" : { "name":"sell_by" }
}' http://localhost:8983/solr/gettingstarted/schema
# 替换字段
curl -X POST -H 'Content-type:application/json' --data-binary '{
"replace-field":{
"name":"sell_by",
"type":"date",
"stored":false }
}' http://localhost:8983/solr/gettingstarted/schema
# copy field
curl -X POST -H 'Content-type:application/json' --data-binary '{"add-copy-field" : {"source":"*","dest":"_text_"}}' http://localhost:8983/solr/films/schema
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-copy-field":{
"source":"shelf",
"dest":[ "location", "catchall" ]}
}' http://localhost:8983/solr/gettingstarted/schema
curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete-copy-field":{ "source":"shelf", "dest":"location" }
}' http://localhost:8983/solr/gettingstarted/schema
# 添加字段类型
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field-type" : {
"name":"myNewTxtField",
"class":"solr.TextField",
"positionIncrementGap":"100",
"analyzer" : {
"charFilters":[{
"class":"solr.PatternReplaceCharFilterFactory",
"replacement":"$1$1",
"pattern":"([a-zA-Z])\\\\1+" }],
"tokenizer":{
"class":"solr.WhitespaceTokenizerFactory" },
"filters":[{
"class":"solr.WordDelimiterFilterFactory",
"preserveOriginal":"0" }]}}
}' http://localhost:8983/solr/gettingstarted/schema
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field-type":{
"name":"myNewTextField",
"class":"solr.TextField",
"indexAnalyzer":{
"tokenizer":{
"class":"solr.PathHierarchyTokenizerFactory",
"delimiter":"/"
},
"filters":[{
"class":"solr.LowerCaseFilterFactory"
}]
},
"queryAnalyzer":{
"tokenizer":{ "class":"solr.KeywordTokenizerFactory" },
"filters":[{ "class":"solr.LowerCaseFilterFactory"}]
}
}
}' http://localhost:8983/solr/gettingstarted/schema
# 删除字段类型
curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete-field-type":{ "name":"myNewTxtField" }
}' http://localhost:8983/solr/gettingstarted/schema
# 可以在一个命令中执行一个或多个添加请求。该 API 具有事务性特性,因此,在同一调用中所有的命令要么全部成功,要么全部失败。
# 依次执行不同命令
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field-type":{
"name":"myNewTxtField",
"class":"solr.TextField",
"positionIncrementGap":"100",
"analyzer":{
"charFilters":[{
"class":"solr.PatternReplaceCharFilterFactory",
"replacement":"$1$1",
"pattern":"([a-zA-Z])\\\\1+" }],
"tokenizer":{
"class":"solr.WhitespaceTokenizerFactory" },
"filters":[{
"class":"solr.WordDelimiterFilterFactory",
"preserveOriginal":"0" }]}},
"add-field" : {
"name":"sell_by",
"type":"myNewTxtField",
"stored":true }
}' http://localhost:8983/solr/gettingstarted/schema
# 执行相同命令
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field":{
"name":"shelf",
"type":"myNewTxtField",
"stored":true },
"add-field":{
"name":"location",
"type":"myNewTxtField",
"stored":true },
"add-copy-field":{
"source":"shelf",
"dest":[ "location", "catchall" ]}
}' http://localhost:8983/solr/gettingstarted/schema
# 可以將重复的命令作为数组发送:
curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-field":[
{ "name":"shelf",
"type":"myNewTxtField",
"stored":true },
{ "name":"location",
"type":"myNewTxtField",
"stored":true }]
}' http://localhost:8983/solr/gettingstarted/schema
# 检索整个模式,返回xml格式,默认json,还有schema.xml
curl http://localhost:8983/solr/gettingstarted/schema?wt=xml
# 获取所有字段的列表。
GET /<collection>/schema/fields
GET /<collection>/schema/fields/<fieldname>
curl http://localhost:8983/solr/gettingstarted/schema/fields
# 查询参数
- wt 定义了响应的格式。可选的格式为 json 或 xml 。如果未指定,则默认返回 JSON 格式的数据。
- fl 包含一个或多个字段的列表,这些字段通过逗号或空格进行分隔。如果未指定具体字段,则默认会返回所有字段。
- includeDynamic 如果指定了 true 参数,并且使用了 fl 查询参数或 fieldname 路径参数,那么匹配到的动态字段会被包含在响应中,并且会通过 dynamicBase 属性进行标识。
- showDefaults 如果为 true ,那么每个字段类型下的所有默认字段属性都会包含在响应中(例如, tokenized 对应 solr.TextField )。如果设置为 false ,那么只有明确指定的字段属性才会被包含在内。
# 列出动态字段 查询参数 wt showDefaults
GET /<collection>/schema/dynamicfields
GET /<collection>/schema/dynamicfields/<name>
#列出字段类型 查询参数 wt showDefaults
GET /<collection>/schema/fieldtypes
GET /<collection>/schema/fieldtypes/<name>
# 列出复制的字段
GET /<collection>/schema/copyfields
# 查询参数
- source.fl 一个由逗号或空格分隔的列表,多个copyField源字段的列表,这些字段将被包含在响应中。与copyField指令相关的所有其他源字段将不会被包含在响应中。如果未指定,那么所有的copyField字段都将包含在响应中。
- dest.fl 一个由逗号或空格分隔的列表,列出了在响应中需要包含的一个或多个`copyField`目标字段。所有带有`copyField`指令的字段都会被排除在外。如果未指定具体字段,那么所有的`copyField`字段都会被包含在响应中。
# 显示模式名称
GET /<collection>/schema/name
# 显示模式版本
GET /<collection>/schema/version
# 列出唯一键
GET /<collection>/schema/uniquekey
# 显示全局相似度
GET /<collection>/schema/similarity
bin/solr status
bin/solr create -c <name>
bin/solr -e techproducts
bin/solr start [options]
bin/solr start -help
bin/solr restart [options]
bin/solr restart -help
#加jvm参数
bin/solr start -a "-Xdebug -Xrunjdwp:transport=dt_socket, server=y,suspend=n,address=1044"
# 系统参数
bin/solr start -Dsolr.autoSoftCommit.maxTime=3000
-f 在后台启动 Solr
-h 指定host
-m 设置jvm内存
-d 指定安装目录下的/server(Jetty服务目录)不推荐修改
-s 指定数据目录(下面是solr.xml和各个核心目录)
# 关闭指定solr
bin/stop -p 8983
# 关闭所有solr
bin/solr -all
#创建核心
bin/solr create -help
-c 核心目录
-d 配置目录,默认_default
#删除核心
bin/solr delete -help
-c 核心目录
-deleteConfig 删除配置
#修改通用属性
bin/solr config -help
# 默认-action是set-property
bin/solr config -c mycollection -p 8983 -action set-property -property updateHandler.autoCommit.maxDocs -value 100
#若要取消设定某个已设置的公共属性,只需使用 -action unset-property 来指定,而无需提供 -value 参数:
bin/solr config -c mycollection -p 8983 -action unset-property -property updateHandler.autoCommit.maxDocs
# 设置用户属性
bin/solr config -c mycollection -p 8983 -action set-user-property -property update.autoCreateFields -value false
# 取消用户属性
bin/solr config -c mycollection -p 8983 -action unset-user-property -property update.autoCreateFields
# 禁用schemaless模式
bin/solr config -c mycollection -p 8983 -action set-user-property -property update.autoCreateFields -value false
<solr-home-directory>/
solr.xml
core_name1/
core.properties
conf/
solrconfig.xml
managed-schema
data/
core_name2/
core.properties
conf/
solrconfig.xml
managed-schema
data/
<dataDir> 参数。你可以使用绝对路径或相对于 SolrCore 实例目录的路径来指定另一个目录。<dataDir>/solr/data/${solr.core.name}</dataDir><schemaFactory/> ,Solr 会默认使用 ManagedIndexSchemaFactory 。 ManagedIndexSchemaFactory 的默认值为 "mutable" ,而模式信息则保存在 managed-schema 文件中。<!-- mutable 用于控制是否可以对模式数据进行修改。为了允许使用模式 API 进行编辑,此参数必须设置为 true。-->
<!-- managedSchemaResourceName 是一个可选的参数,默认值为“managed-schema”,它定义了一个新的模式文件名称,该名称可以是除“schema.xml”之外的任何名称。-->
<schemaFactory class="ManagedIndexSchemaFactory">
<bool name="mutable">true</bool>
<str name="managedSchemaResourceName">managed-schema</str>
</schemaFactory>
请求处理程序负责处理传送到 Solr 的各类请求。这些请求可能是查询请求,也可能是索引更新请求。根据您对 Solr 处理各种请求的需求,可能需要定义多个这样的处理程序。
搜索组件是搜索功能的一部分,比如高亮显示或分屏显示等功能。搜索组件在 solrconfig.xml 中独立于请求处理程序进行定义,然后根据需要将其注册到相应的请求处理程序中。
每个请求处理器的定义都包括一个名称和一个类。请求处理器的名称是通过路径来引用的,通常格式为“/处理器名称” http://localhost:8983/solr/gettingstarted/select?q=solr 这个查询将由名为 /select 的请求处理程序来处理。
还可以通过一个名为 initParams 的部分来配置请求处理器的默认设置。这些默认设置可以在需要为各个处理器配置通用属性时使用。
使用 Solr 定义的主要请求处理程序是"SearchHandler",它负责处理搜索请求。该请求处理程序已经定义好了,接下来可以通过 defaults 列表来定义该处理程序的默认设置。
UpdateRequestHandlers 是负责处理索引更新处理的请求处理程序。
搜索组件定义了 SearchHandler 用于为用户执行查询的逻辑。
<autoSoftCommit>
<maxTime>60000</maxTime>
</autoSoftCommit>
在 Solr 中,“core”一词指的是一个索引单元,以及相关的事务日志和配置文件(包括 solrconfig.xml 文件、Schema 文件等)。如果需要,您的 Solr 安装可以包含多个核心,这样就能在同一台服务器上对结构不同的数据进行分析,并更好地控制数据如何呈现给不同的用户。在 SolrCloud 模式下,您会更熟悉“collection”这个概念。实际上,一个 collection 由一个或多个 core 组成。
solr.xml 文件定义了一些适用于所有或多数核心的全局配置选项。
Solr 核心组件是通过将名为 core.properties 的文件放置到 solr.home 的子目录中来配置的。树的深度没有限制,也可以定义多个核心组件。核心组件可以位于树的任何位置,不过不能将核心组件定义在已有核心组件之下。
core.properties属性
CoreAdmin API 是通过 CoreAdminHandler 来实现的。CoreAdminHandler 是一种专门用于管理 Solr 核心的请求处理程序。与其他请求处理程序不同,CoreAdminHandler 并不依附于某个特定核心,而是在每个 Solr 节点中都有一个独立的 CoreAdminHandler 实例,该实例负责管理该节点中所有核心的运行情况,并且可以通过 /solr/admin/cores 路径访问该实例。
# STATUS 状态
curl http://localhost:8983/solr/admin/cores?action=STATUS&core=core-name
# 创建
admin/cores?action=CREATE&name=core-name&instanceDir=path/to/dir&config=solrconfig.xml&dataDir=data&configSet=configset2
curl http://localhost:8983/admin/cores?action=CREATE&name=mycore&instanceDir=path/to/instance&configSet=configset2
# 重载
admin/cores?action=RELOAD&core=core-name
# 重命名
admin/cores?action=RENAME&core=core-name&other=other-core-name
# 交互核心名称
admin/cores?action=SWAP&core=core-name&other=other-core-name
# 卸载
admin/cores?action=UNLOAD&core=core-name&deleteIndex=false&deleteDataDir=false&deleteInstanceDir=false
# 合并索引
admin/cores?action=MERGEINDEXES&core=new-core-name&indexDir=path/to/core1/data/index&indexDir=path/to/core2/data/index
admin/cores?action=mergeindexes&core=new-core-name&srcCore=core1-name&srcCore=core2-name
# 分割索引
http://localhost:8983/solr/admin/cores?action=SPLIT&core=core0&targetCore=core1&targetCore=core2
http://localhost:8983/solr/admin/cores?action=SPLIT&core=core0&path=/path/to/index/1&path=/path/to/index/2
#请求已经提交的异步 CoreAdmin API 调用的状态。requestid:该异步请求使用了用户自定义的请求 ID。此参数必需的。
admin/cores?action=REQUESTSTATUS&requestid=id
/<configSetBaseDir>
/configset1
/conf
/managed-schema
/solrconfig.xml
/configset2
/conf
/managed-schema
/solrconfig.xml
默认的基础目录是 $SOLR_HOME/configsets 。这个路径可以在 solr.xml 中进行配置 configSetBaseDir
配置api.Config API 允许通过类似 REST 协议的 API 调用来操控 solrconfig.xml 的各种属性。具体来说就是用户自定义的属性。通过此 API 进行的更改会被存储在一个名为 configoverlay.json 的文件中。这个文件应该只能通过 API 来编辑
所有 Config API 接口都针对特定集合进行设计,这意味着该接口可以一次只检查或修改某个集合的配置。
<collection>/config :可以获取完整的有效配置信息,或者修改配置。使用 GET 请求来获取配置数据,而 POST 请求则用于执行各种命令。
<collection>/config/overlay :仅检索 configoverlay.json 中的详细信息,同时移除所有在 solrconfig.xml 中直接或通过默认设置间接定义的选项。
<collection>/config/params :可以创建一些参数集,这些参数集可以覆盖或替代 solrconfig.xml 中定义的参数。有关此端点的更多信息,请参考“请求参数”API。
# 获取配置信息,这个响应结果将是合并 configoverlay.json 中的设置与 solrconfig.xml 中的设置后得到的 Solr 配置。
http://localhost:8983/solr/techproducts/config
http://localhost:8983/api/collections/techproducts/config
# 查询requestHandler配置,类似的有 query 、 requestHandler 、 searchComponent 、 updateHandler 、 queryResponseWriter 、 initParams 、 znodeVersion 、 listener 、 directoryFactory 、 indexConfig 以及 codecFactory 。
http://localhost:8983/solr/techproducts/config/requestHandler
# 若想将请求限制在某个顶层板块内的单个组件上,可以使用 componentName 请求参数。仅限 requestHandler searchComponent queryResponseWriter
http://localhost:8983/solr/techproducts/config/requestHandler?componentName=/select
# 针对属性的修改
# 请求增加 updateHandler.autoCommit.maxTime 的值看起来会是这样的:
curl -X POST -H 'Content-type: application/json' -d '{"set-property":{"updateHandler.autoCommit.maxTime":15000}}' http://localhost:8983/solr/techproducts/config
# v2 api
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 中:
curl http://localhost:8983/solr/techproducts/config/overlay?omitHeader=true
# 取消属性
curl -X POST -H 'Content-type: application/json' -d '{"unset-property": "updateHandler.autoCommit.maxTime"}' http://localhost:8983/solr/techproducts/config
# 修改处理器和组件
# 创建一个请求处理程序,我们可以使用 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"
}
}' http://localhost:8983/solr/techproducts/config
# 调用该处理器,检查是否注册
curl http://localhost:8983/solr/techproducts/mypath?omitHeader=true
# 要更新请求处理器,应该使用 update-requesthandler 命令:
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
# 删除处理器
curl -X POST -H 'Content-type:application/json' -d '{
"delete-requesthandler": "/myterms"
}' http://localhost:8983/solr/techproducts/config
# 修改用户自定义属性的命令
# 设置属性
curl -X POST -H 'Content-type:application/json' -d '{"set-user-property": {"variable_name": "some_value"}}' http://localhost:8983/solr/techproducts/config
# 检查
curl http://localhost:8983/solr/techproducts/config/overlay?omitHeader=true
# 取消
curl -X POST -H 'Content-type:application/json' -d '{"unset-user-property": "variable_name"}' http://localhost:8983/solr/techproducts/config
curl http://localhost:8983/solr/techproducts/config/params -H 'Content-type:application/json' -d '{
"set":{
"myFacets":{
"facet":"true",
"facet.limit":5}},
"set":{
"myQueries":{
"defType":"edismax",
"rows":"5",
"df":"text_all"}}
}'
curl http://localhost:8983/solr/techproducts/config/params -H 'Content-type:application/json' -d '{
"set":{
"my_handler_params":{
"facet.limit":5,
"_invariants_": {
"facet":true,
},
"_appends_":{"facet.field":["field1","field2"]
}
}}
}'
curl http://localhost:8983/solr/techproducts/config/params -H 'Content-type:application/json' -d '{
"delete":[
"myFacets",
"myQueries"
]
}'
curl http://localhost:8983/solr/techproducts/config/params -H 'Content-type:application/json' -d '{
"delete":"myFacets"
}'
# 使用 RequestHandlers 查看扩展的参数集和有效参数
curl http://localhost:8983/solr/techproducts/config/requestHandler?componentName=/export&expandParams=true
# 查看请求参数
curl http://localhost:8983/solr/techproducts/config/params
curl http://localhost:8983/solr/techproducts/config/params/myQueries
# 使用请求参数
http://localhost/solr/techproducts/select?useParams=myQueries
#使用多个,参数集“myQueries”是优先于“myFacets”来应用的
http://localhost/solr/techproducts/select?useParams=myFacets,myQueries
http://localhost:8983/solr/gettingstarted/replication?command=backup backup 命令是一个异步调用,它只会返回最新索引提交点时的数据。所有的索引管理和搜索操作仍将像往常一样在索引上进行执行
location 备份文件将被创建的路径。如果路径不是绝对的,那么备份路径将会相对于 Solr 的实例目录而言。
name 这个快照文件将被创建在名为 snapshot.<name> 的目录下。如果未指定目录名称,那么目录名称将采用如下格式: snapshot.<yyyyMMddHHmmssSSS>。
numberToKeep 需要保留的备份数量。如果已在 solrconfig.xml 中的复制处理程序中指定了 maxNumberOfBackups ,那么 maxNumberOfBackups 总是会被使用;而尝试使用 numberToKeep 则会引发错误。
commitName 在使用 CREATESNAPSHOT 命令创建快照时所使用的提交名称。
http://localhost:8983/solr/gettingstarted/replication?command=details&wt=xml 检查备份状态
恢复备份 http://localhost:8983/solr/gettingstarted/replication?command=restore&name=backup_name
location 备份快照文件的位置。如果未指定位置,则会在 Solr 的数据目录中查找备份文件。
name 需要恢复的备份索引快照的名称。如果未提供名称,则会在指定的位置目录中查找符合 snapshot.<timestamp> 格式备份文件。在这种情况下,会选择时间戳最晚的备份文件进行恢复。
http://localhost:8983/solr/gettingstarted/replication?command=restorestatus&wt=xml 检查恢复状态
创建快照 http://localhost:8983/solr/admin/cores?action=CREATESNAPSHOT&core=techproducts&commitName=commit1
commitName 存储快照的名称。
core 用于执行快照的核心名称。
async 请求 ID 用于跟踪此操作的状态,该操作将以异步方式处理。
http://localhost:8983/solr/admin/cores?action=LISTSNAPSHOTS&core=techproducts&commitName=commit1 LISTSNAPSHOTS 命令列出了特定核心被占用的所有快照列表。
core 用于执行快照的核心名称。
async 请求 ID 用于跟踪此操作的状态,该操作将以异步方式处理。
http://localhost:8983/solr/admin/cores?action=DELETESNAPSHOT&core=techproducts&commitName=commit1 删除快照
commitName 指定要删除的提交名称。
core 用于执行快照的核心名称。
async 请求 ID 用于跟踪此操作的状态,该操作将以异步方式处理。
备份设置
<!-- 主节点配置 -->
<requestHandler name="/replication" class="solr.ReplicationHandler">
<lst name="leader">
<str name="replicateAfter">optimize</str>
<str name="backupAfter">optimize</str>
<str name="confFiles">schema.xml,stopwords.txt,elevate.xml</str>
</lst>
<int name="maxNumberOfBackups">2</int>
<str name="commitReserveDuration">00:00:10</str>
<lst name="invariants">
<str name="maxWriteMBPerSec">16</str>
</lst>
</requestHandler>
# 从节点配置
<requestHandler name="/replication" class="solr.ReplicationHandler">
<lst name="follower">
<!-- fully qualified url for the replication handler of leader. It is
possible to pass on this as a request param for the fetchindex command -->
<str name="leaderUrl">http://remote_host:port/solr/core_name/replication</str>
<!-- Interval in which the follower should poll leader. Format is HH:mm:ss .
If this is absent follower does not poll automatically.
But a fetchindex can be triggered from the admin or the http API -->
<str name="pollInterval">00:00:20</str>
<!-- THE FOLLOWING PARAMETERS ARE USUALLY NOT REQUIRED-->
<!-- To use compression while transferring the index files. The possible
values are internal|external. If the value is 'external' make sure
that your leader Solr has the settings to honor the accept-encoding header.
If it is 'internal' everything will be taken care of automatically.
USE THIS ONLY IF YOUR BANDWIDTH IS LOW.
THIS CAN ACTUALLY SLOW DOWN REPLICATION IN A LAN -->
<str name="compression">internal</str>
<!-- The following values are used when the follower connects to the leader to
download the index files. Default values implicitly set as 5000ms and
10000ms respectively. The user DOES NOT need to specify these unless the
bandwidth is extremely low or if there is an extremely high latency -->
<str name="httpConnTimeout">5000</str>
<str name="httpReadTimeout">10000</str>
<!-- If HTTP Basic authentication is enabled on the leader, then the follower
can be configured with the following -->
<str name="httpBasicAuthUser">username</str>
<str name="httpBasicAuthPassword">password</str>
</lst>
</requestHandler>
# 使用xml格式
curl http://localhost:8983/solr/my_collection/update -H "Content-Type: text/xml" --data-binary '
<add>
<doc>
<field name="authors">Patrick Eagar</field>
<field name="subject">Sports</field>
<field name="dd">796.35</field>
<field name="isbn">0002166313</field>
<field name="yearpub">1982</field>
<field name="publisher">Collins</field>
</doc>
</add>'
# json 单个文档
curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/my_collection/update/json/docs' --data-binary '
{
"id": "1",
"title": "Doc 1"
}'
# 多个文档
curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/my_collection/update' --data-binary '
[
{
"id": "1",
"title": "Doc 1"
},
{
"id": "2",
"title": "Doc 2"
}
]'
# 多个命令,包括添加和删除文档的操作,都可以包含在一个消息中。
curl -X POST -H 'Content-Type: application/json' 'http://localhost:8983/solr/my_collection/update' --data-binary '
{
"add": {
"doc": {
"id": "DOC1",
"my_field": 2.3,
"my_multivalued_field": [ "aaa", "bbb" ]
}
},
"add": {
"commitWithin": 5000,
"overwrite": false,
"doc": {
"f1": "v1",
"f1": "v2"
}
},
"commit": {},
"optimize": { "waitSearcher":false },
"delete": { "id":"ID" },
"delete": { "query":"QUERY" },
"delete": "myid",
"delete": ["id1","id2"]
}'
set 将字段值设置或替换为指定的值;如果新值指定为“null”或空列表,则删除这些字段值。可以作为一个单独的值来指定,也可以用于多值字段,以列表形式呈现。
add 将指定的值添加到多值字段中。可以单独提供一个值,也可以提供多个值,这些值可以是一个列表的形式。
add-distinct 如果指定的值尚未存在于某个字段中,则会将这些值添加到该字段中。可以单独指定一个值,也可以指定一个列表中的多个值。
remove 从多值字段中移除所有指定值的出现。可以单独指定一个值,也可以指定一个值列表。
removeregex 从多值字段中移除所有指定的正则表达式匹配项。可以单独指定一个值,也可以指定一个列表。
inc 将数值增加特定数量。该数值必须是一个具体的数字。
原子式更新文档的核心功能要求,模式中的所有字段必须被配置为存储字段( stored="true" ),或者配置为文档值字段( docValues="true" )。复制字段的目的字段必须配置非存储(stored="false"),所有位于复制字段目标字段中的数据,都必须来自唯一的复制字段源。
# 现有文档
{"id":"mydoc",
"price":10,
"popularity":42,
"categories":["kids"],
"sub_categories":["under_5","under_10"],
"promo_ids":["a123x"],
"tags":["free_to_try","buy_now","clearance","on_sale"]
}
#更新命令
{"id":"mydoc",
"price":{"set":99},
"popularity":{"inc":20},
"categories":{"add":["toys","games"]},
"sub_categories":{"add-distinct":"under_10"},
"promo_ids":{"remove":"a123x"},
"tags":{"remove":["free_to_try","on_sale"]}
}
_version_ 字段也是一个非索引的、不存储数据的单值 docValues 字段;由于确保能够就地执行更新可能会比较困难,Solr 提供了一个名为 update.partial.requireInPlace 的请求参数选项。当该参数设置为 true 时,那些无法就地执行的原子更新将会失败。如果用户希望在无法就地执行更新时让请求“快速失败”,就可以选择这个选项。
#下载:
cd /home/zmy/software
https://mirrors.tuna.tsinghua.edu.cn/apache/lucene/solr/8.11.4/solr-8.11.4.tgz
tar zxf solr-8.11.4.tgz
cd solr-8.11.4
# 配置服务
sudo sh -c 'cat > /etc/systemd/system/solr.service <<EOF
[Unit]
Description=Apache Solr Search Server
Documentation=https://solr.apache.org/
After=network.target syslog.target
[Service]
Environment="SOLR_INSTALL_DIR=/home/zmy/software/solr-8.11.4"
# solr 解压根目录
Environment="SOLR_HOME=/home/zmy/software/solr-8.11.4"
# 指定专属JDK,避免系统JDK冲突
Environment="SOLR_JAVA_HOME=/usr/local/jdk17"
# JVM内存参数按需调整
#Environment="SOLR_HEAP=512m"
Environment="SOLR_JAVA_MEM=-Xms512m -Xmx512m"
# 运行用户
User=zmy
Group=zmy
Type=simple
WorkingDirectory=/home/zmy/software/solr-8.11.4
# 启动命令,后台常驻
ExecStart=/home/zmy/software/solr-8.11.4/bin/solr start -f -p 8983 -s /home/zmy/software/solr-8.11.4/server/solr
# 停止命令
ExecStop=/home/zmy/software/solr-8.11.4/bin/solr stop -p 8983
#PIDFile=/home/zmy/software/solr-8.11.4/bin/solr-8983.pid
# 输出日志
StandardOutput=journal
StandardError=journal
# 进程监控
Restart=on-failure
RestartSec=5
# 最大文件句柄、进程数优化
LimitNOFILE=65535
LimitNPROC=65535
[Install]
# 开机自启级别
WantedBy=multi-user.target
EOF'