2
votes

I have one node in neo4j whose structure looks like:

{
            "nodeId": 32,
            "id": "0290cf88-3345-4c30-8e5f-7ce0cb3f0b6b",
            "type": "User",
            "name": "Mahendra",
            "index": 0,
            "data": "This is sample user",
            "description": null,
            "contentBlocks": [],
            "icon": null,
            "createdOn": null,
            "modifiedOn": null,
            "properties": {
                "displayName": "Mahendra",
                "lastName": "Kawde"
            },
            "tags": [
                "tag1",
                "tag2"
            ],
            "categories": null
        }

Now I want to get all nodes by passing tags as a parameter to my cypher query. I am using below query:

MATCH (node) WHERE node.tags = ['tag1','tag2'] RETURN node

This returns me all nodes with given tags. But if I use below query

MATCH (node) WHERE node.tags = ['tag1'] RETURN node

it does not return me any node.

Also if I change the order lets say ['tag2','tag1'] it does not returns me any node.

Below is my controller method:

@RequestMapping(value = "/getByTag",method = RequestMethod.GET, consumes=MediaType.APPLICATION_JSON, produces=MediaType.APPLICATION_JSON)    
    public Result<Node> getByTag(@RequestParam(value="tags") List<String> tags) throws EntityNotFoundException {
        return nodeService.getByTag(tags);
    }

Service method:

public Result<Node> getByTag(List<String> tags) {

        HashMap params = new HashMap();
        params.put("type",tags);

        String query = "MATCH (node) WHERE node.tags = "+ tags +" RETURN node";

        Result<Node> nodes = neo4jTemplate.query(query, params).to(Node.class);
        return nodes;   
    }

Can you give me a way to use collection, more precisely List<String> in cypher query ?

Please help

1

1 Answers

2
votes

How about

MATCH (node) 
WHERE ALL (x IN ['tag1','tag2'] WHERE x in node.tags)
RETURN node

See http://neo4j.com/docs/2.2.2/query-predicates.html