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
25 changes: 25 additions & 0 deletions solrorbit/conversion/workload_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,26 @@ def replace(match):
return _MERGE_WAIT_OBJECT.sub(replace, text)


def _convert_scroll(op: dict) -> None:
"""
Rewrite a search with ``pages`` as a ``paginated-search``, which pages with Solr's cursorMark.

The query becomes ``q`` (and its filters ``fq``); no ``fl`` is set, so every stored field comes back as OpenSearch
returns ``_source``. A cursor needs a sort ending with the uniqueKey: upstream's sort gets ``id asc`` appended, and a
search without one is sorted by ``id asc``. OpenSearch's scroll orders by score and then index order, which a cursor
cannot use (``_docid_`` is refused).
"""
body = op.pop("body")
translated = translate_to_solr_json_dsl(body)
op["operation-type"] = "paginated-search"
op["q"] = translated["query"]
if translated.get("filter"):
op["fq"] = translated["filter"]
sort = translated.get("sort")
op["sort"] = "id asc" if not sort else (sort if re.search(r"(^|,)\s*id\s+(asc|desc)\s*$", sort) else sort + ",id asc")
Comment on lines +762 to +769
if "index" in op:
op["collection"] = op.pop("index")

def _object_spans(text: str) -> dict:
"""Start -> end of every JSON object in *text*, skipping strings and Jinja2 tags ({{ }}, {% %}, {# #})."""
spans, stack, i, n = {}, [], 0, len(text)
Expand Down Expand Up @@ -898,6 +918,11 @@ def _convert_operation(op, issues, skipped, source_dir, output_dir):
if setup:
op.update(setup)

# A search paged with pages / results-per-page is OpenSearch Benchmark's scroll: Solr pages with a cursor
if op_type == "search" and "pages" in op and _is_opensearch_body(op.get("body")):
_convert_scroll(op)
return True
Comment on lines +922 to +924

# Translate search body from OpenSearch DSL to Solr JSON DSL
if op_type in ("search", "paginated-search", "scroll-search"):
body = op.get("body")
Expand Down
4 changes: 3 additions & 1 deletion solrorbit/workload/params.py
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ def __init__(self, workload, params, **kwargs):

class SolrOptimizeParamSource(ParamSource):
"""
Param source for Solr optimize, commit and refresh operations.
Param source for Solr optimize, commit, refresh and paginated-search operations.

Resolves the target collection via get_target() when not explicitly specified,
mirroring Solr Orbit's default-index mechanism.
Expand All @@ -1206,3 +1206,5 @@ def params(self):
register_param_source_for_name("optimize", SolrOptimizeParamSource)
register_param_source_for_name("commit", SolrOptimizeParamSource)
register_param_source_for_name("refresh", SolrOptimizeParamSource)
register_param_source_for_name("paginated-search", SolrOptimizeParamSource)
register_param_source_for_name("scroll-search", SolrOptimizeParamSource)
31 changes: 31 additions & 0 deletions tests/unit/solr/test_workload_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,3 +1095,34 @@ def test_with_several_collections_the_setup_steps_stay_bare(self):
out, _ = self._convert_with_setup_steps(["a", "b"], '{"settings": {}}')
self.assertEqual("delete-collection", json.loads(out["common_operations/delete_index.json"])["operation"])
self.assertIn('"operation": "refresh"', out["test_procedures/default.json"])


class TestScrollConversion(unittest.TestCase):
"""A search with pages, OpenSearch Benchmark's scroll, becomes a paginated-search that pages with a cursor."""

def _op(self, **extra):
op = dict({"name": "scroll", "operation-type": "search", "pages": 25, "results-per-page": 1000,
"body": {"query": {"match_all": {}}}}, **extra)
_convert_operation(op, [], [], "", "")
return op

def test_a_scroll_over_match_all(self):
self.assertEqual({"name": "scroll", "operation-type": "paginated-search", "pages": 25, "results-per-page": 1000,
"q": "*:*", "sort": "id asc"}, self._op())

def test_no_fl_so_whole_documents_come_back(self):
self.assertNotIn("fl", self._op())

def test_upstream_sort_and_filters_are_kept_with_the_key_last(self):
op = self._op(body={"query": {"bool": {"filter": [{"range": {"population": {"gte": 5}}}]}}, "sort": [{"population": "desc"}]})
self.assertEqual("population desc,id asc", op["sort"])
self.assertEqual(["population:[5 TO *]"], op["fq"])

def test_a_sort_already_ending_with_the_key_is_left_alone(self):
self.assertEqual("id desc", self._op(body={"query": {"match_all": {}}, "sort": [{"id": "desc"}]})["sort"])

def test_a_search_without_pages_is_not_a_scroll(self):
op = {"name": "s", "operation-type": "search", "body": {"query": {"match_all": {}}}}
_convert_operation(op, [], [], "", "")
self.assertEqual("search", op["operation-type"])

7 changes: 7 additions & 0 deletions tests/workload/params_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1802,6 +1802,13 @@ def test_bare_refresh_and_commit_target_the_workload_collection(self):
p = params.param_source_for_operation(op_type, wl, {}, op_type).params()
self.assertEqual("my-col", p["collection"], op_type)

def test_a_paginated_search_without_a_collection_targets_the_workload_collection(self):
wl = workload.Workload(name="unit-test", collections=[workload.Collection(name="my-col")])
for op_type in ("paginated-search", "scroll-search"):
p = params.param_source_for_operation(op_type, wl, {"q": "*:*", "pages": 2}, op_type).params()
self.assertEqual("my-col", p["collection"], op_type)
self.assertEqual(2, p["pages"], op_type)

def test_a_stated_collection_is_kept(self):
wl = workload.Workload(name="unit-test", collections=[workload.Collection(name="my-col")])
p = params.param_source_for_operation("refresh", wl, {"collection": "other"}, "refresh").params()
Expand Down
Loading