#community-help

Queryable Int32 Field for Sorting and Query By

TLDR JP wants to make an int32 field queryable and sortable. Jason suggests creating integer and string versions of the field, but acknowledges the need for integer fields in query_by.

Powered by Struct AI
Mar 02, 2023 (9 months ago)
JP
Photo of md5-b57d2fb1d50d1db78560d888cd361faf
JP
08:58 PM
Hi! 👋 We have a int32 field that we want to be queryable (using query_by), but also to sort_by. For it to be queryable, we have to set the field's type to string in the schema, but sorting on it produces the wrong results. Since these values are now strings instead of numbers if we sort them ascending, we get results for 1, 10, 100, 2, 20, etc instead of 1, 2, 3, etc.

Is there a way to do a type cast or transformation on the field when sorting? Something like sort_by: parseInt32(theField):asc
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
09:00 PM
Hey JP! Unfortunately no. So you would have to create an integer version of the field and use that for sorting and creating a string version of the field to use for query_by.

This came up recently in another context, mind copy-pasting your use-case into this GitHub issue: https://github.com/typesense/typesense/issues/917

This gives an even better use-case to support integer fields in query_by
JP
Photo of md5-b57d2fb1d50d1db78560d888cd361faf
JP
09:02 PM
Thanks for the quick response! Definitely, I'll add it to the issue.