Search code examples
postgresqlcypherapache-age

How to MATCH within a list from vertices using Cypher queries?


I was attempting to match and return all vertices that contain a certain tag from within a list.

SELECT * FROM cypher('muse', $$
CREATE(r: jazz {
name: 'Kind of Blue',
artist: 'Miles Davis',
date: '17 August 1959',
sub_genre: ['Modal Jazz', 'Cool Jazz'],
tags: ['instrumental', 'mellow', 'nocturnal', 'soothing', 'improvisation', 'progressive', 'calm'],
rating: 4.31
}) RETURN r
$$) as (jazz_record agtype);
SELECT * from cypher('muse', $$
MATCH (record)
WHERE record.tags = 'mellow'
RETURN record
$$) as (record agtype);

as per the official AGE documentation, there is only mention of using an index or a range. How would I match all records that contain the tag 'mellow' within a list?


Solution

  • You can use the following query:

    SELECT * from cypher('muse', $$
    MATCH (record)
    WHERE  'mellow' IN record.tags
    RETURN record
    $$) as (record agtype);
    

    Here the IN clause checks if 'mellow' exists in the tags list. Moreover, in the sample query that you have provided, use 'tags' property instead of 'tag'