首页 > 解决方案 > APOC Elasticsearch 查询格式

问题描述

我正在使用 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````

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

标签: elasticsearchneo4jneo4j-apoc

解决方案


如果您想使用滚动 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.htmlhttps://neo4j.com/docs/labs/apoc/4.0/database-integration/elasticsearch /


推荐阅读