> ## Documentation Index
> Fetch the complete documentation index at: https://algolia.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Filter scoring

> Scoring filters by controlling their weight and therefore impact on searches.

export const SearchQuery = () => <Tooltip tip="The text users enter into a search box. In the Search API, this corresponds to the query parameter. A search query is often used with filters, facets, and other parameters, but these aren't part of the query text itself.">
    search query
  </Tooltip>;

export const Records = () => <Tooltip tip="A record is a searchable object in an Algolia index. Each record consists of named attributes." cta="Algolia records" href="/doc/guides/sending-and-managing-data/prepare-your-data#algolia-records">
    records
  </Tooltip>;

export const Facet = () => <Tooltip tip="An attribute in your records that lets users filter or group results (for example, by color, brand, or price)." cta="Faceting" href="/doc/guides/managing-results/refine-results/faceting">
    facet
  </Tooltip>;

As well as refining results, you can use **filter scoring** to rank <Records /> according to how well or poorly they match a set of filters.
Scoring applies numerical settings to filter values, making some filter values more important than others. Records with the highest filter values sit at the top of the results list.

Consider the case of stock portfolios in which companies are scored according to their monetary significance.
You could use filter scoring to rank Google higher than Facebook. For example:

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.SearchSingleIndexAsync<Hit>(
    "INDEX_NAME",
    new SearchParams(
      new SearchParamsObject
      {
        Filters =
          "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
      }
    )
  );
  ```

  ```dart Dart theme={"system"}
  final response = await client.searchSingleIndex(
    indexName: "INDEX_NAME",
    searchParams: SearchParamsObject(
      filters:
          "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
    ),
  );
  ```

  ```go Go theme={"system"}
  response, err := client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(
    "INDEX_NAME").WithSearchParams(search.SearchParamsObjectAsSearchParams(
    search.NewEmptySearchParamsObject().SetFilters("(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)"))))
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  SearchResponse response = client.searchSingleIndex(
    "INDEX_NAME",
    new SearchParamsObject().setFilters("(company:Google<score=3> OR company:Amazon<score=2> OR" + " company:Facebook<score=1>)"),
    Hit.class
  );
  ```

  ```js JavaScript theme={"system"}
  const response = await client.searchSingleIndex({
    indexName: 'indexName',
    searchParams: { filters: '(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)' },
  });
  ```

  ```kotlin Kotlin theme={"system"}
  var response =
    client.searchSingleIndex(
      indexName = "INDEX_NAME",
      searchParams =
        SearchParamsObject(
          filters =
            "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)"
        ),
    )
  ```

  ```php PHP theme={"system"}
  $response = $client->searchSingleIndex(
      'INDEX_NAME',
      ['filters' => '(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)',
      ],
  );
  ```

  ```python Python theme={"system"}
  response = client.search_single_index(
      index_name="INDEX_NAME",
      search_params={
          "filters": "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
      },
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.search_single_index(
    "INDEX_NAME",
    Algolia::Search::SearchParamsObject.new(
      filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)"
    )
  )
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.searchSingleIndex(
      indexName = "INDEX_NAME",
      searchParams = Some(
        SearchParamsObject(
          filters = Some("(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)")
        )
      )
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response: SearchResponse<Hit> = try await client.searchSingleIndex(
      indexName: "INDEX_NAME",
      searchParams: SearchSearchParams
          .searchSearchParamsObject(
              SearchSearchParamsObject(
                  filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)"
              )
          )
  )
  ```
</CodeGroup>

The result is that records with Google stocks sit at the top of the list, higher than Amazon and Facebook.

## How scoring is calculated

Filter scores are integer values from 0 to 65,535.

### Default scoring

In the preceding example, any portfolio containing all three companies (Google, Amazon, and Facebook) would score `3`, as **the overall score is based on the highest score**. In other words, by default, there is no accumulation of individual scores.

### Accumulating scores with `sumOrFiltersScores`

You accumulate scores by setting the [`sumOrFiltersScores`](/doc/api-reference/api-parameters/sumOrFiltersScores) parameter to `true`. In the preceding example, any record with all three companies would have an overall score of 6 (3+2+1).

If `sumOrFiltersScores` is `false`, the default, the system uses the default scoring method: taking the highest score.
Using the preceding example and a <SearchQuery /> filtered on Google and Amazon with `sumOrFiltersScores` = `false` returns a score of 3 (google(3) > amazon(2) > facebook(1)).

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.SearchSingleIndexAsync<Hit>(
    "INDEX_NAME",
    new SearchParams(
      new SearchParamsObject
      {
        Filters =
          "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
        SumOrFiltersScores = false,
      }
    )
  );
  ```

  ```dart Dart theme={"system"}
  final response = await client.searchSingleIndex(
    indexName: "INDEX_NAME",
    searchParams: SearchParamsObject(
      filters:
          "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
      sumOrFiltersScores: false,
    ),
  );
  ```

  ```go Go theme={"system"}
  response, err := client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(
    "INDEX_NAME").WithSearchParams(search.SearchParamsObjectAsSearchParams(
    search.NewEmptySearchParamsObject().
      SetFilters("(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)").
      SetSumOrFiltersScores(false),
  )))
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  SearchResponse response = client.searchSingleIndex(
    "INDEX_NAME",
    new SearchParamsObject()
      .setFilters("(company:Google<score=3> OR company:Amazon<score=2> OR" + " company:Facebook<score=1>)")
      .setSumOrFiltersScores(false),
    Hit.class
  );
  ```

  ```js JavaScript theme={"system"}
  const response = await client.searchSingleIndex({
    indexName: 'indexName',
    searchParams: {
      filters: '(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)',
      sumOrFiltersScores: false,
    },
  });
  ```

  ```kotlin Kotlin theme={"system"}
  var response =
    client.searchSingleIndex(
      indexName = "INDEX_NAME",
      searchParams =
        SearchParamsObject(
          filters =
            "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
          sumOrFiltersScores = false,
        ),
    )
  ```

  ```php PHP theme={"system"}
  $response = $client->searchSingleIndex(
      'INDEX_NAME',
      ['filters' => '(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)',
          'sumOrFiltersScores' => false,
      ],
  );
  ```

  ```python Python theme={"system"}
  response = client.search_single_index(
      index_name="INDEX_NAME",
      search_params={
          "filters": "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
          "sumOrFiltersScores": False,
      },
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.search_single_index(
    "INDEX_NAME",
    Algolia::Search::SearchParamsObject.new(
      filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
      sum_or_filters_scores: false
    )
  )
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.searchSingleIndex(
      indexName = "INDEX_NAME",
      searchParams = Some(
        SearchParamsObject(
          filters = Some("(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)"),
          sumOrFiltersScores = Some(false)
        )
      )
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response: SearchResponse<Hit> = try await client.searchSingleIndex(
      indexName: "INDEX_NAME",
      searchParams: SearchSearchParams.searchSearchParamsObject(SearchSearchParamsObject(
          filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
          sumOrFiltersScores: false
      ))
  )
  ```
