fix: search fix

This commit is contained in:
Krispire Admin
2026-06-23 14:04:08 +05:30
parent fe0bee4ce7
commit eadef33eac
+22 -9
View File
@@ -203,11 +203,20 @@ func (h *SearchHandler) multiSearch(ctx context.Context, collections []string, q
prioritizeTokenPos := true prioritizeTokenPos := true
prefix, numTypos := fuzzinessKnobs(exact) prefix, numTypos := fuzzinessKnobs(exact)
searches := make([]api.MultiSearchCollectionParameters, 0, len(collections)) searches := make([]api.MultiSearchCollectionParameters, 0, len(collections))
// Merge-paginate: every sub-search fetches the first page*limit hits (page 1,
// a wide window) so the merged top page*limit is fully covered; we slice the
// requested page out after sorting below. Asking each collection for its OWN
// page N instead would miss globally-ranked hits and go blank past page 1
// (e.g. a sparse query whose few hits all sit in each collection's page 1).
window := page * limit
if window > 250 { // Typesense per_page ceiling
window = 250
}
for _, col := range collections { for _, col := range collections {
queryBy := collectionQueryByFields[col] queryBy := collectionQueryByFields[col]
weights := collectionQueryByWeights[col] weights := collectionQueryByWeights[col]
perPage := limit perPage := window
pg := page pg := 1
params := api.MultiSearchCollectionParameters{ params := api.MultiSearchCollectionParameters{
Collection: col, Collection: col,
Q: stringPtr(query), Q: stringPtr(query),
@@ -262,14 +271,18 @@ func (h *SearchHandler) multiSearch(ctx context.Context, collections []string, q
return allItems[i].score > allItems[j].score return allItems[i].score > allItems[j].score
}) })
// Paginate the merged results: since each sub-search already returned `limit` // Slice the requested page out of the merged ranking. Collections hold
// results for the given page, the merged set has at most 9*limit items. // disjoint documents, so totalFound (sum of per-collection founds) is the
// We take the top `limit` after sorting. // exact total and start past the end means the page is genuinely empty.
if len(allItems) > limit { start := (page - 1) * limit
allItems = allItems[:limit] if start >= len(allItems) {
return []searchResultItem{}, totalFound
} }
end := start + limit
return allItems, totalFound if end > len(allItems) {
end = len(allItems)
}
return allItems[start:end], totalFound
} }
// singleSearch searches a single collection. // singleSearch searches a single collection.