Reference Entry
ScriptQuery for Custom JavaScript Filters
Lexical Querying · advanced · order 90
Filter documents with a JavaScript function when built-in query types do not express the rule cleanly.
Relevant APIs
ScriptQuery for Custom JavaScript Filters
script lets you filter documents with a JavaScript function.
Use it when the condition is too specific for the built-in query classes.
Basic example
{
"query": {
"script": {
"script": {
"source": "(numericValue(\"popularity\") ?? 0) >= 10"
}
}
}
}
Available helpers
The script receives a context with:
documentdocumentIndexscorevalues(field)value(field)numericValues(field)numericValue(field)
Good uses
- one-off eligibility rules
- field combinations that would be awkward in bool logic
- filters that depend on numeric parsing
Notes
- This is flexible but less declarative than built-in queries.
- Prefer normal query types when they already express the behavior clearly.
scriptreturns matching documents with a base score of1.0.