APOC Elasticsearch 查询格式

APOC Elasticsearch query format

我正在使用 Neo4j 4.0.3、Elasticsearch 6.8.3 和 APOC。

我想 运行 使用 APOC Elasticsearch 程序通过 Neo4J 进行 Elasticsearch 聚合查询。

查询在请求正文中定义并在 Elasticsearch 上运行,但我不确定如何通过该过程构造查询参数。在 Elasticsearch 中,我可以将请求主体作为源参数传递(源内容类型为 JSON)。

为了演示,我尝试了一个简单的查询。

这适用于 Elasticsearch:

GET http://localhost:9200/trends/trend/_search?source={"query": {"match" : {"type" : "ENTITY"}}}&source_content_type=application/json

但是当我通过 ES 过程尝试这个时:

CALL apoc.es.query('elasticsearch:9200', 'trends', 'trend', 'source={"query":{"match":{"type":"ENTITY"}}}&source_content_type=application/json', null)

Neo4J 给出以下错误:

Failed to invoke procedure `apoc.es.query`: Caused by: java.net.URISyntaxException: Illegal character in query at index 54: http://elasticsearch:9200/trends/trend/_search?source={"query":{"match":{"type":%22ENTITY%22%7D%7D%7D````

我需要做什么才能正确传递查询?

成功了,我忽略了 payload 参数。所以 APOC 调用是这样的:

CALL apoc.es.query('elasticsearch:9200', 'trends', 'trend', null, '{"query":{"match":{"type":"ENTITY"}}}')

或者我可以将查询作为 Map

CALL apoc.es.query('elasticsearch:9200', 'trends', 'trend', apoc.map.fromPairs([
    ["source_content_type", "application/json"],
    ["source", '{"query":{"match":{"type":"ENTITY"}}}']
]), null)

如果您想使用滚动 ID 和有效载荷来指定查询,则可以使用如下方法:

CALL apoc.es.query('localhost','indexName','doc','scroll=1m','{
    "query":{
        "query_string":{
            "query": "name:somebody AND date:[now-30d TO now]"
        }
    },
    "size":"1000"
}') yield value
with value._scroll_id as scrollId, value.hits.hits as hits
// Do something with hits
UNWIND hits as hit
return hit

这种格式使编辑查询更容易:我发现不一致的结果和 URL 将查询放在第四个参数位置的字符串中时的转义规则。

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html and https://neo4j.com/docs/labs/apoc/4.0/database-integration/elasticsearch/