-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstorage_merge.go
More file actions
46 lines (39 loc) · 1.13 KB
/
storage_merge.go
File metadata and controls
46 lines (39 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package comet
import "sort"
// mergeResults merges search results from multiple sources.
// Keeps the highest score for each document ID (deduplication).
//
// Parameters:
// - results: Search results from multiple indexes
//
// Returns:
// - []HybridSearchResult: Merged and deduplicated results
func mergeResults(results []HybridSearchResult) []HybridSearchResult {
if len(results) == 0 {
return nil
}
// Use map to deduplicate and keep highest score per doc
scoreMap := make(map[uint32]float64)
for _, result := range results {
existingScore, exists := scoreMap[result.ID]
if !exists || result.Score > existingScore {
scoreMap[result.ID] = result.Score
}
}
// Convert map to slice
merged := make([]HybridSearchResult, 0, len(scoreMap))
for id, score := range scoreMap {
merged = append(merged, HybridSearchResult{
ID: id,
Score: score,
})
}
return merged
}
// sortResultsByScore sorts results by score in descending order.
// Modifies the input slice in place.
func sortResultsByScore(results []HybridSearchResult) {
sort.Slice(results, func(i, j int) bool {
return results[i].Score > results[j].Score
})
}