(cands []candidate, plan *queryPlan, params scoreParams, topK int)
| 232 | } |
| 233 | |
| 234 | func mergeAndScore(cands []candidate, plan *queryPlan, params scoreParams, topK int) []Result { |
| 235 | if topK <= 0 || len(cands) == 0 { |
| 236 | return nil |
| 237 | } |
| 238 | query := []byte(plan.Normalized) |
| 239 | h := &resultHeap{} |
| 240 | heap.Init(h) |
| 241 | for i := range cands { |
| 242 | c := &cands[i] |
| 243 | s := scorePath([]byte(c.Path), c.BaseOff, c.BaseLen, c.Depth, query, plan.Tokens, params) |
| 244 | if s <= 0 { |
| 245 | continue |
| 246 | } |
| 247 | // DirTokenHit is applied here rather than in scorePath because |
| 248 | // it depends on the query plan's directory tokens, which are |
| 249 | // split from the full query during planning. scorePath operates |
| 250 | // on raw query bytes without knowledge of token boundaries. |
| 251 | if len(plan.DirTokens) > 0 { |
| 252 | segments := extractSegments([]byte(c.Path)) |
| 253 | for _, dt := range plan.DirTokens { |
| 254 | for _, seg := range segments { |
| 255 | if equalFoldASCII(seg, dt) { |
| 256 | s += params.DirTokenHit |
| 257 | break |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | r := Result{Path: c.Path, Score: s, IsDir: c.Flags == uint16(FlagDir)} |
| 263 | if h.Len() < topK { |
| 264 | heap.Push(h, r) |
| 265 | } else if s > (*h)[0].Score { |
| 266 | (*h)[0] = r |
| 267 | heap.Fix(h, 0) |
| 268 | } |
| 269 | } |
| 270 | n := h.Len() |
| 271 | results := make([]Result, n) |
| 272 | for i := n - 1; i >= 0; i-- { |
| 273 | v := heap.Pop(h) |
| 274 | if r, ok := v.(Result); ok { |
| 275 | results[i] = r |
| 276 | } |
| 277 | } |
| 278 | return results |
| 279 | } |
| 280 | |
| 281 | type resultHeap []Result |
| 282 |
no test coverage detected