From 82898e56650c31a008c0ddf0ec2885d491119d42 Mon Sep 17 00:00:00 2001 From: yim7 Date: Mon, 26 Sep 2022 22:24:14 +0800 Subject: [PATCH] feat: add fetch field api --- opensearchorm/session.py | 24 +++++++++++++----------- pyproject.toml | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/opensearchorm/session.py b/opensearchorm/session.py index a416ca4..0820090 100644 --- a/opensearchorm/session.py +++ b/opensearchorm/session.py @@ -55,7 +55,6 @@ class QueryExecutor(Generic[Model]): def __init__(self, model_cls: Type[Model], session: SearchSession): self.__query = ModelQuery(model_cls) self.__model_cls = model_cls - self.__include_fields = [] self.__limit: Optional[int] = None self.__offset: Optional[int] = None self.__session = session @@ -80,12 +79,10 @@ class QueryExecutor(Generic[Model]): self.__offset = offset return self - def values(self, fields: List[str]): - self.__include_fields = fields - return self - - def fetch(self, **kwargs): + def fetch_fields(self, fields: List[str], **kwargs): """ + :arg fields: include source fields + :arg kwargs: any additional arguments will be passed on to the opensearch-py call """ @@ -102,16 +99,21 @@ class QueryExecutor(Generic[Model]): index=model.__index__, size=self.__limit, from_=self.__offset, - _source_includes=self.__include_fields or model.default_fields(), + _source_includes=fields, **kwargs, ) hits = resp['hits']['hits'] logging.debug('raw result: %s', hits) - if self.__include_fields: - return [hit['_source'] for hit in hits] - else: - return [model.parse_obj(hit['_source']) for hit in hits] + return [hit['_source'] for hit in hits] + + def fetch(self, **kwargs): + """ + :arg kwargs: any additional arguments will be passed on to the opensearch-py call + """ + model = self.__model_cls + hits = self.fetch_fields(model.default_fields()) + return [model.parse_obj(hit['_source']) for hit in hits] def scroll(self, **kwargs): ... diff --git a/pyproject.toml b/pyproject.toml index ff34792..beaeaac 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "opensearch-orm" -version = "0.1.4" +version = "0.1.5" description = "" authors = ["yim7 "] readme = "README.md"