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?
4
Answers
You can use the following query:
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’
As an alternative to Zainab’s answer, you could try the command
ANY
:You can also try this:
You can return all vertices that contain a certain tag, within their tags list using the
IN
operator OR you can use thecustom
function in your Cypher query.Here’s an example query using
IN
Operator:Explanation:
In this query,
record:jazz
specifies that you are matching nodes labeled ‘jazz’. The WHERE clause filters the results to only include records where ‘mellow’ is present in therecord.tags
list. TheIN
operator checks whether ‘mellow’ is present in the list, and theANY
function is implicitly used to iterate over the list. Finally, theRETURN
clause returns the matching records.Here’s an example query using
custom
function:Firstly, create a function in
pg_catalog
:Then, you can call this function to achieve your desired results.
Explanation:
In this query,
custom
function is used to iterates over each tag inrecord.tags
and checks whether it matches the string ‘mellow’. If at least one tag matches, the function will return true andWHERE
clause will becometrue
.