Skip to content
Open
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
20 changes: 13 additions & 7 deletions solrorbit/conversion/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,13 +221,13 @@ def _translate_query_node(node: dict, fq_list: list = None) -> str:
bool_q = node["bool"]
parts = []

def _add_to_q(clauses, prefix):
def _add_to_q(clauses, prefix, nested_fq_list):
if not clauses:
return
if isinstance(clauses, dict):
clauses = [clauses]
for clause in clauses:
sub = _translate_query_node(clause, fq_list=fq_list)
sub = _translate_query_node(clause, fq_list=nested_fq_list)
if sub and sub != "*:*":
parts.append(f"{prefix}({sub})")

Expand All @@ -242,20 +242,26 @@ def _add_to_fq(clauses):
if fq_str:
fq_list.append(fq_str)

_add_to_q(bool_q.get("must"), "+")
_add_to_q(bool_q.get("must_not"), "-")
# A nested filter may only become a top-level fq where it is required anyway, i.e. under must.
# Under must_not it would be inverted, and under should it would turn alternatives into a conjunction.
_add_to_q(bool_q.get("must"), "+", fq_list)
_add_to_q(bool_q.get("must_not"), "-", None)

# bool.filter → Solr fq when fq_list is available; otherwise inline in q
if fq_list is not None:
_add_to_fq(bool_q.get("filter"))
else:
_add_to_q(bool_q.get("filter"), "+")
_add_to_q(bool_q.get("filter"), "+", None)

shoulds = bool_q.get("should", [])
if isinstance(shoulds, dict):
shoulds = [shoulds]
should_parts = [_translate_query_node(s, fq_list=fq_list) for s in shoulds]
should_parts = [s for s in should_parts if s and s != "*:*"]
should_parts = []
for clause in shoulds:
sub = _translate_query_node(clause, fq_list=None)
if sub and sub != "*:*":
# A nested bool is several clauses; group them so it stays one alternative.
should_parts.append(f"({sub})" if isinstance(clause, dict) and "bool" in clause else sub)
if should_parts:
parts.append("(" + " ".join(should_parts) + ")")

Expand Down
29 changes: 29 additions & 0 deletions tests/unit/solr/test_workload_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,35 @@ def test_bool_with_filter_goes_to_fq(self):
self.assertIn("filter", result)
self.assertTrue(len(result["filter"]) > 0)

def test_filter_under_must_not_stays_excluded(self):
# Hoisting this filter to fq would keep only the documents the query excludes.
body = {"query": {"bool": {"must_not": [{"bool": {"filter": [{"term": {"status": "deleted"}}]}}]}}}
result = translate_to_solr_json_dsl(body)
self.assertNotIn("filter", result)
self.assertEqual("-(+(status:deleted))", result["query"])

def test_filters_under_should_stay_alternatives(self):
# Hoisting both filters to fq would require red AND blue instead of red OR blue.
body = {"query": {"bool": {"should": [
{"bool": {"filter": [{"term": {"color": "red"}}]}},
{"bool": {"filter": [{"term": {"color": "blue"}}]}},
]}}}
result = translate_to_solr_json_dsl(body)
self.assertNotIn("filter", result)
self.assertEqual("((+(color:red)) (+(color:blue)))", result["query"])

def test_nested_bool_under_should_keeps_its_grouping(self):
# (a AND b) OR c, not a AND b AND c
body = {"query": {"bool": {"should": [
{"bool": {"must": [{"term": {"a": 1}}, {"term": {"b": 2}}]}},
{"term": {"c": 3}},
]}}}
self.assertEqual("((+(a:1) +(b:2)) c:3)", translate_to_solr_json_dsl(body)["query"])

def test_filter_under_must_still_goes_to_fq(self):
body = {"query": {"bool": {"must": [{"bool": {"filter": [{"term": {"color": "red"}}]}}]}}}
self.assertEqual({"query": "*:*", "filter": ["color:red"]}, translate_to_solr_json_dsl(body))

def test_sort_is_extracted(self):
body = {"query": {"match_all": {}}, "sort": [{"fare_amount": "desc"}]}
result = translate_to_solr_json_dsl(body)
Expand Down
Loading