📖 Scenario: You have a collection of products in an online store. You want to find all products in the category "electronics" and give them the same importance score, ignoring their relevance scores.
🎯 Goal: Create an Elasticsearch query using constant_score to find all products with category equal to "electronics" and assign them a fixed score of 1.5.
📋 What You'll Learn
Create a query dictionary with a constant_score key
Inside constant_score, use a filter with a term query for category equal to "electronics"
Set the boost value to 1.5
Print the final query dictionary
💡 Why This Matters
🌍 Real World
Online stores often want to find products in a category and treat them equally, ignoring text relevance scores.
💼 Career
Knowing how to write constant score queries helps in building efficient search features in e-commerce and content platforms.
Progress0 / 4 steps
1
Create the base query dictionary
Create a variable called query and set it to an empty dictionary {}.
Elasticsearch
Hint
Use query = {} to start an empty query dictionary.
2
Add the constant_score key with filter and boost
Add a constant_score key to the query dictionary. Set it to a dictionary with a filter key. The filter should be a dictionary with a term key. The term key should be a dictionary with category set to "electronics". Also add a boost key with the value 1.5 inside constant_score.
Elasticsearch
Hint
Use nested dictionaries to build the constant_score query with filter and boost.
3
Wrap the constant_score inside the query key
Wrap the constant_score dictionary inside another dictionary with the key query. Assign this new dictionary back to the variable query.
Elasticsearch
Hint
Put the constant_score dictionary inside another dictionary with key 'query'.
4
Print the final query dictionary
Write a print(query) statement to display the final query dictionary.
Elasticsearch
Hint
Use print(query) to show the final query.
Practice
(1/5)
1. What is the main purpose of a constant_score query in Elasticsearch?
easy
A. To filter documents and assign the same score to all matched documents
B. To sort documents by their relevance score
C. To perform a full-text search with varying scores
D. To aggregate documents by a field value
Solution
Step 1: Understand the role of constant_score query
The constant_score query filters documents and assigns the same score to all matches, ignoring relevance scoring.
Step 2: Compare with other query types
Unlike full-text queries that score by relevance, constant_score keeps scores equal for filtered documents.
Final Answer:
To filter documents and assign the same score to all matched documents -> Option A
Quick Check:
constant_score = equal scoring for filtered docs [OK]
Hint: Constant score means all matched docs get the same score [OK]
Common Mistakes:
Thinking it sorts by relevance
Confusing with full-text scoring
Assuming it aggregates data
2. Which of the following is the correct syntax to create a constant score query with a filter on field status equal to active?
easy
A. {"constant_score": {"filter": {"match": {"status": "active"}}}}
B. {"constant_score": {"query": {"term": {"status": "active"}}}}
C. {"constant_score": {"term": {"status": "active"}}}
D. {"constant_score": {"filter": {"term": {"status": "active"}}}}
Solution
Step 1: Identify correct key for filtering
The constant_score query uses the key "filter" to specify the filter clause, not "query" or direct term.
Step 2: Check filter syntax
The filter should be a term query inside the filter object, correctly written as {"term": {"status": "active"}}.
Final Answer:
{"constant_score": {"filter": {"term": {"status": "active"}}}} -> Option D
Quick Check:
Use "filter" with term inside constant_score [OK]
Hint: Use "filter" key with term query inside constant_score [OK]
Common Mistakes:
Using "query" instead of "filter"
Placing term query directly under constant_score
Using match instead of term for exact filter
3. Given this Elasticsearch query snippet, what will be the score of each matched document?
A. The boost value should be a number, not a string
B. The filter key should be replaced with query
C. The terms query cannot be used inside constant_score
D. The tags field should be inside a match query
Solution
Step 1: Check boost data type
The boost parameter must be a numeric value, not a string. Here, "2" is a string, which is invalid.
Step 2: Validate filter and terms usage
Using "filter" with a "terms" query is valid syntax, so no error there.
Final Answer:
The boost value should be a number, not a string -> Option A
Quick Check:
boost must be numeric, not string [OK]
Hint: Boost must be a number, not quoted string [OK]
Common Mistakes:
Using string for boost instead of number
Replacing filter with query incorrectly
Misunderstanding terms query usage
5. You want to find all documents where status is published and boost their score by 5, but keep all matched documents scored equally. Which query correctly achieves this?
hard
A. { "match": { "status": "published", "boost": 5 } }
To keep scores equal and apply a boost, use constant_score with a filter clause and boost parameter.
Step 2: Validate correct syntax
{ "constant_score": { "filter": { "term": { "status": "published" } }, "boost": 5 } } uses "filter" with a term query and numeric boost, which is the correct syntax.
Step 3: Check other options
{ "bool": { "must": { "term": { "status": "published" } }, "boost": 5 } } uses bool query which scores by relevance, not constant. { "match": { "status": "published", "boost": 5 } } is a match query with boost but scores vary. { "constant_score": { "query": { "term": { "status": "published" } }, "boost": 5 } } incorrectly uses "query" instead of "filter" inside constant_score.