Skip to content

Commit 9719cc4

Browse files
Merge pull request #36 from kitconcept/fhnw-develop-keyword-extra-condition
Add extra conditions for string matching
2 parents db2f5c1 + 904fb0c commit 9719cc4

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed

src/kitconcept/solr/services/solr_utils_extra.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,33 @@ def query_list(self):
7474
result += f"{value}}}"
7575
else:
7676
result += "*]"
77+
elif kind == "string":
78+
keys = set(condition.keys())
79+
if not keys.issubset({"in"}):
80+
raise RuntimeError(
81+
f"invalid keys in options for condition 'string', supported: 'in' [{keys}]"
82+
)
83+
if "in" in condition:
84+
if type(condition["in"]) is not list:
85+
raise RuntimeError(
86+
f"invalid type for condition 'string' [{type(condition['in'])}]"
87+
)
88+
if len(condition["in"]) == 0:
89+
# Empty list, ignore
90+
continue
91+
result = f"{fieldname}:"
92+
result += (
93+
"("
94+
+ " OR ".join(
95+
list(
96+
map(
97+
lambda term: replace_reserved(term),
98+
condition["in"],
99+
)
100+
)
101+
)
102+
+ ")"
103+
)
77104
else:
78105
raise RuntimeError(f"Wrong condition type [{kind}]")
79106
results.append(result)

tests/services/extra_conditions/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ def contents() -> List:
1414
"subjects": ["mymembersubject", "mymembersubjecttwo"],
1515
"_image": b"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAA1JREFUGFdjCDO+/R8ABKsCZD++CcMAAAAASUVORK5CYII=", # noQA
1616
"language": "en",
17+
"searchwords": ["term1", "term2"],
1718
},
1819
{
1920
"_container": "",
2021
"type": "Document",
2122
"id": "myfolder",
2223
"title": "My Folder to store everything about Noam Chomsky",
2324
"language": "en",
25+
"searchwords": ["term1", "term2"],
2426
},
2527
{
2628
"_container": "myfolder",
@@ -37,6 +39,7 @@ def contents() -> List:
3739
"description": "Chomsky alpha",
3840
"language": "en",
3941
"start": "2021-01-01T00:00:00",
42+
"searchwords": ["term1"],
4043
},
4144
{
4245
"_container": "",
@@ -46,6 +49,7 @@ def contents() -> List:
4649
"description": "Chomsky beta",
4750
"language": "en",
4851
"start": "2021-02-01T00:00:00",
52+
"searchwords": ["term1"],
4953
},
5054
{
5155
"_container": "",
@@ -55,6 +59,7 @@ def contents() -> List:
5559
"description": "Chomsky gamma",
5660
"language": "en",
5761
"start": "2021-03-01T00:00:00",
62+
"searchwords": ["term1"],
5863
},
5964
{
6065
"_container": "",
@@ -64,6 +69,7 @@ def contents() -> List:
6469
"description": "Chomsky alpha",
6570
"language": "en",
6671
"start": "2021-01-01T00:00:00",
72+
"searchwords": ["term2"],
6773
},
6874
{
6975
"_container": "",
@@ -73,6 +79,7 @@ def contents() -> List:
7379
"description": "Chomsky beta",
7480
"language": "en",
7581
"start": "2021-02-01T00:00:00",
82+
"searchwords": ["term2"],
7683
},
7784
{
7885
"_container": "",
@@ -82,5 +89,6 @@ def contents() -> List:
8289
"description": "Chomsky gamma",
8390
"language": "en",
8491
"start": "2021-03-01T00:00:00",
92+
"searchwords": ["term2"],
8593
},
8694
]

