上下文加载 · Cypher 查询 API (0.1.4)

Download OpenAPI specification:

用只读 Cypher 查知识网络:标签是对象类,关系类型是关系类,属性是逻辑属性名。 本服务不自带查询语言实现——它把请求带着调用方身份转给 bkn-backend,由那边按 知识网络的模型把 Cypher 编译成一条 SQL 下发,再把行返回来。

与其他查询入口的分工:

  • object-instance.yaml 的 query_object_instance: 单个对象类的平面过滤,不跨关系。
  • instance-subgraph.yaml 的 query_instance_subgraph / explore_subgraph:要的是子图(节点 + 边的拓扑)。
  • 本接口:要的是表(按路径过滤后投影出的行,可聚合、可排序、可分页), 路径与条件写在一句 Cypher 里。
  • data-access.yaml 的 run_sql:绕开本体直查物理表。 本接口表达不了的口径(CTE、UNION、窗口函数)或资源没建成对象类时才落到那里。

不需要 resource_id,也不需要物理列名——名字全部取自 schema-search.yaml 的 search_schema 或 kn-explore.yaml 的 get_object_types / get_relation_types。

授权:由 bkn-backend 按调用方账户裁决——语句点名的每个对象类与关系类都要有 query_data(网络级授权会继承到其下的对象类与关系类),再往下 vega 对每个资源 校验读取权限。无权查询的概念在编译用的模型里直接不存在,写了只会报「未知标签」, 而不是「无权」,所以不能拿本接口探测模型里有什么。本服务只做转发,不放大调用方能 读的范围。

认证:Authorization: Bearer <token>(OAuth access token 或 bak_ AppKey)。

Cypher

对知识网络执行只读 Cypher

编译并执行一条只读 Cypher 语句。支持的子集:

  • MATCH:一个或多个 MATCH 子句;一个 MATCH 里可以写逗号分隔的多条路径。 同名变量指同一个节点;不共享变量的两部分是笛卡尔积。
  • 节点:变量首次出现时必须带标签(对象类 id 或名称),可带内联属性 {prop: value}。
  • 关系:-[:R]->、<-[:R]-、无向 -[:R]-,每条必须指名一个关系类; 一条查询的模式最多 8 条关系、9 个节点(不与其他节点相连的节点也各算一张表)。 关系变量(-[r:R]->)不支持。
  • OPTIONAL MATCH:写一条关系,把一个新节点挂到前面子句已经找到的节点上。 匹配不到时该行保留,新节点的列为 NULL,所以 count(可选侧的属性) 得 0 而不是整行 消失(count(*) 仍然算 1 行)——按邻居数排序又不丢零度邻居,用的就是这一点。 它自带的 WHERE 与内联属性决定这条关系匹配到什么,不决定哪些行留下。 限制:首个子句必须是 MATCH;一条 OPTIONAL MATCH 只能写一条关系、只能引入一个 新节点(两端都已匹配或两端都是新节点都会被拒);它引入的节点不能被后面的 MATCH 或查询的 WHERE 再要求,否则留下来的行会被重新删掉,这类查询一律拒绝而不是照做。
  • WHERE:= <> < > <= >=、IN [...]、IS NULL / IS NOT NULL, STARTS WITH / ENDS WITH / CONTAINS,用 AND / OR / NOT 组合,可加括号。 值可写 $name 参数;IN $list 可把整个列表作为一个参数传入。字符串谓词左侧须为 变量.属性,右侧须为字符串字面量或取值为字符串的参数;按 openCypher 语义做字面 子串匹配(%、_ 等不是通配符),属性为 null 时不匹配;大小写是否敏感取决于 数据源列的排序规则(collation)。只能用于字符串属性,用在数值、日期等属性上返回 400。
  • RETURN:属性、AS 别名、DISTINCT,以及 count / sum / avg / min / max(含 count(*) 与 DISTINCT 形式)。有聚合时按 RETURN 中其余 列自动分组——这是 Cypher 的隐式 GROUP BY,无需也不能自己写 GROUP BY。
  • 算术:+ - * / % 与取负,可加括号,可写在 RETURN(含聚合的参数)、 WHERE 比较的两侧和 ORDER BY 里,如 count(*) * 2、sum(o.price * o.qty)、 count(o.x) * 1.0 / count(*)、WHERE o.amount * o.rate > $floor、 ORDER BY count(g.id) * 0.5 + p.rank * 0.3 DESC。操作数是数值属性、聚合、数字或 取值为数字的参数;字符串、布尔、null 与非数值属性返回 400。与 openCypher 语义不一致的 写法一律拒绝而不是照做:两个整数相除(Cypher 丢余数,各 SQL 库不一致),要小数结果把 一边乘 1.0;% 只用于整数;除以字面量 0;^ 乘方。聚合只能出现在 RETURN 与 ORDER BY 里,不能写在 WHERE 中。一个列或排序键既有聚合又在聚合外直接读属性(如 o.x - avg(o.x))时,该属性须同时作为单独的列返回,结果按它分组;DISTINCT 结果要按 算出来的值排序,请在 RETURN 里给它起别名再按别名排序。没有声明类型的属性交给数据源决定。 IN 列表、内联属性、SKIP / LIMIT 里仍不能写算术。
  • ORDER BY / SKIP / LIMIT:ORDER BY 可用属性、返回列名或聚合。结果被聚合 或 DISTINCT 折叠后,只能按返回的值排序。

