﻿---
title: Weight context
description: Use a Painless script to create a weight for use in a similarity script. The weight makes up the part of the similarity calculation that is independent...
url: https://www.elastic.co/elastic/docs-builder/docs/2510/reference/scripting-languages/painless/painless-weight-context
products:
  - Painless
---

# Weight context
Use a Painless script to create a [weight](https://www.elastic.co/elastic/docs-builder/docs/2510/reference/elasticsearch/index-settings/similarity) for use in a [similarity script](https://www.elastic.co/elastic/docs-builder/docs/2510/reference/scripting-languages/painless/painless-similarity-context). The weight makes up the part of the similarity calculation that is independent of the document being scored, and so can be built up front and cached.
Queries that contain multiple terms calculate a separate weight for each term.
**Variables**
<definitions>
  <definition term="query.boost (float, read-only)">
    The boost value if provided by the query. If this is not provided the value is `1.0f`.
  </definition>
  <definition term="field.docCount (long, read-only)">
    The number of documents that have a value for the current field.
  </definition>
  <definition term="field.sumDocFreq (long, read-only)">
    The sum of all terms that exist for the current field. If this is not available the value is `-1`.
  </definition>
  <definition term="field.sumTotalTermFreq (long, read-only)">
    The sum of occurrences in the index for all the terms that exist in the current field. If this is not available the value is `-1`.
  </definition>
  <definition term="term.docFreq (long, read-only)">
    The number of documents that contain the current term in the index.
  </definition>
  <definition term="term.totalTermFreq (long, read-only)">
    The total occurrences of the current term in the index.
  </definition>
</definitions>

**Return**
<definitions>
  <definition term="double">
    A scoring factor used across all documents.
  </definition>
</definitions>

**API**
The standard [Painless API](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-api-reference-shared.html) is available.