Search code examples
neo4jcypher

match an array of IDs by property with cypher


I am using Neo4J Desktop to test a cypher query that I am trying to write.

The database contains a number of objects such as:

{
  "identity": 44494216,
  "labels": [
    "WikiEntity"
  ],
  "properties": {
"date_added": "2022-01-11T00:00:00",
"indexed_text": "Johnny Dyer",
"name": "John Dyer (rugby union)",
"wikipediaID": "https://en.wikipedia.org/wiki/John_Dyer_(rugby_union)",
"type": "PERSON",
"parent_bin": "parent_0_230_303",
"uuid": "c378ff81-dea3-48e3-8411-4fb1cd085438"
  }
}

and

{
  "identity": 162983523,
  "labels": [
    "Topic"
  ],
  "properties": {
"topicID": "progressive politics-t",
"topicAlternateID": "XXX8383291769194810424",
"name": "Progressive Politics",
"parent_bin": "parent_0_217_230",
"uuid": "f8358b4f-e656-4290-ab26-2270c1d76088",
"slug": "progressive-politics"
  }
}

I would like to be able to return uuid of objects that match from an array of IDs which I provide, and I would also like to only return matches which have a label of either WikiEntity or Topic or Keyword.

Ideally, I would like to be able to return an object which looks like so:

{
  Keywords: [], // list of matching uuids 
  Topics: [], // list of matching uuids 
  Entities: [] // list of matching uuids 
}

Solution

  • You could do something like this, assuming you provide your uuids in parameter $yourUuids

    MATCH (n)
    WHERE n.uuid IN $yourUuids
          AND (n:Topic OR n:Keyword OR n:WikiEntity)
    
    WITH COLLECT(n) AS ns
    
    RETURN { 
    
    Keywords: [n IN ns WHERE n:Keyword | n.uuid],
    Topics: [n IN ns WHERE n:Topic | n.uuid],
    Entities: [n IN ns WHERE n:WikiEntity | n.uuid]
    
    }