不支持(会被拒绝,报错指名是哪个语法与位置):WITH、 UNION、变长路径 [:R*1..3]、XOR、返回整个节点、函数调用、^ 乘方、写操作(CREATE / MERGE / DELETE / SET / REMOVE)。

行数:不写 LIMIT 默认返回 1000 行;LIMIT 不得超过 10000。

关系唯一性:按 openCypher 语义,同一条路径里共用同一关系类的多跳互不相同, 编译器会自动补上主键不等的条件,调用方不必自己写。该规则按子句划分,因此 OPTIONAL MATCH 的那一跳不参与——它补出的条件会落在 WHERE 里,正好删掉外连接 保住的行。

Authorizations:
OAuth2AppKey
query Parameters
response_format
string
Default: "json"
Enum: "json" "toon"

响应格式。toon 返回 application/toon,同构数组压成表格,省 token。

Request Body schema: application/json
required
object (BKNContext)

BKN Trace business-provenance context identifying the business conversation and interaction to which this call belongs. Required on MCP business tools and REST execution APIs while capture is enabled and Trace Core is available; optional on REST reads and /mcp/proxy/.../call. A verified disabled capture policy admits business calls without this context because no Conversation or Interaction IDs are issued. A verified Trace Core infrastructure outage also admits unrecorded REST execution after a bounded, read-only availability probe.

Naming it makes the call managed: the service records an Operation under that interaction and attempts to return a receipt and capture evidence. Omitting it entirely, or sending an empty object, makes an eligible REST read or proxy call ad hoc. Only those two shapes are ad hoc — one id without the other, or a misspelt field, is refused rather than quietly downgraded.

Both required child fields are server-issued handles and must not be invented by the caller:

  • conversation_id: obtained from the Context Loader MCP tool bkn_start_interaction on the first turn
  • interaction_id: obtained from the Context Loader MCP tool bkn_start_interaction

These handles are available only through the provenance MCP tool above. The BKN Trace Core session APIs under /api/agent-observability/v1 accept gateway credentials only from trusted in-cluster services. They do not accept OAuth tokens or bak_ AppKeys; direct external calls receive 401 permission_denied. Context Loader derives the Operation idempotency identity on the server from request correlation, tool name, and normalized input. Callers must not submit operation_key. Network retries should reuse bkn-request-id or provide a stable X-OpenBKN-Client-Invocation-Id for the same logical call. This hint is used only for idempotency and has no role in identity, authorization, or data-isolation decisions.

kn_id
required
string

知识网络 ID,来自 list_knowledge_networks。

branch
string

分支名。不传按主分支查。

query
required
string

只读 Cypher 语句,语法子集见端点说明。

object

语句里 $name 的取值。只能是字符串、数字或布尔值——参数是值,不会变成 表名或列名,因此不能用来拼标签、关系类型或属性名。null 会被拒绝: 判空写 IS NULL。用在 IN $name 右侧的参数须为非空数组,元素为同一类标量 (整数与小数都算数字),不能含 null,最多 500 个,如 {"keys": [6, 7, 8]}。 一条查询里多个长列表合计过大时会返回 400,需拆成多次查询。

Responses

Request samples

Content type
application/json
Example
{
  • "kn_id": "kn_retail",
  • "query": "MATCH (c:customer)<-[:rel_order_customer]-(o:order)-[:rel_order_item_order]->(i:order_item) WHERE c.city = 'Shanghai' AND o.status IN ['paid', 'shipped'] RETURN c.name AS customer, o.order_no AS order_no, i.sku AS sku LIMIT 100"
}

Response samples

Content type
{
  • "columns": [
    ],
  • "entries": [
    ]
}