does Typesense have some built-in default "sort by...
# community-help
e
does Typesense have some built-in default "sort by updated at"? once I update a document, it goes "to the top" in the search results (Instant Search) .. I haven't really investigated, thought I'd ask here first.
k
No we don't have a last updated field. You will have to an explicit field and update it.
e
I'm saying that I'm seeing this behaviour already and I wonder why.
k
The default sorting order is based on internal document ID which gets auto incremented on new records. But it won't be affected by updates.
e
It's a Laravel app and I'm using the official Laravel package for typesense. Maybe that has some default sorting added, that's using Laravels "updated_at" field... I'm not sure. I'll investigate further.
sorry, no, I'm seeing this using InstantSearch, so it has nothing to do with Laravel in this case.
Maybe this can provide some clue for what is going on.
@Kishore Nallan
k
Yes, without a
sort_by
the records are sorted by the internal ID. Which means records that are created last will appear first since they have a greater ID. However, this will certainly not be the case for a record updated. One experiment you can do is this: can you update the record with ID 75124 (appearing first in the screenshot) and see if the records are still sorted in the same order?
Btw, the multi_search endpoint sends multiple search requests in the same API call. The screenshot shows only the first request (index
0
).
e
@Kishore Nallan please take a look at the screenshot, you can see the autoincremented IDs and created_at and updated_at timestamps. As soon as I update a document it goes to the top of the result list. You can see the updated_at timestamps in the screenshot. There is only one search request to the multi_search endpoint (I guess it's InstantSearch using multi_search as the endpoint).
k
@Jason Bosco any thoughts?
j
Could you share the collection schema you’re using?
e
@Jason Bosco Laravel is only applicable when inserting/updating documents. Fetching the data is done with InstantSearch, so it's not going through Laravel, but directly to Typesense.
message has been deleted
it happens on both my apps collections. "Products" and "Store Products"
j
@em1nos Could you do the following with curl, to isolate what might be going on: 1. Do a multi-search query (you could just copy-as-curl the request made by your app) and run it in your terminal 2. Now using curl, update a record 3. Run the same multi-search query in 1) again via curl and see if the order has now changed
e
ok, let me try that
@Jason Bosco ok, so following your instructions, I do not have this issue. So is the Laravel package doing something special when updating the document, that could cause this issue?
j
Ah I think this might be it: The package deletes the previous record and then adds it as a new record: https://github.com/typesense/laravel-scout-typesense-engine/blob/55efffa62e063f5db092917f124c941a88943452/src/Engines/TypesenseSearchEngine.php#L43-L53
By default when a sort_by is not mentioned it's sorted by insertion order (not ID)
So the most recently inserted record will be returned first
e
oh .... I have a feeling this Laravel package is "jank" in more than a few places ...
j
I think this portion might have been written before we supported upserts.
e
ok, so I could just add whatever default sort_by to mitigate this?
j
Yeah that, or a PR to use upsert instead of delete+import in the code block above 🙂 🙏
e
I would love to, but it's too advanced for me .. I can barely understand what's going on in the current code
j
Could you open a github issue about this? May be @Abdullah Al-Faqeir or @hi019 can take a look
💯 1