cypher-shell
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesecypher-shell
cypher-shell
Neo4j Cypher Shell skill. Connect once, query fast, inspect schemas.
Neo4j Cypher Shell 技能:一次连接,快速查询,查看架构。
Connection Config
连接配置
connect~/.neo4j-connection~/.zshrc~/.bashrc~/.profile| Env Var | cypher-shell flag |
|---|---|
| |
| |
| |
| |
Execution pattern (after connect):
bash
cypher-shell --format verbose "<QUERY>"No , no flags, no credentials. Just + query.
sourcecypher-shellPre-flight for every command (except and ): verify env var is set. If not:
connectinstallNEO4J_URIbash
if [ -z "$NEO4J_URI" ]; then echo "Not connected. Run /cypher-shell connect <uri>"; exit 1; ficonnect~/.neo4j-connection~/.zshrc~/.bashrc~/.profile| 环境变量 | cypher-shell 参数 |
|---|---|
| |
| |
| |
| |
执行模式(连接后):
bash
cypher-shell --format verbose "<QUERY>"无需 、无需参数、无需凭据。只需输入 + 查询语句即可。
sourcecypher-shell除 和 外的所有命令执行前都会检查环境变量 是否已设置。如果未设置:
connectinstallNEO4J_URIbash
if [ -z "$NEO4J_URI" ]; then echo "未连接,请运行 /cypher-shell connect <uri>"; exit 1; fiRoute $ARGUMENTS
路由 $ARGUMENTS
Parse the first word of to determine the action:
$ARGUMENTS| First word | Action |
|---|---|
| Connect to instance |
| Run Cypher query |
| Retrieve graph schema |
| Test current connection |
| Show install instructions |
| (none) | Show usage help |
Everything after the first word becomes the sub-argument.
解析 的第一个单词来确定执行操作:
$ARGUMENTS| 第一个单词 | 操作 |
|---|---|
| 连接到实例 |
| 运行Cypher查询 |
| 获取图数据库架构 |
| 测试当前连接 |
| 显示安装说明 |
| (无) | 显示使用帮助 |
第一个单词之后的所有内容将作为子参数。
connect [URI]
connect [URI]
With URI (contains ://
)
://带URI(包含 ://
)
://- Ask user for username (default: ) and password
neo4j - Optionally ask for database name
- Write credentials file:
bash
cat > ~/.neo4j-connection << 'EOF'
export NEO4J_URI="<uri>"
export NEO4J_USERNAME="<user>"
export NEO4J_PASSWORD="<pass>"
export NEO4J_DATABASE="<db>"
EOF
chmod 600 ~/.neo4j-connection- Inject loader into shell profile so every future shell session auto-loads credentials. Detect the profile file and append the loader line only if not already present:
bash
undefined- 询问用户用户名(默认:)和密码
neo4j - 可选询问数据库名称
- 写入凭据文件:
bash
cat > ~/.neo4j-connection << 'EOF'
export NEO4J_URI="<uri>"
export NEO4J_USERNAME="<user>"
export NEO4J_PASSWORD="<pass>"
export NEO4J_DATABASE="<db>"
EOF
chmod 600 ~/.neo4j-connection- 向Shell配置文件注入加载代码,以便未来所有Shell会话自动加载凭据。检测配置文件并仅在代码不存在时追加加载行:
bash
undefinedDetect shell profile
检测Shell配置文件
if [ -f /.zshrc ]; then
SHELL_PROFILE=/.zshrc
elif [ -f /.bashrc ]; then
SHELL_PROFILE=/.bashrc
elif [ -f /.bash_profile ]; then
SHELL_PROFILE=/.bash_profile
else
SHELL_PROFILE=~/.profile
fi
if [ -f /.zshrc ]; then
SHELL_PROFILE=/.zshrc
elif [ -f /.bashrc ]; then
SHELL_PROFILE=/.bashrc
elif [ -f /.bash_profile ]; then
SHELL_PROFILE=/.bash_profile
else
SHELL_PROFILE=~/.profile
fi
Append loader only if not already there
仅在不存在时追加加载代码
LOADER='[ -f ~/.neo4j-connection ] && source ~/.neo4j-connection'
grep -qF "$LOADER" "$SHELL_PROFILE" 2>/dev/null || echo "$LOADER" >> "$SHELL_PROFILE"
5. **Load credentials into current session** (so this session works immediately without restart):
```bash
source ~/.neo4j-connection- Test connection:
cypher-shell "RETURN 'connected' AS status;" - On success, fetch server info:
bash
cypher-shell --format verbose \
"CALL dbms.components() YIELD name, versions, edition RETURN name, versions[0] AS version, edition;"bash
cypher-shell --format verbose \
"SHOW DATABASES YIELD name, currentStatus, default RETURN name, currentStatus, default ORDER BY name;"Report: version, edition, databases, URI. Confirm that credentials are persisted and no flags needed going forward.
LOADER='[ -f ~/.neo4j-connection ] && source ~/.neo4j-connection'
grep -qF "$LOADER" "$SHELL_PROFILE" 2>/dev/null || echo "$LOADER" >> "$SHELL_PROFILE"
5. **将凭据加载到当前会话**(使当前会话无需重启即可立即使用):
```bash
source ~/.neo4j-connection- 测试连接:
cypher-shell "RETURN 'connected' AS status;" - 连接成功后,获取服务器信息:
bash
cypher-shell --format verbose \
"CALL dbms.components() YIELD name, versions, edition RETURN name, versions[0] AS version, edition;"bash
cypher-shell --format verbose \
"SHOW DATABASES YIELD name, currentStatus, default RETURN name, currentStatus, default ORDER BY name;"报告内容:版本、版本类型、数据库、URI。确认凭据已持久化,后续操作无需再输入参数。
Without URI
不带URI
If exists, show current config (mask password). Otherwise ask for URI.
~/.neo4j-connection如果 文件已存在,显示当前配置(密码会被掩码处理)。否则询问用户URI。
~/.neo4j-connectionProtocol schemes
协议方案
| Scheme | Encryption | Routing |
|---|---|---|
| None | Yes (cluster) |
| TLS (CA-verified) | Yes |
| TLS (self-signed) | Yes |
| None | No (single) |
| TLS (CA-verified) | No |
| TLS (self-signed) | No |
| 协议 | 加密 | 路由 |
|---|---|---|
| 无 | 支持(集群) |
| TLS(CA验证) | 支持 |
| TLS(自签名) | 支持 |
| 无 | 不支持(单实例) |
| TLS(CA验证) | 不支持 |
| TLS(自签名) | 不支持 |
test
test
- Verify env var is set (if not, tell user to run
NEO4J_URI)/cypher-shell connect <uri> cypher-shell "RETURN 'ok' AS status;"- Show: URI, user, database, version
- 验证 环境变量是否已设置(如果未设置,提示用户运行
NEO4J_URI)/cypher-shell connect <uri> - 执行
cypher-shell "RETURN 'ok' AS status;" - 显示:URI、用户、数据库、版本
Troubleshooting (if test fails)
故障排查(测试失败时)
- — not found? ->
which cypher-shellinstall - — need 21+
java -version - — port not open? Neo4j not running
nc -z -w3 <host> <port> - Auth error — reset at http://localhost:7474 or
cypher-shell --change-password - TLS error — try or
neo4j+ssc://bolt://
- — 未找到?-> 运行
which cypher-shellinstall - — 需要Java 21+
java -version - — 端口未开放?Neo4j未运行
nc -z -w3 <host> <port> - 认证错误 — 在 http://localhost:7474 重置或运行
cypher-shell --change-password - TLS错误 — 尝试使用 或
neo4j+ssc://bolt://
install
install
Print install instructions for the user (do NOT run them):
- macOS:
brew install cypher-shell - Debian/Ubuntu:
curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/neo4j.gpg && echo 'deb [signed-by=/usr/share/keyrings/neo4j.gpg] https://debian.neo4j.com stable latest' | sudo tee /etc/apt/sources.list.d/neo4j.list && sudo apt-get update && sudo apt-get install -y cypher-shell - Docker:
docker run --rm -it --network host neo4j/neo4j:latest cypher-shell
Requires Java 21+.
打印安装说明(不会自动执行):
- macOS:
brew install cypher-shell - Debian/Ubuntu:
curl -fsSL https://debian.neo4j.com/neotechnology.gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/neo4j.gpg && echo 'deb [signed-by=/usr/share/keyrings/neo4j.gpg] https://debian.neo4j.com stable latest' | sudo tee /etc/apt/sources.list.d/neo4j.list && sudo apt-get update && sudo apt-get install -y cypher-shell - Docker:
docker run --rm -it --network host neo4j/neo4j:latest cypher-shell
需要 Java 21+ 环境。
query [CYPHER or shortcut]
query [CYPHER 或快捷命令]
Full Cypher
完整Cypher语句
If sub-argument contains Cypher keywords (, , , , , , , , , , , , , , , , ), run directly:
MATCHCREATEMERGECALLSHOWRETURNWITHUNWINDLOADDROPDELETEEXPLAINPROFILEFILTERLETSEARCHFINISHbash
cypher-shell --format verbose "<sub-argument>"如果子参数包含Cypher关键字(、、、、、、、、、、、、、、、、),则直接运行:
MATCHCREATEMERGECALLSHOWRETURNWITHUNWINDLOADDROPDELETEEXPLAINPROFILEFILTERLETSEARCHFINISHbash
cypher-shell --format verbose "<sub-argument>"Shortcuts
快捷命令
| Shortcut | Query |
|---|---|
| |
| |
| |
| |
| WARN + ASK CONFIRMATION then |
| 快捷命令 | 对应查询 |
|---|---|
| |
| |
| |
| |
| 警告 + 确认提示 然后执行 |
No sub-argument -> show shortcuts list.
无子参数 -> 显示快捷命令列表。
schema [Label]
schema [标签]
Full schema (no label argument)
完整架构(无标签参数)
Run ALL and present formatted summary:
cypher
MATCH (n) RETURN labels(n) AS label, count(n) AS count ORDER BY count DESC;cypher
MATCH ()-[r]->() RETURN type(r) AS type, count(r) AS count ORDER BY count DESC;cypher
MATCH (a)-[r]->(b) RETURN DISTINCT labels(a) AS from_label, type(r) AS relationship, labels(b) AS to_label ORDER BY from_label, relationship, to_label;cypher
MATCH (n) WITH labels(n) AS lbls, keys(n) AS props RETURN DISTINCT lbls AS label, props AS properties ORDER BY lbls;cypher
MATCH ()-[r]->() WITH type(r) AS rel, keys(r) AS props WHERE size(props) > 0 RETURN DISTINCT rel AS relationship, props AS properties ORDER BY rel;cypher
SHOW INDEXES YIELD name, type, labelsOrTypes, properties, state RETURN name, type, labelsOrTypes, properties, state ORDER BY name;cypher
SHOW CONSTRAINTS YIELD name, type, labelsOrTypes, properties RETURN name, type, labelsOrTypes, properties ORDER BY name;Present as ASCII schema map:
Graph Schema
============
Nodes: X total across Y labels
Rels: Z total across W types
[File] (145) props: name, path, language
--[:CONTAINS]--> [Function]
--[:IMPORTS]--> [File]
[Function] (312) props: name, file, line
--[:CALLS]--> [Function]
Indexes: idx_file_path RANGE :File(path) ONLINE
Constraints: uniq_file_id UNIQUENESS :File(id)运行所有查询并展示格式化的摘要:
cypher
MATCH (n) RETURN labels(n) AS label, count(n) AS count ORDER BY count DESC;cypher
MATCH ()-[r]->() RETURN type(r) AS type, count(r) AS count ORDER BY count DESC;cypher
MATCH (a)-[r]->(b) RETURN DISTINCT labels(a) AS from_label, type(r) AS relationship, labels(b) AS to_label ORDER BY from_label, relationship, to_label;cypher
MATCH (n) WITH labels(n) AS lbls, keys(n) AS props RETURN DISTINCT lbls AS label, props AS properties ORDER BY lbls;cypher
MATCH ()-[r]->() WITH type(r) AS rel, keys(r) AS props WHERE size(props) > 0 RETURN DISTINCT rel AS relationship, props AS properties ORDER BY rel;cypher
SHOW INDEXES YIELD name, type, labelsOrTypes, properties, state RETURN name, type, labelsOrTypes, properties, state ORDER BY name;cypher
SHOW CONSTRAINTS YIELD name, type, labelsOrTypes, properties RETURN name, type, labelsOrTypes, properties ORDER BY name;以ASCII架构图形式展示:
Graph Schema
============
Nodes: X total across Y labels
Rels: Z total across W types
[File] (145) props: name, path, language
--[:CONTAINS]--> [Function]
--[:IMPORTS]--> [File]
[Function] (312) props: name, file, line
--[:CALLS]--> [Function]
Indexes: idx_file_path RANGE :File(path) ONLINE
Constraints: uniq_file_id UNIQUENESS :File(id)Label deep-dive (argument matches a label)
标签深度查询(参数匹配某个标签)
cypher
MATCH (n:<LABEL>) RETURN n LIMIT 5;cypher
MATCH (n:<LABEL>) WITH n LIMIT 100 UNWIND keys(n) AS key
RETURN DISTINCT key AS property, head(collect(DISTINCT valueType(n[key]))) AS type, count(*) AS present_in ORDER BY present_in DESC;cypher
MATCH (n:<LABEL>)-[r]->(m) RETURN type(r) AS rel, labels(m) AS target, count(*) AS count ORDER BY count DESC;cypher
MATCH (n:<LABEL>)<-[r]-(m) RETURN type(r) AS rel, labels(m) AS source, count(*) AS count ORDER BY count DESC;cypher
MATCH (n:<LABEL>) RETURN count(n) AS total;cypher
MATCH (n:<LABEL>) WHERE NOT (n)--() RETURN count(n) AS orphans;cypher
MATCH (n:<LABEL>) RETURN n LIMIT 5;cypher
MATCH (n:<LABEL>) WITH n LIMIT 100 UNWIND keys(n) AS key
RETURN DISTINCT key AS property, head(collect(DISTINCT valueType(n[key]))) AS type, count(*) AS present_in ORDER BY present_in DESC;cypher
MATCH (n:<LABEL>)-[r]->(m) RETURN type(r) AS rel, labels(m) AS target, count(*) AS count ORDER BY count DESC;cypher
MATCH (n:<LABEL>)<-[r]-(m) RETURN type(r) AS rel, labels(m) AS source, count(*) AS count ORDER BY count DESC;cypher
MATCH (n:<LABEL>) RETURN count(n) AS total;cypher
MATCH (n:<LABEL>) WHERE NOT (n)--() RETURN count(n) AS orphans;cypher-shell CLI Reference
cypher-shell CLI 参考
Key flags
关键参数
| Flag | Description | Default |
|---|---|---|
| Connection URI | |
| Username | env |
| Password | env |
| Database | env |
| Execute .cypher file | — |
| Set params: | |
| Output format | |
| Access mode | |
| Force non-interactive | |
| Error handling for files | |
| Rows for table width | |
| Wrap long columns | |
| e.g. | |
| Error display | |
| Encryption | |
| Run as user | — |
| Change password | — |
| Tab-complete (5+) | |
| Query notifications | |
| Auto-exit | |
| Debug log | — |
| Version | — |
| 参数 | 描述 | 默认值 |
|---|---|---|
| 连接URI | |
| 用户名 | 环境变量 |
| 密码 | 环境变量 |
| 数据库 | 环境变量 |
| 执行.cypher文件 | — |
| 设置参数: | |
| 输出格式 | |
| 访问模式 | |
| 强制非交互模式 | |
| 文件执行的错误处理 | |
| 用于计算表格宽度的行数 | |
| 长列自动换行 | |
| 事务超时时间,例如 | |
| 错误显示格式 | |
| 加密设置 | |
| 模拟指定用户运行 | — |
| 修改密码 | — |
| 启用Tab补全(版本5+) | |
| 查询通知 | |
| 空闲自动退出 | |
| 调试日志 | — |
| 显示版本 | — |
Shell commands (interactive)
交互式Shell命令
:help:exit:use <db>:source <file>:param {k:v}:param k => expr:param:param clear:begin:commit:rollback:history:connect:disconnect:sysinfo:impersonate:access-mode [read|write]:help:exit:use <db>:source <file>:param {k:v}:param k => expr:param:param clear:begin:commit:rollback:history:connect:disconnect:sysinfo:impersonate:access-mode [read|write]Cypher quick patterns
Cypher 常用模式
cypher
MATCH (n:Label {prop: "val"}) RETURN n;
MATCH (a)-[:REL]->(b) RETURN a.name, b.name;
MATCH path = (a)-[*1..3]->(b) WHERE a.name = "x" RETURN path;
MATCH path = shortestPath((a {name:"x"})-[*]-(b {name:"y"})) RETURN path;
MATCH (n:Label) RETURN n.prop, count(n) ORDER BY count(n) DESC;
MERGE (n:Label {id: "x"}) ON CREATE SET n.created = timestamp() ON MATCH SET n.updated = timestamp();
CALL { MATCH (n:Label) WITH n LIMIT 10000 DETACH DELETE n } IN TRANSACTIONS OF 10000 ROWS;
LOAD CSV WITH HEADERS FROM 'file:///data.csv' AS row CREATE (:Label {name: row.name});
EXPLAIN MATCH (n)-[r]->(m) RETURN n, r, m;
PROFILE MATCH (n)-[r]->(m) RETURN n, r, m;Cypher 25 (Neo4j 2025.06+): , , , , , (vector), .
FILTERLETFINISHWHENNEXTSEARCHcoll.distinct/flatten/indexOf/insert/max/min/remove/sortcypher
MATCH (n:Label {prop: "val"}) RETURN n;
MATCH (a)-[:REL]->(b) RETURN a.name, b.name;
MATCH path = (a)-[*1..3]->(b) WHERE a.name = "x" RETURN path;
MATCH path = shortestPath((a {name:"x"})-[*]-(b {name:"y"})) RETURN path;
MATCH (n:Label) RETURN n.prop, count(n) ORDER BY count(n) DESC;
MERGE (n:Label {id: "x"}) ON CREATE SET n.created = timestamp() ON MATCH SET n.updated = timestamp();
CALL { MATCH (n:Label) WITH n LIMIT 10000 DETACH DELETE n } IN TRANSACTIONS OF 10000 ROWS;
LOAD CSV WITH HEADERS FROM 'file:///data.csv' AS row CREATE (:Label {name: row.name});
EXPLAIN MATCH (n)-[r]->(m) RETURN n, r, m;
PROFILE MATCH (n)-[r]->(m) RETURN n, r, m;Cypher 25(Neo4j 2025.06+)新增:, , , , , (向量), 。
FILTERLETFINISHWHENNEXTSEARCHcoll.distinct/flatten/indexOf/insert/max/min/remove/sort