</CodeGroup>

The same query with `sumOrFiltersScores` = `true` returns a score of 6 (google (3) + amazon(2) + facebook(1)).

<CodeGroup>
  ```cs C# theme={"system"}
  var response = await client.SearchSingleIndexAsync<Hit>(
    "INDEX_NAME",
    new SearchParams(
      new SearchParamsObject
      {
        Filters =
          "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
        SumOrFiltersScores = true,
      }
    )
  );
  ```

  ```dart Dart theme={"system"}
  final response = await client.searchSingleIndex(
    indexName: "INDEX_NAME",
    searchParams: SearchParamsObject(
      filters:
          "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
      sumOrFiltersScores: true,
    ),
  );
  ```

  ```go Go theme={"system"}
  response, err := client.SearchSingleIndex(client.NewApiSearchSingleIndexRequest(
    "INDEX_NAME").WithSearchParams(search.SearchParamsObjectAsSearchParams(
    search.NewEmptySearchParamsObject().
      SetFilters("(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)").
      SetSumOrFiltersScores(true),
  )))
  if err != nil {
    // handle the eventual error
    panic(err)
  }
  ```

  ```java Java theme={"system"}
  SearchResponse response = client.searchSingleIndex(
    "INDEX_NAME",
    new SearchParamsObject()
      .setFilters("(company:Google<score=3> OR company:Amazon<score=2> OR" + " company:Facebook<score=1>)")
      .setSumOrFiltersScores(true),
    Hit.class
  );
  ```

  ```js JavaScript theme={"system"}
  const response = await client.searchSingleIndex({
    indexName: 'indexName',
    searchParams: {
      filters: '(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)',
      sumOrFiltersScores: true,
    },
  });
  ```

  ```kotlin Kotlin theme={"system"}
  var response =
    client.searchSingleIndex(
      indexName = "INDEX_NAME",
      searchParams =
        SearchParamsObject(
          filters =
            "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
          sumOrFiltersScores = true,
        ),
    )
  ```

  ```php PHP theme={"system"}
  $response = $client->searchSingleIndex(
      'INDEX_NAME',
      ['filters' => '(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)',
          'sumOrFiltersScores' => true,
      ],
  );
  ```

  ```python Python theme={"system"}
  response = client.search_single_index(
      index_name="INDEX_NAME",
      search_params={
          "filters": "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
          "sumOrFiltersScores": True,
      },
  )
  ```

  ```ruby Ruby theme={"system"}
  response = client.search_single_index(
    "INDEX_NAME",
    Algolia::Search::SearchParamsObject.new(
      filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
      sum_or_filters_scores: true
    )
  )
  ```

  ```scala Scala theme={"system"}
  val response = Await.result(
    client.searchSingleIndex(
      indexName = "INDEX_NAME",
      searchParams = Some(
        SearchParamsObject(
          filters = Some("(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)"),
          sumOrFiltersScores = Some(true)
        )
      )
    ),
    Duration(100, "sec")
  )
  ```

  ```swift Swift theme={"system"}
  let response: SearchResponse<Hit> = try await client.searchSingleIndex(
      indexName: "INDEX_NAME",
      searchParams: SearchSearchParams.searchSearchParamsObject(SearchSearchParamsObject(
          filters: "(company:Google<score=3> OR company:Amazon<score=2> OR company:Facebook<score=1>)",
          sumOrFiltersScores: true
      ))
  )
  ```
</CodeGroup>

{/* vale Vale.Spelling = NO  */}

## Scoring ANDs and ORs

Use the `OR` operator when you want to weigh terms differently.

* When filtering with `OR`, the overall score is based on the individual `true` value scores. If you have three filter values in your query and all three match, it will have a higher score than another record that matches only one filter value.
* Filtering only with ANDs removes the effect of scoring. With a group of ANDs, records are only chosen if all filters match: all records will have the same score.

## Scoring using numeric filters

You can't apply scores with numeric filters (like `>=`, `!=`, `>`).
Scoring can only be done on <Facet /> values using the `attribute:value<score=X>` syntax.

## See also

* [Optional filters use filter scoring to improve relevance](/doc/guides/managing-results/rules/merchandising-and-promoting/in-depth/optional-filters)
* [Using filter scoring to control the order of records (blog)](https://www.algolia.com/blog/engineering/feature-spotlight-optional-filters/)
* [Optional filters and filter scoring (blog)](https://www.algolia.com/blog/engineering/discover-what-fuzzy-search-is-with-fuzzy-matching/)
