Search code examples
elasticsearchneo4jneo4j-apoc

APOC Elasticsearch query format


I am using Neo4j 4.0.3, Elasticsearch 6.8.3 and APOC.

I want to run an Elasticsearch aggregation query through Neo4J using the APOC Elasticsearch procedure.

The query is defined in the request body and works on Elasticsearch but I'm not sure how to construct the query parameter through the procedure. In Elasticsearch I can pass the request body as a source parameter (with a source content type of JSON).

To demonstrate, I've tried to get a simple query working.

This works in Elasticsearch:

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

but when I try this through the ES procedure:

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

Neo4J gives the following error:

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````

What do I need to do to pass the query correctly?


Solution

  • Got it to work, I overlooked the payload parameter. So the APOC call was this:

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

    Alternatively I could have passed the query as a Map

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