跳转至

Elasticsearch 查询函数

Elasticsearch 查询函数可以让 NebulaGraph 向 Elasticsearch 发送 GET 请求读取数据,并将返回的 JSON 转换为 NebulaGraph 内的 map 格式,以便用于同一个复合图查询语句中。

关于 Elasticsearch 语法,请参见 Elasticsearch 官方文档

Enterpriseonly

仅企业版支持本功能。

注意事项

  • 不能使用已经部署了全文索引的 Elasticsearch 服务,需要使用独立部署的 Elasticsearch 服务,并且用户需要自行向 Elasticsearch 中写入数据,NebulaGraph 不会向该 Elasticsearch 写入数据。部署 Elasticsearch 集群请参见 Kubernetes 安装 Elasticsearch单机安装 Elasticsearch
  • 使用前需要 SIGN IN 服务,例如SIGN IN TEXT SERVICE (127.0.0.1:9200, HTTP),不再使用时可以 SIGN OUT 服务。具体语法请参见登录文本搜索客户端
  • 只支持 GET 请求。请用 Elasticsearch 客户端执行 POST、PUT、DELETE 请求。
  • 没有超时时间,NebulaGraph 会一直等待 Elasticsearch 返回结果。
  • 不支持函数后直接跟操作符,例如RETURN calles("/test/player/1")["_source"]
  • 兼容 Elasticsearch 7 及以上版本,更低版本未测试。

openCypher 兼容性

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

语法

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

calles("<es_query>");
callesfile("<es_query_file_path>");
  • 返回类型:map。Elasticsearch 返回 JSON 格式字符串,NebulaGraph 转换成 map 格式。

示例

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"}) |
+---------------------------------------------------+

//对于不存在的数据,会返回 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"})>   |
...

最后更新: August 9, 2022