Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Add ability to boost search results by authority (#891)
Olga Bulata46e3b240159
diff --git a/api/catalog/api/controllers/search_controller.py b/api/catalog/api/controllers/search_controller.pyindex a5056601b..9dc0f699e 100644--- a/api/catalog/api/controllers/search_controller.py+++ b/api/catalog/api/controllers/search_controller.py@@ -33,6 +33,7 @@ URL = "url"PROVIDER = "provider"DEEP_PAGINATION_ERROR = "Deep pagination is not allowed."QUERY_SPECIAL_CHARACTER_ERROR = "Unescaped special characters are not allowed."+DEFAULT_BOOST = 10000class RankFeature(Query):@@ -374,7 +375,12 @@ def search(s = s.query("simple_query_string", fields=["tags.name"], query=tags)if settings.USE_RANK_FEATURES:- feature_boost = {"standardized_popularity": 10000}+ feature_boost = {"standardized_popularity": DEFAULT_BOOST}+ if search_params.data["unstable__authority"]:+ feature_boost["authority_boost"] = (+ search_params.data["unstable__authority_boost"] * DEFAULT_BOOST+ )+rank_queries = []for field, boost in feature_boost.items():rank_queries.append(Q("rank_feature", field=field, boost=boost))diff --git a/api/catalog/api/serializers/media_serializers.py b/api/catalog/api/serializers/media_serializers.pyindex b8c63cfff..266717355 100644--- a/api/catalog/api/serializers/media_serializers.py+++ b/api/catalog/api/serializers/media_serializers.py@@ -50,6 +50,8 @@ class MediaSearchRequestSerializer(serializers.Serializer):"qa",# "unstable__sort_by", # excluding unstable fields# "unstable__sort_dir", # excluding unstable fields+ # "unstable__authority", # excluding unstable fields+ # "unstable__authority_boost", # excluding unstable fields"page_size","page",]@@ -138,6 +140,22 @@ class MediaSearchRequestSerializer(serializers.Serializer):required=False,default=DESCENDING,)+ unstable__authority = serializers.BooleanField(+ label="authority",+ help_text="If enabled, the search will add a boost to results that are "+ "from authoritative sources.",+ required=False,+ default=False,+ )+ unstable__authority_boost = serializers.FloatField(+ label="authority_boost",+ help_text="The boost coefficient to apply to authoritative sources, "+ "multiplied with the popularity boost.",+ required=False,+ default=1.0,+ min_value=0.0,+ max_value=10.0,+ )page_size = serializers.IntegerField(label="page_size",@@ -155,6 +173,10 @@ class MediaSearchRequestSerializer(serializers.Serializer):min_value=1,)+ def is_request_anonymous(self):+ request = self.context.get("request")+ return bool(request and request.user and request.user.is_anonymous)+@staticmethoddef _truncate(value):max_length = 200@@ -201,18 +223,16 @@ class MediaSearchRequestSerializer(serializers.Serializer):return self._truncate(value)def validate_unstable__sort_by(self, value):- request = self.context.get("request")- is_anonymous = bool(request and request.user and request.user.is_anonymous)- return RELEVANCE if is_anonymous else value+ return RELEVANCE if self.is_request_anonymous() else valuedef validate_unstable__sort_dir(self, value):- request = self.context.get("request")- is_anonymous = bool(request and request.user and request.user.is_anonymous)- return DESCENDING if is_anonymous else value+ return DESCENDING if self.is_request_anonymous() else value++ def validate_unstable__authority(self, value):+ return False if self.is_request_anonymous() else valuedef validate_page_size(self, value):- request = self.context.get("request")- is_anonymous = bool(request and request.user and request.user.is_anonymous)+ is_anonymous = self.is_request_anonymous()max_value = (settings.MAX_ANONYMOUS_PAGE_SIZEif is_anonymousdiff --git a/api/test/auth_test.py b/api/test/auth_test.pyindex b5022570d..daa397306 100644--- a/api/test/auth_test.py+++ b/api/test/auth_test.py@@ -121,6 +121,35 @@ def test_sorting_authed(assert indexed_on == exp_indexed_on+ "authority_boost, exp_source",+ [+ ("1.0", "stocksnap"),+ ("0.0", "flickr"), # Authority boost is disabled+ ],+)+def test_authority_authed(+ client, monkeypatch, test_auth_token_exchange, authority_boost, exp_source+):+ # Prevent DB lookup for ES results because DB is empty.+ monkeypatch.setattr("catalog.api.views.image_views.ImageSerializer.needs_db", False)++ time.sleep(1)+ token = test_auth_token_exchange["access_token"]+ query_params = {+ "q": "cat",+ "unstable__authority": "true",+ "unstable__authority_boost": authority_boost,+ }+ res = client.get("/v1/images/", query_params, HTTP_AUTHORIZATION=f"Bearer {token}")+ assert res.status_code == 200++ res_data = res.json()+ source = res_data["results"][0]["source"]+ assert source == exp_source++@pytest.mark.django_dbdef test_page_size_limit_unauthed(client):query_params = {"page_size": 20}