Neo4j APOC triggers and web APIs

If you work with Neo4j, you work with APOC plugin, there is just no way anyone would want to pass up the amazing options it adds to Neo4j`s cypher query language. It offers several awesome procedures and one of them is definitely apoc.meta.graph. My mission is to show you what can be achieved using awesome procedures.

Today I want to show you an example of apoc.trigger together with apoc.load.json for calling web APIs.

Requirements:

Set below in neo4j.conf

apoc.trigger.enabled=true

Web API:

I choose Google`s knowledge graph for enriching our data. Let me show you an example of cypher and walk you through it.

// we define our variables,  replace api_key with your own Google API key

WITH “Keanu+Reeves” as name,”google API key” as api_key

// we create our API url from variables and send a request with apoc.load.json

CALL apoc.load.json(“https://kgsearch.googleapis.com/v1/entities:search?query=” + name + “&key=” + key + “&limit=1&indent=True”)  YIELD value

// return values from web API — do whatever you want with them

UNWIND value.itemListElement as list RETURN list

This is very simple and easy to understand, so now lets get creative and put the apoc.trigger in the mix. I will be using the movies dataset that can be accessed in Neo4j browser by using.

:play movies

What I want to do, is to create a trigger for every Person node that gets created and search in Google`s Knowledge Graph for more info. Remember that when adding your trigger in Neo4j, you must keep your query in one line , but I will break it down into multiple lines for simplicity.

// we create a new trigger for all nodes, that are created and call it knowledge

CALL apoc.trigger.add(‘knowledge’, ‘UNWIND {createdNodes} AS n

// we filter by Person label and urlencode their names, so we do not have empty spaces in url

WITH n,apoc.text.urlencode(n.name) as name where labels(n) = “Person”

// we create an url from name and send request

CALL apoc.load.json(“https://kgsearch.googleapis.com/v1/entities:search?query=” + name + “&key=” + key + “&limit=1&indent=True”) yield value

// we save response to Person node

unwind value.itemListElement as list with n,list where list.result.detailedDescription is not null set n += list.result.detailedDescription’, {phase:’after’});

Let me also give you the oneliner for easy testing. Just replace the key variable with you google key and run the query.

CALL apoc.trigger.add(‘spatial’, ‘UNWIND {createdNodes} AS n WITH n where labels(n) = “Person” with n,apoc.text.urlencode(n.name) as name CALL apoc.load.json(“https://kgsearch.googleapis.com/v1/entities:search?query=” + name + “&key= ” + key + “&limit=1&indent=True”) yield value unwind value.itemListElement as list with n,list where list.result.detailedDescription is not null set n += list.result.detailedDescription’, {phase:’after’});

So now that we have the trigger all set up we can run and that click on the query and apply it.

:play movies

Lets now check results and see how knowledge graph has enriched our data.

Screen Shot 2017-03-07 at 23.24.39

If we check our actors, we can see that now we have some description and wikipedia links, that is not there in the sample, but was enriched by apoc.trigger script for calling knowledge graph API.

Thanks for reading, please share with friends 🙂

Advertisements

5 thoughts on “Neo4j APOC triggers and web APIs

  1. Hello, how to handle case insensitive query in Neo4j and Web API. For example, if Keanu Reeves was written in lower case / upper case. I would really appreciate if you could answer.

    Like

  2. Thanks, this is awesome 🙂
    Q: Where did you find the values for `{phase: ‘after’}` and are there other events that you can hook into? Same question for the parameter `{createdNodes}`, I can’t find anything about this in the official docs.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s