Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ES function #1886

Merged
merged 17 commits into from
Jun 30, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,4 @@ nebula> LOOKUP ON player WHERE FUZZY(player.name, "Tim Dunncan", AUTO, OR) YIELD
//删除全文索引。
nebula> DROP FULLTEXT INDEX nebula_index_1;
```

106 changes: 106 additions & 0 deletions docs-2.0/3.ngql-guide/6.functions-and-expressions/17.ES-function.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Elasticsearch 查询函数

Elasticsearch 查询函数可以让 Nebula Graph 向 Elasticsearch 发送 GET 请求读取数据,并将返回的 JSON 转换为Nebula Graph 内的 map 格式,以便用于同一个复合图查询语句中。
cooper-lzy marked this conversation as resolved.
Show resolved Hide resolved

关于 Elasticsearch 语法,请参见 [Elasticsearch 官方文档](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-your-data.html)。

!!! enterpriseonly

仅企业版支持本功能。

## 注意事项

- 不能使用已经部署了全文索引的 Elasticsearch 服务,需要使用独立部署的 Elasticsearch 服务,并且用户需要自行向 Elasticsearch 中写入数据,Nebula Graph 不会向该 Elasticsearch 写入数据。部署 Elasticsearch 集群请参见 [Kubernetes 安装 Elasticsearch](https://www.elastic.co/guide/en/cloud-on-k8s/current/k8s-deploy-elasticsearch.html) 或[单机安装 Elasticsearch](https://www.elastic.co/guide/en/elasticsearch/reference/7.15/targz.html)。

- 使用前需要 SIGN IN 服务,例如`SIGN IN TEXT SERVICE (127.0.0.1:9200, HTTP)`,不再使用时可以 SIGN OUT 服务。具体语法请参见[登录文本搜索客户端](../../4.deployment-and-installation/6.deploy-text-based-index/2.deploy-es.md#_3)。

- 只支持 GET 请求。请用 Elasticsearch 客户端执行 POST、PUT、DELETE 请求。

- 没有超时时间,Nebula Graph 会一直等待 Elasticsearch 返回结果。

- 不支持函数后直接跟操作符,例如`RETURN calles("/test/player/1")["_source"]`。

- 兼容 Elasticsearch 7及以上版本,更低版本未测试。
cooper-lzy marked this conversation as resolved.
Show resolved Hide resolved

## openCypher 兼容性

本文操作仅适用于 openCypher 方式。

## 语法

可以通过 calles() 直接执行 Elasticsearch 语句,也可以通过 callesfile() 执行指定文件内的 Elasticsearch 语句。

```ngql
calles("<es_query>");
callesfile("<es_query_file_path>");
cooper-lzy marked this conversation as resolved.
Show resolved Hide resolved
```

- 返回类型:map。Elasticsearch 返回 JSON 格式字符串,Nebula Graph 转换成 map 格式。

## 示例

```ngql
nebula> RETURN calles("/test/player/1") AS a;
+---------------------------------------------------------------------------------------------------------------------------------------------+
| a |
+---------------------------------------------------------------------------------------------------------------------------------------------+
| {_id: "1", _index: "test", _primary_term: 1, _seq_no: 0, _source: {age: 43, name: "Tim Duncan"}, _type: "player", _version: 1, found: true} |
+---------------------------------------------------------------------------------------------------------------------------------------------+

nebula> WITH calles("/test/player/1") AS a RETURN a;
+---------------------------------------------------------------------------------------------------------------------------------------------+
| a |
+---------------------------------------------------------------------------------------------------------------------------------------------+
| {_id: "1", _index: "test", _primary_term: 1, _seq_no: 0, _source: {age: 43, name: "Tim Duncan"}, _type: "player", _version: 1, found: true} |
+---------------------------------------------------------------------------------------------------------------------------------------------+

nebula> WITH calles("/test/player/1") AS a \
MATCH (b:player) WHERE b.player.age == a["_source"]["age"] \
RETURN b;
+---------------------------------------------------+
| b |
+---------------------------------------------------+
| ("player141" :player{age: 43, name: "Ray Allen"}) |
+---------------------------------------------------+
cooper-lzy marked this conversation as resolved.
Show resolved Hide resolved

