#community-help

Understanding Typesense Data Sorting Behavior

TLDR Imtiaz had a query about data sorting order in Typesense. Kishore Nallan clarified that data with the same text match score are sorted based on their internal sequential ID, descending.

Powered by Struct AI
6
24mo
Solved
Join the chat
Jan 20, 2022 (24 months ago)
Imtiaz
Photo of md5-5c86b23fcf7d52313d7a346d8cc3e86b
Imtiaz
10:46 AM
Hi. I have a question regarding sorting. I have referenced https://typesense.org/docs/guide/ranking-and-relevance.html#text-match-score but would like to clarify some behaviour I observe.

A subset of my data appears as follows (NB - when seeding these are added to the index in the order listed below):

icd10,description
E78.1,Pure hyperglyceridaemia
E78.2,Mixed hyperlipidaemia
E78.3,Hyperchylomicronaemia

and my index is as follows:

{
    'name': 'diagnosis',
    'fields': [
        {'name': 'id', 'type': 'string'},  # maps to icd10
        {'name': 'code', 'type': 'string'},  # maps to icd10
        {'name': 'description', 'type': 'string'},  # maps to description
    ]
}

Running a query E78 I get the results in reverse order (E78.3, followed by E78.2 and E78.1)

If I seed the data in reverse order than I get the results in the correct order.

I would just like to understand this behaviour better, and how order of insertion affects sorting (and if its fine to rely on this for my use case since the data wont change).
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:48 AM
👋 When you query E78 all matching documents have the same text match score, so to break the tie, we sort on their internal document ID, which is an increasing sequential ID.
Imtiaz
Photo of md5-5c86b23fcf7d52313d7a346d8cc3e86b
Imtiaz
10:50 AM
Is this sort by internal sequential ID descending or ascending?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:50 AM
Descending, you can rely on this behavior.
Imtiaz
Photo of md5-5c86b23fcf7d52313d7a346d8cc3e86b
Imtiaz
10:50 AM
Awesome - thanks!
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:50 AM
Welcome.