-
Notifications
You must be signed in to change notification settings - Fork 123
added return_fields function, attempting to optionally limit fields r… #633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -423,6 +423,7 @@ def __init__( | |
limit: Optional[int] = None, | ||
page_size: int = DEFAULT_PAGE_SIZE, | ||
sort_fields: Optional[List[str]] = None, | ||
return_fields: Optional[List[str]] = None, | ||
nocontent: bool = False, | ||
): | ||
if not has_redisearch(model.db()): | ||
|
@@ -447,6 +448,11 @@ def __init__( | |
else: | ||
self.sort_fields = [] | ||
|
||
if return_fields: | ||
self.return_fields = self.validate_return_fields(return_fields) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since |
||
else: | ||
self.return_fields = [] | ||
|
||
self._expression = None | ||
self._query: Optional[str] = None | ||
self._pagination: List[str] = [] | ||
|
@@ -504,8 +510,19 @@ def query(self): | |
if self._query.startswith("(") or self._query == "*" | ||
else f"({self._query})" | ||
) + f"=>[{self.knn}]" | ||
if self.return_fields: | ||
self._query += f" RETURN {','.join(self.return_fields)}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sorry, what are you going to do with deserialization? I mean https://github.com/redis/redis-om-python/blob/main/aredis_om/model/model.py#L1583 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me this works
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO you'd return a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm talking about this behavior: I get the output like JSON document And here are right data types (id and page id are integers) But when I use RETURN I get the output like this
So my question is how are you going to deserialize this results? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My fault guys, I've turned it off, very expensive to run validations on read queries There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But please check if it works with dict fields, I get error "value is not a valid dict" while processing '{"foo": "bar"}' |
||
return self._query | ||
|
||
def validate_return_fields(self, return_fields: List[str]): | ||
for field in return_fields: | ||
if field not in self.model.__fields__: # type: ignore | ||
raise QueryNotSupportedError( | ||
f"You tried to return the field {field}, but that field " | ||
f"does not exist on the model {self.model}" | ||
) | ||
return return_fields | ||
|
||
@property | ||
def query_params(self): | ||
params: List[Union[str, bytes]] = [] | ||
|
@@ -967,6 +984,11 @@ def sort_by(self, *fields: str): | |
if not fields: | ||
return self | ||
return self.copy(sort_fields=list(fields)) | ||
|
||
def return_fields(self, *fields: str): | ||
if not fields: | ||
return self | ||
return self.copy(return_fields=list(fields)) | ||
|
||
async def update(self, use_transaction=True, **field_values): | ||
""" | ||
|
@@ -1553,7 +1575,9 @@ def find( | |
*expressions: Union[Any, Expression], | ||
knn: Optional[KNNExpression] = None, | ||
) -> FindQuery: | ||
return FindQuery(expressions=expressions, knn=knn, model=cls) | ||
return FindQuery( | ||
expressions=expressions, knn=knn, model=cls | ||
) | ||
|
||
@classmethod | ||
def from_redis(cls, res: Any): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -950,9 +950,21 @@ class TypeWithUuid(JsonModel): | |
|
||
await item.save() | ||
|
||
@py_test_mark_asyncio | ||
async def test_return_specified_fields(members, m): | ||
member1, member2, member3 = members | ||
actual = await m.Member.find( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One issue to contend with here is that the model validation will not work correctly when running find, you'll need to make sure you return a dictionary or some sub-set of the model to prevent validation errors from being tossed by the find. |
||
(m.Member.first_name == "Andrew") & (m.Member.last_name == "Brookins") | ||
| (m.Member.last_name == "Smith") | ||
).all() | ||
assert actual == [ | ||
{"first_name": "Andrew", "last_name": "Brookins"}, | ||
{"first_name": "Andrew", "last_name": "Smith"}, | ||
] | ||
|
||
|
||
@py_test_mark_asyncio | ||
async def test_xfix_queries(m): | ||
async def test_xfix_queries(m):4 | ||
await m.Member( | ||
first_name="Steve", | ||
last_name="Lorello", | ||
|
Uh oh!
There was an error while loading. Please reload this page.