redis-om-node
redis-om-node copied to clipboard
Redis search: syntax error when query is `""`
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.
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!
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.
yep
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.
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();
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])