Indexes: Boosting


  • When querying with some filtering conditions, a basic score is calculated by the underlying engine for each document in the results.

  • Providing a boost value to selected fields allows prioritization of the resulting documents.
    The boos value is integrated with the basic score, increasing the document rank.

  • The automatic ordering of results by their score is configurable.

  • Boosting can be achieved in the following ways:

    • At query time:
      By applying a boost factor to searched terms at query time (see Boost search results).

    • Via index definition:
      By applying a boost factor in the index definition, as described in this article.

  • In this page:


Assign a boost factor to an index-field

Applying a boost value to an index-field allows prioritization of matching documents based on an index-field.


The index:

class Orders_ByCountries_BoostByField extends AbstractCsharpIndexCreationTask {
    constructor() {
        super();

        this.map = `from order in docs.Orders
             let company = LoadDocument(order.Company, "Companies")
             select new {
             
                 // Boost index-field 'ShipToCountry':
                 // * Use method 'Boost', pass a numeric value to boost by 
                 // * Documents that match the query criteria for this field will rank higher
                
                 ShipToCountry = order.ShipTo.Country.Boost(10),
                 CompanyCountry = company.Address.Country
             }`;
    }
}
The query:

const orders = await session
    .query({ indexName: "Orders/ByCountries/BoostByField" })
    .whereEquals("ShipToCountry", "Poland")
    .orElse()
    .whereEquals("CompanyCountry", "Portugal")
    .all();

// Because index-field 'ShipToCountry' was boosted (inside the index definition),
// then documents containing 'Poland' in their 'ShipTo.Country' field will get a higher score than
// documents containing a company that is located in 'Portugal'.
from index "Orders/ByCountries/BoostByField"
where ShipToCountry == "poland" or CompanyCountry == "portugal"

Assign a boost factor to the index-entry

Applying a boost value to the whole index-entry allows prioritization of matching documents by content from the document.


The index:

class Orders_ByCountries_BoostByIndexEntry extends AbstractCsharpIndexCreationTask {
    constructor() {
        super();

        this.map = `from order in docs.Orders
             let company = LoadDocument(order.Company, "Companies")
             select new {
                 ShipToCountry = order.ShipTo.Country,
                 CompanyCountry = company.Address.Country
             }
             
             // Boost the whole index-entry:
             // * Use method 'Boost'
             // * Pass a document-field that will set the boost level dynamically per document indexed.  
             // * The boost level will vary from one document to another based on the value of this field.
            
             .Boost(order.Freight)`;
    }
}
The query:

const orders = await session
    .query({ indexName: "Orders/ByCountries/BoostByIndexEntry" })
    .whereEquals("ShipToCountry", "Poland")
    .orElse()
    .whereEquals("CompanyCountry", "Portugal")
    .all();

// The resulting score per matching document is affected by the value of the document-field 'Freight'. 
// Documents with a higher 'Freight' value will rank higher.
from index "Orders/ByCountries/BoostByIndexEntry"
where ShipToCountry == "poland" or CompanyCountry == "portugal"

Automatic score-based ordering

  • By default, whenever boosting is applied, either via dynamic querying or when querying an index that has a boosting factor in its definition, the results will be automatically ordered by the score.

  • This behavior can be modified using the OrderByScoreAutomaticallyWhenBoostingIsInvolved
    configuration key.

  • Refer to the Get resulting score section to learn how to retrieve the calculated score of each result.