Solr:V2 API

2018-12-23 14:16 更新
v2 API是一個現(xiàn)代化的自我記錄API接口,覆蓋了大多數(shù)當(dāng)前的Solr API。預(yù)計一旦v2 API達到完全覆蓋范圍,Solr內(nèi)部的API用法(如SolrJ和Admin UI)已經(jīng)從舊的API轉(zhuǎn)換到v2 API,舊的API將最終被淘汰。

現(xiàn)在兩種API風(fēng)格將共存,所有舊的API將繼續(xù)工作,沒有任何改變。您可以通過此-Ddisable.v2.api=true系統(tǒng)屬性啟動您的服務(wù)器以禁用所有V2 API端點。

舊的API和v2 API的不同在三個主要方面體現(xiàn):

  1. 命令格式:通過HTTP GET請求的URL請求參數(shù)提供舊的API命令和相關(guān)的參數(shù),而在v2 API中,大多數(shù)API命令是通過JSON體POST到v2 API端點提供的。v2 API還在適當(dāng)?shù)牡胤街С諬TTP方法:GET和DELETE。
  2. 端點結(jié)構(gòu):v2 API端點結(jié)構(gòu)已合理化和規(guī)范化。
  3. 文檔:v2 API是自我記錄的:附加/_introspect到任何有效的v2 API路徑,API規(guī)范將以JSON格式返回。

v2 API路徑前綴

以下是一些v2 API URL路徑和路徑前綴,以及在這些路徑及其子路徑中支持的一些操作。

路徑前綴一些支持的操作

/api/collections 或 /api/c

創(chuàng)建,別名,備份和恢復(fù)集合

/api/c/collection-name/update

更新請求

/api/c/collection-name/config

配置請求

/api/c/collection-name/schema

架構(gòu)請求

/api/c/collection-name/handler-name

處理程序特定的請求

/api/c/collection-name/shards

拆分碎片,創(chuàng)建碎片,添加副本

/api/c/collection-name/shards/shard-name

刪除碎片,強制領(lǐng)導(dǎo)人選舉

/api/c/collection-name/shards/shard-name/replica-name

刪除副本

/api/cores

創(chuàng)建一個核心

/api/cores/core-name

重新加載,重命名,刪除和卸載核心

/api/node

執(zhí)行監(jiān)督操作,重新組織領(lǐng)導(dǎo)選舉

/api/cluster

添加角色,刪除角色,設(shè)置集群屬性

/api/c/.system/blob

上傳和下載blob和元數(shù)據(jù)

Introspect

附加/_introspect到任何有效的v2 API路徑,API規(guī)范將以JSON格式返回:

http://localhost:8983/api/c/_introspect

為了限制introspect輸出到僅包括一個特定的HTTP方法中,請?zhí)砑泳哂兄礕ET、POST或DELETE的請求參數(shù)method:

http://localhost:8983/api/c/_introspect?method=POST

大多數(shù)端點支持通過POST發(fā)送的主體中提供的命令。要將反省輸出限制為只有一個命令,請?zhí)砑诱埱髤?shù):command=command-name,

http://localhost:8983/api/c/gettingstarted/_introspect?method=POST&command=modify

解釋Introspect輸出

例如:http://localhost:8983/api/c/gettingstarted/get/_introspect

{
  "spec":[{
      "documentation":"https://lucene.apache.org/solr/guide/real-time-get.html",
      "description":"RealTime Get allows retrieving documents by ID before the documents have been committed to the index. It is useful when you need access to documents as soon as they are indexed but your commit times are high for other reasons.",
      "methods":["GET"],
      "url":{
        "paths":["/c/gettingstarted/get"],
        "params":{
          "id":{
            "type":"string",
            "description":"A single document ID to retrieve."},
          "ids":{
            "type":"string",
            "description":"One or more document IDs to retrieve. Separate by commas if more than one ID is specified."},
          "fq":{
            "type":"string",
            "description":"An optional filter query to add to the query. One use case for this is security filtering, in case users or groups should not be able to retrieve the document ID requested."}}}}],
  "WARNING":"This response format is experimental.  It is likely to change in the future.",
  "availableSubPaths":{}
}

文檔: 此 API 的聯(lián)機 Solr 參考指南部分的 URL
描述: 特征/變量/命令等的文本描述。
規(guī)范/方法: 此 API 支持的 HTTP 方法
規(guī)范/url/路徑: 此 API 支持的 url 路徑
規(guī)范/url/參數(shù): 支持的 url 請求參數(shù)列表
availableSubPaths: 有效 URL 路徑和 HTTP 方法的列表每個支持


