Skip to content

Commit

Permalink
[SDBELGA-742] improve(api): Support searching languages field (#1832)
Browse files Browse the repository at this point in the history
* [SDBELGA-742] improve(api): Support searching languages field

* run black utility
  • Loading branch information
MarkLark86 authored Jul 26, 2023
1 parent 3e08c30 commit 6012cd7
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 6 deletions.
73 changes: 70 additions & 3 deletions server/features/search_multilingual.feature
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
Feature: Search multilingual metadata
@auth
Scenario: Can search multilingual data
Background: Init system metadata
Given "vocabularies"
"""
[{
Expand Down Expand Up @@ -38,7 +37,10 @@ Feature: Search multilingual metadata
}
}]
"""
And "events"

@auth
Scenario: Can search multilingual data
Given "events"
"""
[{
"guid": "event_123",
Expand Down Expand Up @@ -105,3 +107,68 @@ Feature: Search multilingual metadata
"""
{"_items": [{"_id": "event_123"}]}
"""

@auth
Scenario: Can search languages fields
Given "events"
"""
[{
"guid": "event-single-en", "slugline": "slugline", "name": "name",
"dates": {"start": "2045-01-20T00:00:00+0000", "end": "2045-01-20T01:00:00+0000"},
"language": "en"
}, {
"guid": "event-single-nl", "slugline": "slugline", "name": "name",
"dates": {"start": "2045-01-20T02:00:00+0000", "end": "2045-01-20T03:00:00+0000"},
"language": "nl"
}, {
"guid": "event-multi-en", "slugline": "slugline", "name": "name",
"dates": {"start": "2045-01-20T04:00:00+0000", "end": "2045-01-20T05:00:00+0000"},
"languages": ["en"]
}, {
"guid": "event-multi-nl", "slugline": "slugline", "name": "name",
"dates": {"start": "2045-01-20T06:00:00+0000", "end": "2045-01-20T07:00:00+0000"},
"languages": ["nl"]
}, {
"guid": "event-multi-en-nl", "slugline": "slugline", "name": "name",
"dates": {"start": "2045-01-20T08:00:00+0000", "end": "2045-01-20T09:00:00+0000"},
"languages": ["en", "nl"]
}, {
"guid": "event-single-de", "slugline": "slugline", "name": "name",
"dates": {"start": "2045-01-20T10:00:00+0000", "end": "2045-01-20T11:00:00+0000"},
"language": "de"
}, {
"guid": "event-multi-de", "slugline": "slugline", "name": "name",
"dates": {"start": "2045-01-20T10:00:00+0000", "end": "2045-01-20T11:00:00+0000"},
"languages": ["de"]
}]
"""
When we get "/events_planning_search?repo=events&language=en"
Then we get list with 3 items
"""
{"_items": [
{"_id": "event-single-en"},
{"_id": "event-multi-en"},
{"_id": "event-multi-en-nl"}
]}
"""
When we get "/events_planning_search?repo=events&language=de"
Then we get list with 2 items
"""
{"_items": [
{"_id": "event-single-de"},
{"_id": "event-multi-de"}
]}
"""
When we get "/events_planning_search?repo=events&language=fr"
Then we get list with 0 items
When we get "/events_planning_search?repo=events&language=en,nl"
Then we get list with 5 items
"""
{"_items": [
{"_id": "event-single-en"},
{"_id": "event-single-nl"},
{"_id": "event-multi-en"},
{"_id": "event-multi-nl"},
{"_id": "event-multi-en-nl"}
]}
"""
5 changes: 4 additions & 1 deletion server/planning/events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ def on_create(self, docs):

# SDCP-638
if not event.get("language"):
event["language"] = app.config["DEFAULT_LANGUAGE"]
try:
event["language"] = event["languages"][0]
except (KeyError, IndexError):
event["language"] = app.config["DEFAULT_LANGUAGE"]

# family_id get on ingest we don't need it planning
event.pop("family_id", None)
Expand Down
5 changes: 4 additions & 1 deletion server/planning/planning/planning.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,10 @@ def on_create(self, docs):

# SDCP-638
if not doc.get("language"):
doc["language"] = app.config["DEFAULT_LANGUAGE"]
try:
doc["language"] = doc["languages"][0]
except (KeyError, IndexError):
doc["language"] = app.config["DEFAULT_LANGUAGE"]

self.validate_planning(doc)
set_original_creator(doc)
Expand Down
6 changes: 5 additions & 1 deletion server/planning/search/queries/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ def search_language(params: Dict[str, Any], query: elastic.ElasticQuery):
languages = str_to_array(params.get("language"))

if len(languages):
query.must.append(elastic.terms(field="language", values=languages))
query.must.append(
elastic.bool_or(
[elastic.terms(field="language", values=languages), elastic.terms(field="languages", values=languages)]
)
)


def search_locked(params: Dict[str, Any], query: elastic.ElasticQuery):
Expand Down

0 comments on commit 6012cd7

Please sign in to comment.