教程

搜索

post工具

# 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

schema api


# 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

solr脚本

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/

solrconfig.xml

<!-- mutable 用于控制是否可以对模式数据进行修改。为了允许使用模式 API 进行编辑,此参数必须设置为 true。-->
<!-- managedSchemaResourceName 是一个可选的参数,默认值为“managed-schema”,它定义了一个新的模式文件名称,该名称可以是除“schema.xml”之外的任何名称。-->
<schemaFactory class="ManagedIndexSchemaFactory">
  <bool name="mutable">true</bool>
  <str name="managedSchemaResourceName">managed-schema</str>
</schemaFactory>
<autoSoftCommit>
  <maxTime>60000</maxTime>
</autoSoftCommit>
# 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
# 获取配置信息,这个响应结果将是合并 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

备份和恢复

<!-- 主节点配置 -->
<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"]
}'

文档更新

原子更新修饰符
# 现有文档
{"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"]}
}
💡tip

由于确保能够就地执行更新可能会比较困难,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'