redis-om-node icon indicating copy to clipboard operation
redis-om-node copied to clipboard

Redis search: syntax error when query is `""`

Open Dhravya opened this issue 2 years ago • 6 comments

I have a search query that looks something like this:

  const image = await repository
    .search()
    .where("slug")
    .eq(slug)
    .and("domain")
    .eq(domain || "")
    .first();

However, upon running this, I receive the following error:

RedisError: The query to RediSearch had a syntax error: "Syntax error at offset 36 near domain".
This is often the result of using a stop word in the query. Either change the query to not use a stop word or change the stop words in the schema definition.

Most likely, the redis command generated is invalid because there's no value over there.

This is far from ideal, because in some cases, I would want to fallback to an empty string in the search query.

Dhravya avatar Aug 30 '22 13:08 Dhravya

Can you share your Schema here as well? I'm assuming that domain is a string given the error but would like to be sure. Thanks!

guyroyse avatar Aug 30 '22 13:08 guyroyse

Also, 99.9% certain this is a bug. Just need to add code to check when building the query for the special case of an empty string.

guyroyse avatar Aug 30 '22 13:08 guyroyse

yep

Dhravya avatar Sep 08 '22 15:09 Dhravya

This may not be immediately doable: https://forum.redis.com/t/how-to-query-for-absence-empty-field/147

it might be a case of storing a special value to signify "not set", such as an underscore (something that wouldn't normally be matched on) so it can then be searched on.

CaptainCodeman avatar Sep 27 '22 19:09 CaptainCodeman

What would we want to expected behavior to be here? Are you wanting to search where domain is an empty string, or do you just not want to include domain in the search if it is empty. I could certainly implement the latter, but, as @CaptainCodeman mentions, the former isn't terribly doable.

You could work around this as well, if you want the latter scenario, like this:

const search = repository.search();

search.where("slug").eq(slug);
if (domain) search.and("domain").eq(domain);

const image = search.return.first();

guyroyse avatar Jul 24 '23 20:07 guyroyse

it happens to me something very similar but when I search strings containing "@" I get this error: Syntax error at offset 27 near com ie: Page @email:([email protected])

mendiu avatar Dec 19 '23 21:12 mendiu