#community-help

Sorting Results in Typesense and Handling Errors

TLDR Ramees asks about sorting results by distance in Typesense, entering a list of strings as a field, and resolving an error with fields format. Kishore Nallan assists with these issues and advises on storing timestamps and proper authentication.

Powered by Struct AI

2

46
28mo
Solved
Join the chat
Jul 21, 2021 (28 months ago)
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
11:29 AM
Hi, Can Typesense sort result by distance?
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:30 AM
๐Ÿ‘‹ For geo search?
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
11:30 AM
yes
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:31 AM
Yes it can do that. Let me find the docs.
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
11:32 AM
query response need to be sorted by distance so the result will show documents near to the user first
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:32 AM
Check the sort_by clause in the geo search example.
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
11:33 AM
thank you
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
11:33 AM
๐Ÿ‘
Jul 22, 2021 (28 months ago)
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
05:47 AM
Hi, sorry for asking too many question here ๐Ÿ™. Please tell me if I can put list of strings as field (not going to index it)
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
05:48 AM
No worries. Yes, you can have a list of strings. If the field name is not mentioned in the schema, it will be stored (and returned in response) but not indexed.
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
05:49 AM
thank you for your fast response.
06:22
Ramees
06:22 AM
Hi, I got error while creating a collection.
06:22
Ramees
06:22 AM
Error: Request failed with HTTP code 400 | Server said: Wrong format for fields. It should be an array of objects containing name, type, optional and facet properties.
06:23
Ramees
06:23 AM
beside this 4 properties I included "index": false also for some field
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:24 AM
Can you post the exact schema sent?
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
06:24 AM
{
"name": "businessId",
"type": "string",
"index": false
},
{
"name": "businessName",
"type": "string",
"index": false
}
06:26
Ramees
06:26 AM
{
"name": "BPS",
"fields": [
{
"name": "id",
"type": "string",
"index": false
},
{
"name": "name",
"type": "string",
},
{
"name": "address",
"type": "string",
"index": false
},
]
}
06:26
Ramees
06:26 AM
it will be something like this...
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
06:30 AM
I see some trailing commas.
06:30
Kishore Nallan
06:30 AM
> "type": "string",
> },
06:31
Kishore Nallan
06:31 AM
Also you should not include the id field in the schema.
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
06:44 AM
thank you

1

06:59
Ramees
06:59 AM
hi do which type should I use to store timestamp so that I can retrieve only data which not expired
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:01 AM
int64
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
07:05 AM
or thank you, I have my DateTime stores as String (2023-01-01T00:00:00.000)
07:05
Ramees
07:05 AM
in firestore
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:06 AM
You will have to convert to timestamp for indexing because typesense does not have a datetime type that can parse that format.
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
07:10 AM
I am using firebase extension. firestore doesn't have timestamp format so I converted the date from timestamp to string (Iso8601String). is there any way. otherwise I should handle it client side.
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:11 AM
You can store as an integer, correct?
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
07:13 AM
you mean, need to convert timestamp to int64 in client side and save it on the firestore as int
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
07:14 AM
Yes, store timestamp in firestore as an integer, which can then be mapped to Typesense without any type change.
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
07:14 AM
oh. thank you.

1

10:00
Ramees
10:00 AM
Hi Kishore, can i query Typesense from POSTMAN, where I will put my search only API Key
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:00 AM
Yes, you can do that.
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
10:01 AM
<http://xxxx-1.a1.typesense.net:443/collections/students/documents/search?q=search-term&amp;query_by=name&amp;auth=MySearchKey|xxxx-1.a1.typesense.net:443/collections/students/documents/search?q=search-term&amp;query_by=name&amp;auth=MySearchKey>
10:01
Ramees
10:01 AM
this is how I structured the url. it is not working
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:02 AM
How are you passing the typesense API key? You have to send it either as a header or as a parameter in the get request.
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
10:02 AM
auth=MySearchKey
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:03 AM
But that's not how Typesense authenticates. Where did you see that example?
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
10:03 AM
I use that format in elastic search
10:04
Ramees
10:04 AM
and I couldn't find in Typesense docs for direct HTTP
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:04 AM
10:05
Kishore Nallan
10:05 AM
You have to send a header:

"X-TYPESENSE-API-KEY": "FOO"
Ramees
Photo of md5-ddfa968eca4266b70a36c83bdcdc7b94
Ramees
10:05 AM
I will do, thank you
Kishore Nallan
Photo of md5-4e872368b2b2668460205b409e95c2ea
Kishore Nallan
10:11 AM
I will also update the Authentication docs page to make this clearer.