Skip to content
Merged
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
13 changes: 11 additions & 2 deletions solrorbit/conversion/workload_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,15 @@ def replace(match):
return _MERGE_WAIT_OBJECT.sub(replace, text)


def _is_opensearch_body(body):
"""A search body written in OpenSearch DSL: a dict whose `query`, if it has one, is an object.

A body with no `query` at all (only `aggs`, or only `size`) is still OpenSearch DSL and must be
translated; a `query` that is already a string is Solr's and is left alone.
"""
return isinstance(body, dict) and (("query" not in body) or isinstance(body["query"], dict))


def _convert_operation(op, issues, skipped, source_dir, output_dir):
"""Convert an operation definition dict in-place.

Expand Down Expand Up @@ -695,7 +704,7 @@ def _convert_operation(op, issues, skipped, source_dir, output_dir):
# Translate search body from OpenSearch DSL to Solr JSON DSL
if op_type in ("search", "paginated-search", "scroll-search"):
body = op.get("body")
if isinstance(body, dict) and isinstance(body.get("query"), dict):
if _is_opensearch_body(body):
try:
op["body"] = translate_to_solr_json_dsl(body)
except Exception as exc:
Expand All @@ -709,7 +718,7 @@ def _convert_operation(op, issues, skipped, source_dir, output_dir):
try:
with open(body_abs, encoding="utf-8") as f:
body_dict = json.load(f)
if isinstance(body_dict.get("query"), dict):
if _is_opensearch_body(body_dict):
converted = translate_to_solr_json_dsl(body_dict)
out_body_dir = os.path.join(output_dir, os.path.dirname(body_file))
os.makedirs(out_body_dir, exist_ok=True)
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/solr/test_workload_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,28 @@ def test_calendar_month_abbreviation_is_not_a_minute(self):
self.assertEqual("+1MONTH", gap)


class TestSearchBodiesWithoutAQuery(unittest.TestCase):
def _convert(self, body):
op = {"name": "country_agg", "operation-type": "search", "body": body}
_convert_operation(op, [], [], "", "")
return op["body"]

def test_aggs_only_body_is_translated(self):
body = self._convert({"size": 0, "aggs": {"by_country": {"terms": {"field": "country_code.raw"},
"aggs": {"total": {"sum": {"field": "population"}}}}}})
self.assertEqual("*:*", body["query"])
self.assertEqual(0, body["limit"])
self.assertEqual({"type": "terms", "field": "country_code_raw", "limit": 10, "facet": {"total": "sum(population)"}},
body["facet"]["by_country"])

def test_size_only_body_is_translated(self):
self.assertEqual({"query": "*:*", "limit": 5}, self._convert({"size": 5}))

def test_body_that_already_holds_a_solr_query_string_is_left_alone(self):
body = {"query": "country_code:NL", "limit": 5}
self.assertEqual(body, self._convert(dict(body)))


class TestMergeWaitConversion(unittest.TestCase):
"""Tests for OpenSearch's index-stats merge wait becoming Solr's wait-for-merges."""

Expand Down
Loading