πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Issue with filtering documents using `match` query in Elasticsearch

1 replies
Last updated: Feb 19, 2023
Hi, I ran into an issue trying to filter documents with a
match
query. I'm trying to filter on a string field containing version number-like data - 1.2.3. So I though I could get all the "version 1.x"-related documents with
version match '1.*'
but it seems periods before the wildcard -
.*
- for some reason does not work.
version match '1.2.3'
returns documents, but
version match '1.*'
does not.. πŸ˜• I don't see the documentation mentioning
.
being a special character in this context. Is this a bug?
Feb 19, 2023, 3:35 PM
The
match
operator is made for human text, and can't be used this way. A string like
1.2.3
is turned into the tokens
[1, 2, 3]
, and then matched against the text, which is also tokenized this way. So
"1.2.3" match "3.2.1"
returns true.
If you only want to search by prefix, I recommend using
string::startsWith()
, e.g.:
string::startsWith(version, "1.")
This will match
1.2
,
1.2.3
, etc.
Feb 19, 2023, 6:55 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?