How do start-with and end-with queries in Redis search

195 views Asked by At

Is it possible to find documents which starts or ends with a specific expression?

For example if we search with this command:

FT.SEARCH "our.domain.DocumentIdx" "bar*"

It will returns documents such as : "foo bar", "foo bar baz", "bar baz".

But I couldn't narrow down my search enough to get only "bar baz".

1

There are 1 answers

0
Guy Royse On

Full-text search doesn't work that way in Redis. You can only search for terms within a document. Wildcards to those terms still search for occurrences of that term, just ones that match the wildcards.

So, for example, if you are searching for bar*, this will match terms such as bar, barbarian, and barrister. It will match them anywhere they occur in the document.

You can use TAGs instead of TEXT in Redis if you want to search for exact values at exact places in a string. This would work for your simple example above. But, keep in mind that it will parse on commas and treat them as separate values in that TAG.

So, for example, if you had the string bar baz qux and searched on bar* it would work just fine and a search on qux* would return nothing. What you'd expect. But if you had the string bar,baz,qux it would search each TAG separately and a search on qux* would match and return.

When you create your index for a TAG, you can override the character used to parse the TAG elements. I usually use | instead.

Also, you can index the same field twice. So you could index your field as both a TEXT field and a TAG field. This allows you to search on it in more ways, although your index will, of course, be larger.

All this said, indexing large chunks of human-readable text as TAG will probably result in large indices. Might be a problem. Might not. Just depends on scale and need. So, go into that with you eyes open.