Full text index with relevance ranking, using an Okapi BM25 rank.
"Okapi" (much like "cosine rule" also) is a large family of scoring gimmicks. It's based on probability arguments about how words are distributed in documents, not on an abstract vector space model. A long paper by its principal inventors gives an excellent overview of how it was derived:
A probabilistic model of information retrieval: development and status K. Sparck Jones, S. Walker, S.E. Robertson http://citeseer.nj.nec.com/jones98probabilistic.html
Spellings that ignore relevance information (which we don't have) are of this high-level form:
score(D, Q) = sum(for t in D&Q: TF(D, t) * IDF(Q, t))
D a specific document
Q a specific query
t a term (word, atomic phrase, whatever)
D&Q the terms common to D and Q
IDF(Q, t) a measure of t's importance in the query and in the set of documents as a whole -- a kind of inverse document frequency weight
The IDF(Q, t) here is identical to the one used for our cosine measure. Since queries are expected to be short, it ignores Q entirely:
IDF(Q, t) = log(1.0 + N / f(t))
N the total number of documents f(t) the number of documents in which t appears
Most Okapi literature seems to use log(N/f(t)) instead. We don't, because that becomes 0 for a term that's in every document, and, e.g., if someone is searching for "documentation" on python.org (a term that may well show up on every page, due to the top navigation bar), we still want to find the pages that use the word a lot (which is TF's job to find, not IDF's -- we just want to stop IDF from considering this t to be irrelevant).
The TF(D, t) spellings are more interesting. With lots of variations, the most basic spelling is of the form
f(D, t) TF(D, t) = --------------- f(D, t) + K(D)
f(D, t) the number of times t appears in D K(D) a measure of the length of D, normalized to mean doc length
The functional form f/(f+K) is clever. It's a gross approximation to a mixture of two distinct Poisson distributions, based on the idea that t probably appears in D for one of two reasons:
Note that f/(f+K) is always between 0 and 1. If f is very large compared to K, it approaches 1. If K is very large compared to f, it approaches 0. If t appears in D more or less "for random reasons", f is likely to be small, and so K will dominate unless it's a very small doc, and the ratio will be small. OTOH, if t appears a lot in D, f will dominate unless it's a very large doc, and the ratio will be close to 1.
We use a variation on that simple theme, a simplification of what's called BM25 in the literature (it was the 25th stab at a Best Match function from the Okapi group; "a simplification" means we're setting some of BM25's more esoteric free parameters to 0):
f(D, t) (k1 + 1) TF(D, t) = -------------------- f(D, t) + k1 K(D)
k1 a "tuning factor", typically between 1.0 and 2.0. We use 1.2, the usual default value. This constant adjusts the curve to look more like a theoretical 2-Poisson curve.
Note that as f(D, t) increases, TF(D, t) increases monotonically, approaching an asymptote of k1+1 from below.
K(D) = (1-b) + b * len(D)/E(len(D))
b is another free parameter, discussed below. We use 0.75.
len(D) the length of D in words
E(len(D)) the expected value of len(D) across the whole document set; or, IOW, the average document length
b is a free parameter between 0.0 and 1.0, and adjusts for the expected effect of the "Verbosity Hypothesis". Suppose b is 1, and some word t appears 10 times as often in document d2 than in document d1. If document d2 is also 10 times as long as d1, TF(d1, t) and TF(d2, t) are identical:
f(d2, t) (k1 + 1) TF(d2, t) = --------------------------------- = f(d2, t) + k1 len(d2)/E(len(D))
because the 10's cancel out. This is appropriate if we believe that a word appearing 10x more often in a doc 10x as long is simply due to that the longer doc is more verbose. If we do believe that, the longer doc and the shorter doc are probably equally relevant. OTOH, it could be that the longer doc is talking about t in greater depth too, in which case it's probably more relevant than the shorter doc.
At the other extreme, if we set b to 0, the len(D)/E(len(D)) term vanishes completely, and a doc scores higher for having more occurences of a word regardless of the doc's length.
Reality is between these extremes, and probably varies by document and word too. Reports in the literature suggest that b=0.75 is a good compromise "in general", favoring the "verbosity hypothesis" end of the scale.
f(D, t) (k1 + 1) TF(D, t) = -------------------------------------------- f(D, t) + k1 ((1-b) + b*len(D)/E(len(D)))
with k1=1.2 and b=0.75.
Query Term Weighting --------------------
I'm ignoring the query adjustment part of Okapi BM25 because I expect our queries are very short. Full BM25 takes them into account by adding the following to every score(D, Q); it depends on the lengths of D and Q, but not on the specific words in Q, or even on whether they appear in D(!):
E(len(D)) - len(D) k2 len(Q) ------------------- E(len(D)) + len(D)
Here k2 is another "tuning constant", len(Q) is the number of words in Q, and len(D) & E(len(D)) were defined above. The Okapi group set k2 to 0 in TREC-9, so it apparently doesn't do much good (or may even hurt).
f(Q, t) * (k3 + 1) ------------------ f(Q, t) + k3
where k3 is yet another free parameter, and f(Q,t) is the number of times t appears in Q. Since we're using short "web style" queries, I expect f(Q,t) to always be 1, and then that quotient is
regardless of k3's value. So, in a trivial sense, we are incorporating
this measure (and optimizing it by not bothering to multiply by 1