Skip to content

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

Fix sensitive term reindex query (#2363)

sarayourfriend6bccdf967262
diff --git a/ingestion_server/ingestion_server/es_mapping.py b/ingestion_server/ingestion_server/es_mapping.py
index 06f4e204d..b780088a8 100644
--- a/ingestion_server/ingestion_server/es_mapping.py
+++ b/ingestion_server/ingestion_server/es_mapping.py
@@ -60,7 +60,10 @@ def index_settings(table_name):
"title": {
"type": "text",
"similarity": "boolean",
- "fields": {"keyword": {"type": "keyword", "ignore_above": 256}},
+ "fields": {
+ "keyword": {"type": "keyword", "ignore_above": 256},
+ "raw": {"type": "text", "index": True},
+ },
"analyzer": "custom_english",
},
"foreign_landing_url": {
@@ -68,7 +71,10 @@ def index_settings(table_name):
"type": "text",
},
"description": {
- "fields": {"keyword": {"type": "keyword", "similarity": "boolean"}},
+ "fields": {
+ "keyword": {"type": "keyword", "similarity": "boolean"},
+ "raw": {"type": "text", "index": True},
+ },
"type": "text",
"analyzer": "custom_english",
},
@@ -111,7 +117,10 @@ def index_settings(table_name):
"accuracy": {"type": "float"},
"name": {
"type": "text",
- "fields": {"keyword": {"type": "keyword", "ignore_above": 256}},
+ "fields": {
+ "keyword": {"type": "keyword", "ignore_above": 256},
+ "raw": {"type": "text", "index": True},
+ },
"analyzer": "custom_english",
},
}
diff --git a/ingestion_server/ingestion_server/indexer.py b/ingestion_server/ingestion_server/indexer.py
index c8e149c40..3e4850266 100644
--- a/ingestion_server/ingestion_server/indexer.py
+++ b/ingestion_server/ingestion_server/indexer.py
@@ -517,13 +517,10 @@ class TableIndexer:
"query": {
"bool": {
"must_not": [
- {
- "multi_match": {
- "query": f'"{term}"',
- "fields": ["tags.name", "title", "description"],
- }
- }
- for term in sensitive_terms
+ # Use `terms` query for exact matching against
+ # unanalyzed raw fields
+ {"terms": {f"{field}.raw": sensitive_terms}}
+ for field in ["tags.name", "title", "description"]
]
}
},
diff --git a/ingestion_server/ingestion_server/utils/sensitive_terms.py b/ingestion_server/ingestion_server/utils/sensitive_terms.py
index 7d0eeeead..da0928ad6 100644
--- a/ingestion_server/ingestion_server/utils/sensitive_terms.py
+++ b/ingestion_server/ingestion_server/utils/sensitive_terms.py
@@ -12,4 +12,4 @@ SENSITIVE_TERMS_URL = config(
def get_sensitive_terms() -> list[str]:
response: HTTPResponse = urlopen(SENSITIVE_TERMS_URL)
- return [line.decode("utf-8") for line in response.readlines()]
+ return [line.decode("utf-8").strip() for line in response.readlines()]
diff --git a/ingestion_server/test/integration_test.py b/ingestion_server/test/integration_test.py
index 00802f77b..a0275e46c 100644
--- a/ingestion_server/test/integration_test.py
+++ b/ingestion_server/test/integration_test.py
@@ -548,25 +548,32 @@ class TestIngestion(unittest.TestCase):
)
def test_filtered_indexes(self):
"""
- Check that the audio data has been successfully indexed in Elasticsearch.
+ Check that the sensitive terms are correctly filtered out.
- The number of hits for a blank search should match the size of the loaded mock
- data.
+ Each sensitive term should have zero exact matches for it when querying
+ the filtered index. We use `term` against the `.raw` fields to ensure this.
"""
params = zip(mock_sensitive_terms, ["audio-filtered", "image-filtered"])
for sensitive_term, index in params:
with self.subTest(
- f"Check that {index} does not include mock sensitive term {sensitive_term}"
+ f"Check that {index} does not include mock sensitive term {sensitive_term} exactly"
):
es = self._get_es()
- res = es.search(
- index=index, query={"query_string": {"query": sensitive_term}}
- )
+ queryable_fields = ["title", "description", "tags.name"]
+ query = {
+ "bool": {
+ "should": [
+ {"term": {f"{field}.raw": sensitive_term}}
+ for field in queryable_fields
+ ]
+ }
+ }
+ res = es.search(index=index, query=query)
count = res["hits"]["total"]["value"]
self.assertEqual(
count,
0,
- f"There should be no results for {sensitive_term} in {index}. Found {count}.",
+ f"There should be no results that exactly match {sensitive_term} in {index}. Found {count}.",
)
@pytest.mark.order(after="test_upstream_indexed_audio")