tests/services/extra_conditions/test_extra_conditions.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,3 +285,119 @@ def test_facet_fields(self):
285285
def test_paths(self, all_path_string, path: str, expected: bool):
286286
path_strings = all_path_string(self.data)
287287
assert (path in path_strings) is expected
288+
289+
290+
class TestExtraConditionsStringInSingleTerm(TestEndpointCustom):
291+
extra_conditions = [
292+
[
293+
"searchwords",
294+
"string",
295+
{"in": ["term1"]},
296+
],
297+
]
298+
url = f"/@solr?q=chomsky&group_select=1&extra_conditions={encoded(extra_conditions)}"
299+
300+
@pytest.mark.parametrize(
301+
"path,expected",
302+
[
303+
("/plone/myimage", False),
304+
("/plone/myfolder", True),
305+
("/plone/myfolder/mynews", False),
306+
("/plone/foo_alpha", True),
307+
("/plone/foo_beta", True),
308+
("/plone/foo_gamma", True),
309+
("/plone/bar_alpha", False),
310+
("/plone/bar_beta", False),
311+
("/plone/bar_gamma", False),
312+
],
313+
)
314+
def test_paths(self, all_path_string, path: str, expected: bool):
315+
path_strings = all_path_string(self.data)
316+
assert (path in path_strings) is expected
317+
318+
319+
class TestExtraConditionsStringInSingleTerm2(TestEndpointCustom):
320+
extra_conditions = [
321+
[
322+
"searchwords",
323+
"string",
324+
{"in": ["term2"]},
325+
],
326+
]
327+
url = f"/@solr?q=chomsky&group_select=1&extra_conditions={encoded(extra_conditions)}"
328+
329+
@pytest.mark.parametrize(
330+
"path,expected",
331+
[
332+
("/plone/myimage", False),
333+
("/plone/myfolder", True),
334+
("/plone/myfolder/mynews", False),
335+
("/plone/foo_alpha", False),
336+
("/plone/foo_beta", False),
337+
("/plone/foo_gamma", False),
338+
("/plone/bar_alpha", True),
339+
("/plone/bar_beta", True),
340+
("/plone/bar_gamma", True),
341+
],
342+
)
343+
def test_paths(self, all_path_string, path: str, expected: bool):
344+
path_strings = all_path_string(self.data)
345+
assert (path in path_strings) is expected
346+
347+
348+
class TestExtraConditionsStringInMultipleTerms(TestEndpointCustom):
349+
extra_conditions = [
350+
[
351+
"searchwords",
352+
"string",
353+
{"in": ["term1", "term2"]},
354+
],
355+
]
356+
url = f"/@solr?q=chomsky&group_select=1&extra_conditions={encoded(extra_conditions)}"
357+
358+
@pytest.mark.parametrize(
359+
"path,expected",
360+
[
361+
("/plone/myimage", False),
362+
("/plone/myfolder", True),
363+
("/plone/myfolder/mynews", False),
364+
("/plone/foo_alpha", True),
365+
("/plone/foo_beta", True),
366+
("/plone/foo_gamma", True),
367+
("/plone/bar_alpha", True),
368+
("/plone/bar_beta", True),
369+
("/plone/bar_gamma", True),
370+
],
371+
)
372+
def test_paths(self, all_path_string, path: str, expected: bool):
373+
path_strings = all_path_string(self.data)
374+
assert (path in path_strings) is expected
375+
376+
377+
class TestExtraConditionsStringInNoTerms(TestEndpointCustom):
378+
extra_conditions = [
379+
[
380+
"searchwords",
381+
"string",
382+
{"in": []},
383+
],
384+
]
385+
url = f"/@solr?q=chomsky&group_select=1&extra_conditions={encoded(extra_conditions)}"
386+
387+
@pytest.mark.parametrize(
388+
"path,expected",
389+
[
390+
("/plone/myimage", False),
391+
("/plone/myfolder", True),
392+
("/plone/myfolder/mynews", False),
393+
("/plone/foo_alpha", True),
394+
("/plone/foo_beta", True),
395+
("/plone/foo_gamma", True),
396+
("/plone/bar_alpha", True),
397+
("/plone/bar_beta", True),
398+
("/plone/bar_gamma", True),
399+
],
400+
)
401+
def test_paths(self, all_path_string, path: str, expected: bool):
402+
path_strings = all_path_string(self.data)
403+
assert (path in path_strings) is expected

tests/utils/test_utils_extra_conditions.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,29 @@ def test_query_list_invalid_combination_2(self):
122122
obj = SolrExtraConditions(config)
123123
with pytest.raises(RuntimeError):
124124
obj.query_list()
125+
126+
127+
class TestUtilsExtraConditionsString:
128+
def test_query_list_string_in_single_term(self):
129+
config = [["keywords", "string", {"in": ["term1"]}]]
130+
obj = SolrExtraConditions(config)
131+
result = obj.query_list()
132+
assert result == ["keywords:(term1)"]
133+
134+
def test_query_list_string_in_multiple_terms(self):
135+
config = [["keywords", "string", {"in": ["term1", "term2"]}]]
136+
obj = SolrExtraConditions(config)
137+
result = obj.query_list()
138+
assert result == ["keywords:(term1 OR term2)"]
139+
140+
def test_query_list_string_in_empty_term(self):
141+
config = [["keywords", "string", {"in": []}]]
142+
obj = SolrExtraConditions(config)
143+
result = obj.query_list()
144+
assert result == []
145+
146+
def test_query_list_string_in_invalid_term_type(self):
147+
config = [["keywords", "string", {"in": "NotAList"}]]
148+
obj = SolrExtraConditions(config)
149+
with pytest.raises(RuntimeError):
150+
obj.query_list()

0 commit comments

Comments
 (0)