#community-help

Resolving Issues with Infix and Prefix in Query Searches

TLDR Daren struggled with searches missing values in production. Jason and Kishore Nallan offered insights and created new features to help solve the problem, which was then tested and deployed by Daren.

Powered by Struct AI

2

38
3mo
Solved
Join the chat
Jul 02, 2023 (3 months ago)
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
10:32 PM
Please help were blocked from production because a value like 123456 won’t be hit if we query 0123456
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:55 PM
By default, Typesense only returns matches where the search term occurs in the beginning of strings (this is called a prefix search)

If you want to search in the middle of strings, it’s called an infix search and you can enable it by setting infix:true in the field definition in the collection schema, and then set infix=true as a search parameter
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
11:07 PM
All infix can be prefix?
11:07
Daren
11:07 PM
But prefix can’t be infix
11:07
Daren
11:07 PM
Just making sure
11:11
Daren
11:11 PM
Hmm it’s not working
11:13
Daren
11:13 PM
Infix parameter for seaching is always or fallback I tried both and it doesn’t work. I have infix on my schema by patching via curl
11:27
Daren
11:27 PM
Sorry if my question was vague but the bad query was the 0, when trying to match 123456
11:28
Daren
11:28 PM
It matches for 012345670
11:28
Daren
11:28 PM
But not for 01234567
11:30
Daren
11:30 PM
It matches 0123456x
Except when x= actual value
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
11:34 PM
Just to make sure we’re on the same page, could you update this code snippet, with your schema settings, add a few sample docs and update the search query to replicate the issue?

https://gist.github.com/jasonbosco/7c3432713216c378472f13e72246f46b
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
11:38 PM
Ok I commented,
Jul 03, 2023 (3 months ago)
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
12:19 AM
Are u able to reproduce ?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:22 AM
Hi Daren, we might not always be able to support typos at the start of the query because that tend to involve almost all the words in your dataset. Infix index won't help here if you are adding a letter at the start.
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
12:39 AM
hmmm
12:42
Daren
12:42 AM
Any workarounds? Or i have to switch to Algolia :/
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
12:44 AM
I'll have to get back to you. Possible to give us a day or so?
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
12:44 AM
Yeah
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
08:26 PM
Copy-pasting the snippet from the GitHub comment here:

export TYPESENSE_API_KEY=xyz

curl "" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"


curl "" \
       -X POST \
       -H "Content-Type: application/json" \
       -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
       -d '{
         "name": "numbers",
         "fields": [
           {"name": "handle", "type": "string", infix:true , index:true},
  
         ],
         "default_sorting_field": ""
       }'
       
curl "" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -H "Content-Type: text/plain" \
        -X POST \
        -d '{"id": "1","handle": "23456789"}
            {"id": "2","handle": "9172837237"}'
            
curl "" \
        -X POST \
        -H "Content-Type: application/json" \
        -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
        -d '{
          "searches": [
            {
              "collection": "numbers",
              "q": "123456789",
              "query_by": "handle"
            }
          ]
        }'
Jul 04, 2023 (3 months ago)
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:26 AM
I took a look into this and we can certainly support this. I'm going to try a few approaches now and figure out the optimized way to achieve this. Will keep you posted.

1

Jul 05, 2023 (3 months ago)
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
07:58 PM
How’s it going? U don’t have to go crazy on it, algolia only allows 1-2 typos on prefix
Jul 06, 2023 (3 months ago)
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
01:33 AM
Almost there. Writing tests, will clean up, merge and share a build in a day or so.
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
02:20 AM
Sounds good, I will deploy tomorrow night then :)
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:20 AM
Hi Daren, I'm done with the changes and I have a build ready for you. Since we are in code freeze for v0.25 release and 0.26 has other unrelated changes, I created a build just with this change on the 0.25 RC branch that we have. Let me know how you deploy Typesense and I can share the build with you.
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
05:31 PM
I’m running it locally on home brew, for production I can see version 0.25.0rc43 in the cloud console. I’d like to test it locally
05:39
Daren
05:39 PM
brew install typesense/tap/typesense-server@
0.25.0.rc43

Or something like that
10:33
Daren
10:33 PM
Hi
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:36 PM
We typically don’t publish RC builds for mac…
10:36
Jason
10:36 PM
Would you be able to test via Docker?
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
10:38 PM
Sure
10:40
Daren
10:40 PM
Btw I’m using type sense cloud on production rc43 is the correct one right
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:41 PM
This particular feature is on 0.25.0.rct46 - this is a build that we would have to upgrade you to from our side. Would you like us to do that?
Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
10:42 PM
I’d like to test locally before I do anything
10:42
Daren
10:42 PM
Where to get the docker image
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
10:43 PM
The docker tag is typesense/typesense:0.25.0.rct46

1

Daren
Photo of md5-a2785b9d22ba23f3627d4bd877e95e7c
Daren
11:01 PM
Working on my end. Will let you know when I’m deployed
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
11:04 PM
Great! Let me know when you’d like to upgrade your cloud cluster to this build.