代码之家  ›  专栏  ›  技术社区  ›  Swordfish

APOC Elasticsearch查询格式

  •  0
  • Swordfish  · 技术社区  · 6 年前

    我使用的是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````
    

    我需要做什么才能正确地通过查询?

    0 回复  |  直到 6 年前
        1
  •  0
  •   Swordfish    6 年前

    成功了,我忽略了 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)
    
        2
  •  0
  •   phhu Dan Milon    6 年前

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

    推荐文章