//对于不存在的数据,会返回 found 字段为 false。
nebula> WITH calles("/test/player/123") AS a RETURN a;
+-------------------------------------------------------------+
| a |
+-------------------------------------------------------------+
| {_id: "123", _index: "test", _type: "player", found: false} |
+-------------------------------------------------------------+

//对于不存在的文件,会返回报错 read Elaticsearch file fail。
nebula> RETURN callesfile('/home/xxx/es_query_test.txt');
+---------------------------------------------+
| callESFile( "/home/xxx/es_query_test.txt" ) |
+---------------------------------------------+
| {error: "read Elaticsearch file fail."} |
+---------------------------------------------+

nebula> RETURN callesfile('/home/xxx/es_query.txt');
+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| callESFile( "/home/xxx/es_query.txt" ) |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
| {_shards: {failed: 0, skipped: 0, successful: 1, total: 1}, hits: {hits: [{_id: "2", _index: "test", _score: 1.0, _source: {age: 36, name: "Tony Parker"}... |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------+


nebula> WITH callesfile('/home/xxx/es_query.txt') AS s RETURN [n in s["hits"]["hits"]|n.`_source`.name];
+--------------------------------------------------------------------------------------------------------------------------------------+
| [n IN s["hits"]["hits"] | n._source.name] |
+--------------------------------------------------------------------------------------------------------------------------------------+
| ["LeBron James", "Danny Green", "Kevin Durant", "Stephen Curry", "Rudy Gay", "Ray Allen", "Tim Duncan", "Tony Parker", "Chris Paul"] |
+--------------------------------------------------------------------------------------------------------------------------------------+

nebula> WITH callesfile('/home/xxx/es_query.txt') AS s \
MATCH p= (a:player)-[:serve]->() \
WHERE id(a) in [n in s["hits"]["hits"]|n.`_source`.name] RETURN p;
+---------------------------------------------------------------------------------------------------------------------------------------------------+
| p |
+---------------------------------------------------------------------------------------------------------------------------------------------------+
| <("LeBron James" :player{age: 34, name: "LeBron James"})-[:serve@1 {end_year: 2018, start_year: 2014}]->("Cavaliers" :team{name: "Cavaliers"})> |
| <("LeBron James" :player{age: 34, name: "LeBron James"})-[:serve@0 {end_year: 2010, start_year: 2003}]->("Cavaliers" :team{name: "Cavaliers"})> |
...
```
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

- 全文索引不支持搜索属性值为`NULL`的属性。

- 写入数据时,Elastic Search 会自动创建相应的索引。在 Nebula Graph 中创建全文索引,但是未写入数据,查询时会报错`text search not found`。
cooper-lzy marked this conversation as resolved.
Show resolved Hide resolved

- 不支持修改 Elastic Search 中的索引,只能删除重建。
cooper-lzy marked this conversation as resolved.
Show resolved Hide resolved

- 不支持管道符。
Expand All @@ -37,3 +39,5 @@
- 从写入 Nebula Graph,到写入 listener,再到写入 Elasticsearch 并创建索引可能需要一段时间。如果访问全文索引时返回未找到索引,可等待索引生效(但是,该等待时间未知,也无返回码检查)。

- 使用 K8s 方式部署的 Nebula Graph 集群不支持全文索引。

- 不能与 [calles()](../../3.ngql-guide/6.functions-and-expressions/17.ES-function.md) 函数同时使用。
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ nav:
- 条件表达式函数: 3.ngql-guide/6.functions-and-expressions/5.conditional-expressions.md
- 谓词函数: 3.ngql-guide/6.functions-and-expressions/8.predicate.md
- geo 函数: 3.ngql-guide/6.functions-and-expressions/14.geo.md
# - Elasticsearch 查询函数: 3.ngql-guide/6.functions-and-expressions/17.ES-function.md
cooper-lzy marked this conversation as resolved.
Show resolved Hide resolved
- 自定义函数: 3.ngql-guide/6.functions-and-expressions/9.user-defined-functions.md

- 通用查询语句:
Expand Down