Repository: Informasjonsforvaltning/fdk-fulltext-search

Issue:
Støtte and og or filter som input fra bruker
I dag er det hardkoda at vi bruker `and` når vi filterer på tema. 

Vi bør støtte at bruker oppgir om det skal brukes `and` eller `or`.

Discussion:

Patch:
diff --git a/src/fdk_fulltext_search/search/query_filter_utils.py b/src/fdk_fulltext_search/search/query_filter_utils.py
index d9c83a0..e8ab8cb 100644
--- a/src/fdk_fulltext_search/search/query_filter_utils.py
+++ b/src/fdk_fulltext_search/search/query_filter_utils.py
@@ -131,12 +131,16 @@ def collection_filter(filter_obj: Dict) -> Dict[str, Dict]:
         key=filter_obj["field"], collection=filter_obj["values"]
     )
 
-    if get_field_by_filter_key("datasetMediaType") == filter_obj["field"]:
-        return {"bool": {"must": collection}}
-    if get_field_by_filter_key("formats") == filter_obj["field"]:
-        return {"bool": {"must": collection}}
+    clause = "should"
+    if (get_field_by_filter_key("datasetMediaType") == filter_obj["field"]) or (
+        get_field_by_filter_key("formats") == filter_obj["field"]
+    ):
+        clause = "must"
 
-    return {"bool": {"should": collection}}
+    if "operator" in filter_obj:
+        clause = "must" if filter_obj["operator"] == "AND" else "should"
+
+    return {"bool": {clause: collection}}
 
 
 def keyword_filter(keyword: str) -> Dict[str, Dict]:


Test patch:
diff --git a/tests/test_query_utils.py b/tests/test_query_utils.py
index ccec6bd..7b87838 100644
--- a/tests/test_query_utils.py
+++ b/tests/test_query_utils.py
@@ -727,3 +727,65 @@ def test_collection_filter():
 
     result = collection_filter(filter_obj)
     assert result == expected
+
+
+def test_collection_filter_with_and_operator():
+    expected = {
+        "bool": {
+            "must": [
+                {
+                    "term": {
+                        "uri.keyword": "https://fellesdatakatalog.brreg.no/api/concepts/a8ea479a-9b61-4cc2-86e8-650a03a322cc"
+                    }
+                },
+                {
+                    "term": {
+                        "uri.keyword": "http://brreg.no/catalogs/910244132/datasets/c32b7a4f-655f-45f6-88f6-d01f05d0f7c2"
+                    }
+                },
+            ]
+        }
+    }
+
+    filter_obj = {
+        "field": "uri",
+        "values": [
+            "https://fellesdatakatalog.brreg.no/api/concepts/a8ea479a-9b61-4cc2-86e8-650a03a322cc",
+            "http://brreg.no/catalogs/910244132/datasets/c32b7a4f-655f-45f6-88f6-d01f05d0f7c2",
+        ],
+        "operator": "AND",
+    }
+
+    result = collection_filter(filter_obj)
+    assert result == expected
+
+
+def test_collection_filter_with_or_operator():
+    expected = {
+        "bool": {
+            "should": [
+                {
+                    "term": {
+                        "uri.keyword": "https://fellesdatakatalog.brreg.no/api/concepts/a8ea479a-9b61-4cc2-86e8-650a03a322cc"
+                    }
+                },
+                {
+                    "term": {
+                        "uri.keyword": "http://brreg.no/catalogs/910244132/datasets/c32b7a4f-655f-45f6-88f6-d01f05d0f7c2"
+                    }
+                },
+            ]
+        }
+    }
+
+    filter_obj = {
+        "field": "uri",
+        "values": [
+            "https://fellesdatakatalog.brreg.no/api/concepts/a8ea479a-9b61-4cc2-86e8-650a03a322cc",
+            "http://brreg.no/catalogs/910244132/datasets/c32b7a4f-655f-45f6-88f6-d01f05d0f7c2",
+        ],
+        "operator": "OR",
+    }
+
+    result = collection_filter(filter_obj)
+    assert result == expected