cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

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

1 ACCEPTED SOLUTION

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

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

View solution in original post

25 REPLIES 25

Hello @tarendran.vivekanand

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
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

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

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

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

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

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

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

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

No changes no records. I believe the double quotes make it an exact match

Ah, so, did you try to test without?

yes it works when I search for something that exists but won't work when I search for something that doesn't exists

It makes sense no?

Try:

RETURN coalesce(collect(node.car_type), []) AS test, node

No still doesn't work. I get why it doesn't work. I was hoping to get more than just empty square brackets in the response so that I know the reason is that there was actually no results from what I am searching.
I guess there is really no work around this.

If the thing you are searching doesn't not exist, the YIELD node will return nothing, that's why you must ass something at the end of your list in coalesce to make sure to return something