對上面的例子中的一些鍵的描述:

  • documentation :該API的聯(lián)機Solr參考指南部分的URL
  • description :特征/變量/命令(feature/variable/command)的文本描述等
  • spec/methods :此API支持的HTTP方法
  • spec/url/paths :此API支持的URL路徑
  • spec/url/params :支持的URL請求參數(shù)列表
  • availableSubPaths :有效的URL子路徑列表和每個支持的HTTP方法

introspect的一個POST API的例子: http://localhost:8983/api/c/gettingstarted/_introspect?method=POST&command=modify

{
  "spec":[{
      "documentation":"https://lucene.apache.org/solr/guide/collections-api.html",
      "description":"Several collection-level operations are supported with this endpoint: modify collection attributes; reload a collection; migrate documents to a different collection; rebalance collection leaders; balance properties across shards; and add or delete a replica property.",
      "methods":["POST"],
      "url":{"paths":["/collections/{collection}",
          "/c/{collection}"]},
      "commands":{"modify":{
          "documentation":"https://lucene.apache.org/solr/guide/collections-api.html#modifycollection",
          "description":"Modifies specific attributes of a collection. Multiple attributes can be changed at one time.",
          "type":"object",
          "properties":{
            "rule":{
              "type":"array",
              "documentation":"https://lucene.apache.org/solr/guide/rule-based-replica-placement.html",
              "description":"Modifies the rules for where replicas should be located in a cluster.",
              "items":{"type":"string"}},
            "snitch":{
              "type":"array",
              "documentation":"https://lucene.apache.org/solr/guide/rule-based-replica-placement.html",
              "description":"Details of the snitch provider",
              "items":{"type":"string"}},
            "autoAddReplicas":{
              "type":"boolean",
              "description":"When set to true, enables auto addition of replicas on shared file systems (such as HDFS). See https://lucene.apache.org/solr/guide/running-solr-on-hdfs.html for more details on settings and overrides."},
            "replicationFactor":{
              "type":"string",
              "description":"The number of replicas to be created for each shard. Replicas are physical copies of each shard, acting as failover for the shard. Note that changing this value on an existing collection does not automatically add more replicas to the collection. However, it will allow add-replica commands to succeed."},
            "maxShardsPerNode":{
              "type":"integer",
              "description":"When creating collections, the shards and/or replicas are spread across all available, live, nodes, and two replicas of the same shard will never be on the same node. If a node is not live when the collection is created, it will not get any parts of the new collection, which could lead to too many replicas being created on a single live node. Defining maxShardsPerNode sets a limit on the number of replicas can be spread to each node. If the entire collection can not be fit into the live nodes, no collection will be created at all."}}}}}],
  "WARNING":"This response format is experimental.  It is likely to change in the future.",
  "availableSubPaths":{
    "/c/gettingstarted/select":["POST", "GET"],
    "/c/gettingstarted/config":["POST", "GET"],
    "/c/gettingstarted/schema":["POST", "GET"],
    "/c/gettingstarted/export":["POST", "GET"],
    "/c/gettingstarted/admin/ping":["POST", "GET"],
    "/c/gettingstarted/update":["POST"]}
}

以上示例中的"commands"部分對于此端點支持的每個命令都有一個條目。關(guān)鍵是命令名,值是使用JSON架構(gòu)描述命令結(jié)構(gòu)的json對象(參見http://json-schema.org/)。

調(diào)用示例

對于“gettingstarted”集合,請設(shè)置復(fù)制因子以及是否自動添加副本(請參閱上面的"modify"用于此處使用的命令的introspect輸出):

$ curl http://localhost:8983/api/c/gettingstarted -H 'Content-type:application/json' -d '
{ modify: { replicationFactor: "3", autoAddReplicas: false } }'

{"responseHeader":{"status":0,"QTime":842}}

查看集群的狀態(tài):

$ curl http://localhost:8983/api/cluster

{"responseHeader":{"status":0,"QTime":0},"collections":["gettingstarted",".system"]}

設(shè)置一個集群屬性:

$ curl http://localhost:8983/api/cluster -H 'Content-type: application/json' -d '
{ set-property: { name: autoAddReplicas, val: "false" } }'

{"responseHeader":{"status":0,"QTime":4}}
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號