Similarityclass and using the index statistics that Lucene saves. Documents can also be assigned a static score to denote their importance in the overall corpus, irrespective of the query that is being executed, e.g their popularity, ratings or PageRank.
Prior to Lucene 4.0, you could assign a static score to a document by calling
document.setBoost. Internally, the boost was applied to every field of the document, by multiplying the field's boost factor with the document's. However, as explained here, this has never worked correctly and depending on the type of query executed, might not affect the document's rank at all.
With the addition of DocValues to Lucene, boosting documents is as easy as adding a
NumericDocValuesFieldand use it in a
CustomScoreQuery, which multiplies the computed score by the value of the 'boost' field. The code example below illustrates how to achieve that:
// add two documents to the index Document doc = new Document(); doc.add(new TextField("f", "test document", Store.NO)); doc.add(new NumericDocValuesField("boost", 1L)); writer.addDocument(doc); doc = new Document(); doc.add(new TextField("f", "test document", Store.NO)); doc.add(new NumericDocValuesField("boost", 2L)); writer.addDocument(doc); // search for 'test' while boosting by field 'boost' Query baseQuery = new TermQuery(new Term("f", "test")); Query boostQuery = new FunctionQuery(new LongFieldSource("boost")); Query q = new CustomScoreQuery(baseQuery, boostQuery); searcher.search(q, 10);
The new Expressions module can also be used for boosting documents by writing a simple formula, as depicted below. While it's more verbose than using
CustomScoreQuery, it makes boosting by computing more complex formulas trivial, e.g.
sqrt(_score) + ln(boost).
Now that Lucene allows updating
NumericDocValuesFieldswithout re-indexing the documents, you can incorporate frequently changing fields (popularity, ratings, price, last-modified time...) in the boosting factor without re-indexing the document every time any one of them changes.