diff --git a/esxport/click_opt/click_custom.py b/esxport/click_opt/click_custom.py index f0fad84..c1e0e05 100644 --- a/esxport/click_opt/click_custom.py +++ b/esxport/click_opt/click_custom.py @@ -52,7 +52,7 @@ class Json(ParamType): def convert(self: Self, value: Any, param: Parameter | None, ctx: Context | None) -> dict[str, Any]: """Convert input to json.""" try: - return json.loads(value) # type: ignore[no-any-return] + return value if isinstance(value, dict) else json.loads(value) # type: ignore[no-any-return] except json.JSONDecodeError as exc: self.fail(invalid_query_format.format(value=value, exc=exc), param, ctx) diff --git a/esxport/esxport.py b/esxport/esxport.py index f32cf23..c32a51a 100644 --- a/esxport/esxport.py +++ b/esxport/esxport.py @@ -16,6 +16,8 @@ from esxport.strings import index_not_found, meta_field_not_found, output_fields, sorting_by, using_indexes, using_query from esxport.writer import Writer +from .click_opt.click_custom import Json + if TYPE_CHECKING: from typing_extensions import Self @@ -87,7 +89,7 @@ def _prepare_search_query(self: Self) -> None: "scroll": self.scroll_time, "size": self.opts.scroll_size, "terminate_after": self.opts.max_results, - "body": self.opts.query, + "query": Json().convert(self.opts.query, None, None)["query"], } if self.opts.sort: self.search_args["sort"] = self.opts.sort @@ -97,7 +99,7 @@ def _prepare_search_query(self: Self) -> None: if self.opts.debug: logger.debug(using_indexes.format(indexes={", ".join(self.opts.index_prefixes)})) - query = json.dumps(self.opts.query) + query = json.dumps(self.opts.query, default=str) logger.debug(using_query.format(query={query})) logger.debug(output_fields.format(fields={", ".join(self.opts.fields)})) logger.debug(sorting_by.format(sort=self.opts.sort)) diff --git a/test/esxport/_prepare_search_query_test.py b/test/esxport/_prepare_search_query_test.py index 704dac8..1bf9444 100644 --- a/test/esxport/_prepare_search_query_test.py +++ b/test/esxport/_prepare_search_query_test.py @@ -93,9 +93,10 @@ def test_query(self: Self, _: Any, esxport_obj: EsXport) -> None: """ expected_query: dict[str, Any] = {"query": {"match_all": {}}} esxport_obj.opts.query = expected_query + actual_query = expected_query["query"] esxport_obj._prepare_search_query() - assert esxport_obj.search_args["body"] == expected_query + assert esxport_obj.search_args["query"] == actual_query def test_terminate_after(self: Self, _: Any, esxport_obj: EsXport) -> None: """Arr, matey!.