Documentation
¶
Overview ¶
Package rerank is the runnable Pattern A reference implementation from doc/search-integration.md. It exposes an HTTP search endpoint that does two-stage retrieval:
- Recall — call OpenSearch (or any candidate-source), get top N by text
- Rerank — fetch counter features from Murmur via GetMany / GetWindowMany, score each candidate, return top K
Counters never go in the OpenSearch index. The text-side index serves only slow-moving fields; popularity is fetched fresh per query and applied as a second-stage rerank in this service.
Pairs with examples/search-projector (Pattern B). Production deployments use both: B for filterable bucket fields ("≥1k likes"), A for fine ranking within a bucket. See doc/search-integration.md.
Counter feature lookup ¶
The rerank stage reads counters from Murmur via the QueryService.GetMany (all-time) and GetWindowMany (windowed-recent) RPCs. Both are batched — one BatchGetItem-shaped fetch covers up to 100 entities, with the singleflight coalescing layer collapsing concurrent identical requests. For N=200 candidates the fetch is two BatchGetItems in parallel, ~10ms p99.
Score function ¶
The default score is a hand-tuned combination of BM25 and log-scaled likes:
score = bm25 * (1 + log10(likes_24h + 1))
Real production systems use learned models (XGBoost / cross-encoder transformers); Murmur is invariant under that choice — it just provides the counter-feature inputs. Override ScoreFn to plug a different model in.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DefaultScore ¶
DefaultScore is the default ScoreFn — a multiplicative combination of BM25 and log10-scaled likes. Typical product-search shape; tunable per-app.
Types ¶
type Candidate ¶
type Candidate struct {
ID string // document ID
BM25 float64 // text-relevance score from OpenSearch
}
Candidate is the input to the rerank stage — what the recall stage returned, before counter features are applied.
type Config ¶
type Config struct {
// Recall is the candidate-generation stage. Required.
Recall Recaller
// LikesAllTime is the Murmur QueryService client for the all-time
// likes counter. Optional — set nil to skip this feature.
LikesAllTime murmurv1connect.QueryServiceClient
// LikesWindowed is the Murmur QueryService client for the windowed
// likes counter. Same client type, but typically points at a
// DIFFERENT pipeline (a windowed counter over the last 24h, not the
// all-time one). Set nil to skip the windowed feature.
LikesWindowed murmurv1connect.QueryServiceClient
// WindowDuration is the duration to query against LikesWindowed.
// Default: 24 hours.
WindowDuration time.Duration
// TopN is the recall set size. Default: 200.
TopN int
// TopK is the final returned set size. Default: 20.
TopK int
// ScoreFn is the final-stage scoring function. Default: DefaultScore.
ScoreFn ScoreFn
}
Config configures the rerank service. ScoreFn defaults to DefaultScore; set it explicitly for ML rerankers.
type Recaller ¶
type Recaller interface {
Recall(ctx context.Context, query string, topN int) ([]Candidate, error)
}
Recaller is the abstraction over the candidate-generation stage. Production: the OpenSearch BM25 + filter query path. Tests: a fake that returns a fixed list. The interface is intentionally minimal — the rerank service doesn't need to know anything about the recall implementation beyond "give me up to N candidates for this query."
type ScoreFn ¶
ScoreFn computes the final ranking score from a candidate's BM25 and counter features. Default — see DefaultScore — uses log10-scaled likes; override to plug in a learned model.
type ScoredCandidate ¶
type ScoredCandidate struct {
ID string
Score float64
// Features carried through for inspection.
BM25 float64
LikesAll int64
Likes24h int64
}
ScoredCandidate is the output of the rerank stage — the candidate plus its final blended score and the raw feature values that produced it (handy for explainability and A/B logging).
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the rerank service — wraps a Recaller plus Murmur clients behind a single Search method.
func (*Service) Search ¶
Search runs the two-stage retrieval and returns the top-K reranked candidates. Failures in the counter-fetch stage degrade gracefully — missing features are treated as zero, the recall result still scores (BM25 alone), and the response carries on. The user sees a slightly less relevant ranking, not an error.
type Stats ¶
type Stats struct {
Searches atomic.Int64
CandidatesFetched atomic.Int64
LikesAllHits atomic.Int64
LikesWindowHits atomic.Int64
LikesMisses atomic.Int64
}
Stats reports per-search latency and counter coverage. Useful for dashboards and for verifying the rerank stage is doing what you think.