Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Changed "exact match boost" implementation to use a match_phrase query in should (#4978)

Aditya raj7a0e4eb47c24
diff --git a/api/api/controllers/search_controller.py b/api/api/controllers/search_controller.py
index b7420ca02..2d38380c6 100644
--- a/api/api/controllers/search_controller.py
+++ b/api/api/controllers/search_controller.py
@@ -302,14 +302,7 @@ def build_search_query(
search_queries["must"].append(Q("simple_query_string", **base_query_kwargs))
# Boost exact matches on the title
- quotes_stripped = query.replace('"', "")
- exact_match_boost = Q(
- "simple_query_string",
- flags=DEFAULT_SQS_FLAGS,
- fields=["title"],
- query=f"{quotes_stripped}",
- boost=10000,
- )
+ exact_match_boost = Q("match_phrase", title={"query": query, "boost": 10000})
search_queries["should"].append(exact_match_boost)
else:
for field, field_name in [
diff --git a/api/test/unit/controllers/test_search_controller_search_query.py b/api/test/unit/controllers/test_search_controller_search_query.py
index 541443833..a64e84759 100644
--- a/api/test/unit/controllers/test_search_controller_search_query.py
+++ b/api/test/unit/controllers/test_search_controller_search_query.py
@@ -91,11 +91,11 @@ def test_create_search_query_q_search_no_filters(media_type_config, anon_request
],
"should": [
{
- "simple_query_string": {
- "boost": 10000,
- "fields": ["title"],
- "query": "cat",
- "flags": DEFAULT_SQS_FLAGS,
+ "match_phrase": {
+ "title": {
+ "query": "cat",
+ "boost": 10000,
+ }
}
},
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}},
@@ -129,11 +129,11 @@ def test_create_search_query_q_search_with_quotes_adds_raw_suffix(
],
"should": [
{
- "simple_query_string": {
- "boost": 10000,
- "fields": ["title"],
- "query": "The cutest cat",
- "flags": DEFAULT_SQS_FLAGS,
+ "match_phrase": {
+ "title": {
+ "query": '"The cutest cat"',
+ "boost": 10000,
+ }
}
},
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}},
@@ -185,11 +185,11 @@ def test_create_search_query_q_search_with_filters(
],
"should": [
{
- "simple_query_string": {
- "boost": 10000,
- "fields": ["title"],
- "query": "cat",
- "flags": DEFAULT_SQS_FLAGS,
+ "match_phrase": {
+ "title": {
+ "query": "cat",
+ "boost": 10000,
+ }
}
},
{"rank_feature": {"boost": 10000, "field": "standardized_popularity"}},