is it possible to search by prefix only?
I need to search exact matches that start with a code.
example search text :
13910
and expected results:
- 13910-1
- 13910-2
Currently it gives the first results plus a bunch of other results with fuzzy search.
In this case i would like to do a prefix search only.
I was thinking about using filter
but not sure how to achieve startsWith with filter14 Replies
the convex bot suggested to use the filter with
q.gte("fieldname", "13910").lt("fieldname", "13910-10")
Exactly what I was going to suggest, you’ll just want to tweak the specific strings a bit (assuming “-10” may not be the actual maximum value you want to use)
You’ll probably want to replace that second value with “13911”
ok so i ended up with using search + filter
Note that, given code 13910, 13910-10 and above would be excluded with that filter
oh good to know! i'll update that, thanks
Cool solution! I think a standard index would probably be a little faster. A regular index would allow more than 1024 results, and would put the codes in lexicographic order instead of relevance order. Maybe we should write up a pattern for using a regular index to search for a string prefix
i would like to take a look at that approach
It's basically what the convex bot said
index("referenceCode", ["referenceCode"])
in the schema
await db.query("quotes").withIndex("referenceCode", q=>q.gte("referenceCode", prefix).lte("referenceCode", prefix + "~")
Note "~" is pretty close to the end of the ascii tablethanks, I'll try that too, good idea
I didnt know it's possible to use gte in index
btw I noticed I could not chain the
.gte
and .lte
. I had to wrap them in an .and
In a .filter you can't chain .gte with .lte, but in a .withIndex you should be able to. In a .withIndex there is no .and
Totally missed that a filter was being used, should definitely be an index
good to know
so with this approach I don't need to additionally do
withSearch
?Right, just a regular index.
ok perfect