Solr5 Schema API

1. Schema API

Schema API提供了对每个collection的schema的读写访问。
对所有schema元素的读访问都支持。
字段(Fields), dynamic fields, field types 和 copyField 可以被添加、删除或替代。未来Solr可能支持对更多schema元素的写操作。

注意:一旦schema被修改,重新索引所有数据。

要通过API修改schema, schema需要是managed且mutable, 参见Managed Schema配置。
API运行两种输出模式:JSON或XML.
当使用API修改schema时,core reload会自动发生以使之生效。

API的基本地址是 http://<host>:<port>/solr/<collection_name>, 如 http://localhost:8983/solr/test

1.1 API入口点(Entry Points)

/schema:  获取schema, 或修改schema用以添加、删除或替换字段、动态字段、拷贝字段, 或字段类型。
/schema/fields:  获取所有fields或指定field信息。
/schema/dynamicfields:  获取所有dynamic fields或指定field规则信息。
/schema/copyfields:  获取copy fields信息。
/schema/fieldtypes:  获取所有field types或指定field type信息。
/schema/name: 获取schema名称
/schema/version:
/schema/uniquekey:
/schema/similarity:
/schema/solrqueryparser/defaultoperator:

例:
curl http://localhost:8983/solr/test/schema  #GET (-O)

1.2 修改Schema

POST请求到/collection/schema,并提交一系列命令。

* add-field: 使用你提供的参数添加一个新的字段。
* delete-field: 删除一个字段
* replace-field: 使用不同配置替换已有字段

* add-dynamic-field: 使用你提供的参数添加一个新的dynamic字段。
* delete-dynamic-field: 删除一个dynamic字段
* replace-dynamic-field: 使用不同配置替换已有dynamic字段

* add-field-type: 使用你提供的参数添加一个新的字段类型。
* delete-field-type: 删除一个字段类型
* replace-field-type: 使用不同配置替换已有字段类型

* add-copy-field: 添加一个新的copy字段。
* delete-copy-field: 删除一个copy字段

这些命令可以在单独的post, 也可以在同一个post中,将按指定的顺序执行。

- Add a New Field

curl -X POST -H 'Content-type:application/json' --data-binary '{
  "add-field":{ 
    "name":"sell-by",
    "type":"tdate",
    "stored":true }
}' http://localhost:8983/solr/test/schema

- Delete a Field
curl -X POST -H 'Content-type:application/json' --data-binary '{
  "delete-field" : { "name":"sell-by" }
}' http://localhost:8983/solr/test/schema


- Replace a Field

curl -X POST -H 'Content-type:application/json' --data-binary '{
"replace-field":{ 
"name":"sell-by",
"type":"date",
"stored":false }
}' http://localhost:8983/solr/test/schema


- Add a Dynamic Field Rule

curl -X POST -H 'Content-type:application/json' --data-binary '{
"add-dynamic-field":{ 
"name":"*_s",
"type":"string",
"stored":true }
}' http://localhost:8983/solr/test/schema


- Delete a Dynamic Field Rule

curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete-dynamic-field":{ "name":"*_s" }
}' http://localhost:8983/solr/test/schema


- Replace a Dynamic Field Rule

curl -X POST -H 'Content-type:application/json' --data-binary '{
"replace-dynamic-field":{ 
"name":"*_s",
"type":"text_general",
"stored":false }
}' http://localhost:8983/solr/test/schema


- Add a New Field Type

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/test/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":"/" }},
"queryAnalyzer":{
  "tokenizer":{ 
  "class":"solr.KeywordTokenizerFactory" }}}
}' http://localhost:8983/solr/test/schema


- Delete a Field Type

curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete-field-type":{ "name":"myNewTxtField" }
}' http://localhost:8983/solr/test/schema


- Replace a Field Type

curl -X POST -H 'Content-type:application/json' --data-binary '{
"replace-field-type":{
  "name":"myNewTxtField",
  "class":"solr.TextField",
  "positionIncrementGap":"100",
  "analyzer":{
    "tokenizer":{ 
    "class":"solr.StandardTokenizerFactory" }}}
}' http://localhost:8983/solr/test/schema


- Add a New Copy Field Rule

curl -X POST -H 'Content-type:application/json' --data-binary '{
  "add-copy-field":{
  "source":"shelf",
  "dest":[ "location", "catchall" ]}
}' http://localhost:8983/solr/test/schema


- Delete a Copy Field Rule

curl -X POST -H 'Content-type:application/json' --data-binary '{
"delete-copy-field":{ "source":"shelf", "dest":"location" }
}' http://localhost:8983/solr/test/schema


- 多个命令在一个POST中

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/test/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/test/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/test/schema


- 在复制集之间修改Schema
当以SolrCloud模式运行时,一个节点的修改会传递到所有集合。你可以传递一个updateTimeoutSecs参数,
来指定等待所有复制节点确认应用了此修改所等待的时间(秒数)。


1.3 获取Schema信息


- Retrieve the Entire Schema

路径参数:
/collection/

请求参数:
wt

例:
curl http://localhost:8983/solr/test/schema
curl http://localhost:8983/solr/test/schema?wt=json
curl http://localhost:8983/solr/test/schema?wt=xml
curl http://localhost:8983/solr/test/schema?wt=schema.xml


- List Fields

路径参数:
/collection/fieldname

请求参数:
wt=json/xml
fl=
includeDynamic=false/true
showDefaults=false/true

例:
curl http://localhost:8983/solr/test/schema/fields?wt=json

- List Dynamic Fields

路径参数:
/collection/fieldname/

请求参数:
wt=json/xml
showDefaults=false/true

例:
curl http://localhost:8983/solr/test/schema/dynamicfields?wt=json

- List Field Types

路径参数:
/collection/fieldname/

请求参数:
wt=json/xml
showDefaults=false/true

例:
curl http://localhost:8983/solr/test/schema/fieldtypes?wt=json

- List Copy Fields

路径参数:
/collection/

请求参数:
wt=json/xml
source.fl=
dest.fl=

例:
curl http://localhost:8983/solr/test/schema/copyfields?wt=json

- Show Schema Name

curl http://localhost:8983/solr/test/schema/name?wt=json

- Show the Schema Version

curl http://localhost:8983/solr/test/schema/version?wt=json

- List UniqueKey

curl http://localhost:8983/solr/test/schema/uniquekey?wt=json

- Show Global Similarity

curl http://localhost:8983/solr/test/schema/similarity?wt=json

- Get the Default Query Operator

curl http://localhost:8983/solr/test/schema/solrqueryparser/defaultoperator?wt=json

1.4 Manage Resource Data

Manage Resource REST API 提供了一个机制,任何Solr插件可暴露支持CRUD操作的资源。






























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值