Hyperspace Docs
Hyperspace Homepage
  • Getting started
    • Overview
      • Hyperspace Advantages
      • Hyperspace Search
    • Quick Start
  • flows
    • Setting Up
      • Installing the Hyperspace API Client
      • Connecting to the Hyperspace Server
      • Creating a Database Schema Configuration File
        • Vector Similarity Metrics
        • Index Type Methods
      • Creating a Collection
      • Uploading Data to a Collection
      • Building and Running Queries
        • Building a Lexical Search Query
        • Building a Vector Search Query
        • Building a Hybrid Search Query
      • Retrieving Results
    • Data Collections
      • Uploading Data
      • Accessing Data
      • Supported Data Types
    • Queries
      • DSL Query interface
        • Aggregations
        • Bool Query
        • Candidate Generation and Metadata Filtering
        • Scoring and Ranking
  • Reference
    • Hyperspace Query Flow
    • Features and Benefits
    • Search Processing Unit (SPU)
    • Hyperspace Document Prototype
  • API Documentation
    • Hyperspace Client
      • add_batch
      • add_document
      • async_req
      • clear_collection
      • collections_info
      • commit
      • create_collection
      • delete_collection
      • delete_by_query
      • dsl_search
      • get_schema
      • get_document
      • reset_password
      • search
      • update_by_query
      • update_document
    • DSL Query Framework
      • Aggregations
        • Cardinality Aggregation
        • Date Histogram
        • Metric Aggregations
        • Terms Aggregation
      • Bool Queries
        • Free Text Search
        • 'match' Clause
        • 'filter' Clause
        • 'must' Clause
        • 'must_not' Clause
        • 'should' Clause
        • 'should_not' Clause
      • Candidate Generation and Metadata Filtering
        • Geo Coordinates Match
        • Range Match
        • Term Match
      • Scoring and Ranking
        • Boost
        • 'dis_max'
        • Function Score
        • Rarity Score (TF-IDF)
  • Releases
    • 2024 Releases
Powered by GitBook
On this page
  • Function Score
  • Combining Functions:
  • Boost Mode:
  • Score Mode:
  1. API Documentation
  2. DSL Query Framework
  3. Scoring and Ranking

Function Score

Function Score

The function_score query allows you to modify the relevance score of documents returned by a query. It's particularly useful when you want to introduce custom scoring logic, boost certain documents, or apply mathematical functions to influence the relevance of search results. The function_score query wraps around an existing query (e.g., a match query) and modifies the scores produced by that query. Scoring functions are defined within the functions array. Each function applies a specific logic to modify the relevance score of documents. Common types of functions include:

  • weight: Assigns a static weight to the documents.

  • field_value_factor: Scales scores based on the values of a numeric field.

  • script_score: Allows you to define custom scoring logic using a script.

  • random_score: Introduces randomness to the scores.

Combining Functions:

Multiple scoring functions can be defined within the functions array. The results of these functions are combined to produce the final relevance score. You can control how the scores are combined using parameters like score_mode and boost_mode.

Boost Mode:

The boost_mode parameter specifies how the scores from different functions are combined. Common options include:

  • multiply: Multiply the scores from different functions.

  • sum: Add the scores from different functions.

  • replace: Use the score of the first function that produces a non-zero score.

Score Mode:

The score_mode parameter determines how the scores of individual functions are combined. Common options include:

  • multiply: Multiply the scores.

  • sum: Add the scores.

  • avg: Calculate the average of the scores.

{
  "query": {
    "function_score": {
      "query": {
        "match": { "field": "value" }
      },
      "functions": [
        {
          "weight": 2
        },
        {
          "field_value_factor": {
            "field": "numeric_field",
            "factor": 1.5,
            "modifier": "sqrt"
          }
        }
      ],
      "score_mode": "sum",
      "boost_mode": "multiply"
    }
  }
}

In the above example, the function_score query is applied to a match query. It includes two functions: one that assigns a static weight of 2, and another that scales the scores based on the square root of a numeric field.

  • The first function (weight) multiplies the score by 2.0 (weight * base score).

  • The second function (field_value_factor) uses the square root of the numeric field.

  • The final score for this document would be the sum of these scores (basis_score + first function+ second function)

Previous'dis_max'NextRarity Score (TF-IDF)

Last updated 1 year ago