wiki:FAQ/ExtraWeight

How can I apply extra weight to terms from particular fields?

You can either do this in a static way at index time by increasing the wdf of terms, or dynamically at search time using OP_SCALE_WEIGHT.

Doing it at index time is the approach to use when generating terms with the same prefix (or all unprefixed) from different sources - for example, you might rate terms in the first paragraph as more important, as in many situations they summarise a document. Sometimes this is a formal feature of the document's structure, like an abstract for an academic paper.

If you want to adjust weights between different term prefixes, doing it at search time is more flexible. The weighting factors don't need to be integers, and there's no performance overhead compared to the index time approach. OP_SCALE_WEIGHT was added in Xapian 1.0.4, so it is reasonable to assume it's present.

Example: Applying extra weight to titles, using OP_SCALE_WEIGHT:

    const double FACTOR = 2.5;
    Xapian::Query q_title("Sxapian");
    Xapian::Query q_body("weight");
    Xapian::Query q(OP_AND, Xapian::Query(OP_SCALE_WEIGHT, q_title, FACTOR), q_body);

Example: Applying extra weight to terms from titles at indexing time using wdfinc:

    const int FACTOR = 3;
    Xapian::Document doc;
    // "xapian" is a term from the title.
    doc.add_posting("xapian", 1, FACTOR);
    // "weight" is a term from the body.
    doc.add_posting("weight", 100);

FAQ Index

Last modified 11 years ago Last modified on 01/07/14 06:24:06
Note: See TracWiki for help on using the wiki.