Not returning NULL when no records are found while using apoc.do.when

Hi neo4j community,
I don't quite understand why my query doesn't return NULL when no records are found. It just returns (no changes, no records) when i search for something that is not there.

CALL db.index.fulltext.queryNodes('test', 'car_type:test1')
YIELD node
with coalesce(collect(node.car_type)) as test, node
CALL apoc.do.when(
	size(test) = 0,
    'RETURN NULL',
    'MATCH (c:Car:Vehicle)-[:RACE]->(:Formula1)
    WHERE c.car_type = node.car_type
    RETURN DISTINCT c',
    {test:test,node:node}) YIELD value
RETURN value

Thanks in advance

Hello @tarendran.vivekanand :slight_smile:

Can you check what is returned by:

CALL db.index.fulltext.queryNodes('test', 'car_type:test1')
YIELD node
RETURN coalesce(collect(node.car_type)) AS test, node

Regards,
Cobra

Hello @cobra :slight_smile:
I get (no changes, no results)

That's why I think, the next part of your request is not working, you can't get the size of something that doesn't exist :slight_smile:

To be able to work, it should return at least an empty collection [] and the node :slight_smile:

1 Like

Is there a work around this?

You don't need to specify the property since you defined the index on it. So try:

CALL db.index.fulltext.queryNodes('test', 'test1')
YIELD node
RETURN coalesce(collect(node.car_type)) AS test, node

I am doing that cause I have index other properties. Thus, when I search I want to specify the property I want to search.

Oh I see, so there is a syntax mistake :slight_smile:

It should be: CALL db.index.fulltext.queryNodes('test', 'car_type:"test1"') :slight_smile:

If I do that then I won't be able to do any fuzzy search :frowning:

Why? You can use a parameter: CALL db.index.fulltext.queryNodes('test', 'car_type:$arg')

Same thing, it only works if I DON'T do a fuzzy search :frowning:

Can you give me several examples about what you want to search? :slight_smile:

Nodes Created:

CREATE (c:Car:Vehicle {car_type:'Sedan', car_model:'Toyota', car_description:'Used in last 5 years'})
CREATE (c:Car:Vehicle {car_type:'Sedan', car_model:'Toyota', car_description:'Unused in last 3 months'})
CREATE (c:Car:Vehicle {car_type:'Sedan', car_model:'Honda', car_description:'Used in last 2 years'})
MERGE (c)-[:Drives]->(:Driver)

In this example when I search for car_model: *toy* AND car_description:*use* I want the (1st & 2nd) results returned and its attached nodes and if I search for something that isnt there I want to return NULL.

I don't understand why the last request I sent you could not work???

This is how i added the parameter :param filter: 'car_type:*test*'

CALL db.index.fulltext.queryNodes('test', $filter)
YIELD node
WITH coalesce(collect(node.car_type)) AS test, node
CALL apoc.do.when(
	size(test) = 0,
    'RETURN NULL',
    'MATCH (c:Car:Vehicle)-[:RACE]->(:Formula1)
    WHERE c.car_type = node.car_type
    RETURN DISTINCT c',
    {test:test,node:node}) YIELD value
RETURN value

When i do this I get no changes no records when i search for something that doesnt exists

Use: :param arg: 'car_type:"*test*"'

I get no changes, no results

And: :param arg: 'car_type:"*Sedan*"'

Same thing no changes, no results. Also its my bad the query below still gets no changes no records when I search for something that isnt there

CALL db.index.fulltext.queryNodes('test', $filter)
YIELD node
RETURN coalesce(collect(node.car_type)) AS test, node

Can you try:

CALL db.index.fulltext.queryNodes('test', 'car_type:"*Sedan*"')
YIELD node
RETURN coalesce(collect(node.car_type)) AS test, node