Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check filtered index feature flag before querying in SearchContext #2408

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions api/api/utils/search_context.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from dataclasses import asdict, dataclass
from typing import Self

from django.conf import settings

from elasticsearch_dsl import Q, Search
from elasticsearch_dsl.response import Hit

Expand All @@ -26,6 +28,9 @@ def build(cls, results: list[Hit], origin_index: OriginIndex) -> Self:

all_result_identifiers = {r.identifier for r in results}

if not settings.ENABLE_FILTERED_INDEX_QUERIES:
return cls(all_result_identifiers, set())

filtered_index_search = Search(index=f"{origin_index}-filtered")
filtered_index_search = filtered_index_search.query(
# Use `identifier` rather than the document `id` due to
Expand Down
34 changes: 31 additions & 3 deletions api/test/unit/utils/test_search_context.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pook
import pytest

from api.utils.search_context import SearchContext
Expand All @@ -17,7 +18,16 @@ def test_no_results(media_type_config):
(True, False),
ids=lambda x: "has_sensitive_text" if x else "no_sensitive_text",
)
def test_sensitive_text(media_type_config, has_sensitive_text):
@pytest.mark.parametrize(
"setting_enabled",
(True, False),
ids=lambda x: "setting_enabled" if x else "setting_disabled",
)
def test_sensitive_text(
media_type_config, has_sensitive_text, setting_enabled, settings
):
settings.ENABLE_FILTERED_INDEX_QUERIES = setting_enabled

clear_results = media_type_config.model_factory.create_batch(
# Use size 10 to force result size beyond the default ES query window
size=10,
Expand All @@ -39,9 +49,27 @@ def test_sensitive_text(media_type_config, has_sensitive_text):

results = [maybe_sensitive_text_hit] + [hit for _, hit in clear_results]

search_context = SearchContext.build(results, media_type_config.origin_index)
if not setting_enabled:
es_host = settings.ES.transport.kwargs["host"]
es_port = settings.ES.transport.kwargs["port"]

with pook.post(
f"http://{es_host}:{es_port}/{media_type_config.filtered_index}/_search",
reply=500,
) as mock:
search_context = SearchContext.build(
results, media_type_config.origin_index
)
assert (
mock.total_matches == 0
), "There should be zero requests to ES if the settting is disabled"
pook.off()
else:
search_context = SearchContext.build(results, media_type_config.origin_index)

assert search_context == SearchContext(
{r.identifier for r in results},
{maybe_sensitive_text_model.identifier} if has_sensitive_text else set(),
{maybe_sensitive_text_model.identifier}
if has_sensitive_text and setting_enabled
else set(),
)