Skip to content

Commit a79151b

Browse files
Support 'AS' on fields and on RETURN (#135)
* Support AS with RETURN fileds Co-authored-by: Chayim <[email protected]>
1 parent 5787432 commit a79151b

File tree

3 files changed

+81
-4
lines changed

3 files changed

+81
-4
lines changed

redisearch/client.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ class Field(object):
2020
TAG = 'TAG'
2121
SORTABLE = 'SORTABLE'
2222
NOINDEX = 'NOINDEX'
23+
AS = 'AS'
2324

24-
def __init__(self, name, args=[], sortable=False, no_index=False):
25+
def __init__(self, name, args=[], sortable=False, no_index=False, as_name=None):
2526
self.name = name
2627
self.args = args
2728
self.args_suffix = list()
29+
self.as_name = as_name
2830

2931
if sortable:
3032
self.args_suffix.append(Field.SORTABLE)
@@ -38,7 +40,12 @@ def append_arg(self, value):
3840
self.args.append(value)
3941

4042
def redis_args(self):
41-
return [self.name] + self.args + self.args_suffix
43+
args = [self.name]
44+
if self.as_name:
45+
args += [self.AS, self.as_name]
46+
args += self.args
47+
args += self.args_suffix
48+
return args
4249

4350

4451
class TextField(Field):

redisearch/query.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,18 @@ def limit_ids(self, *ids):
5050

5151
def return_fields(self, *fields):
5252
"""
53-
Only return values from these fields
53+
Add fields to return fields
5454
"""
55-
self._return_fields = fields
55+
self._return_fields += fields
56+
return self
57+
58+
def return_field(self, field, as_field=None):
59+
"""
60+
Add field to return fields (Optional: add 'AS' name to the field)
61+
"""
62+
self._return_fields.append(field)
63+
if as_field is not None:
64+
self._return_fields += ("AS", as_field)
5665
return self
5766

5867
def _mk_field_list(self, fields):

test/test.py

+61
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,67 @@ def testCreateClientDefinitionJson(self):
10521052
self.assertEqual(res.docs[0].json, '{"name":"henry"}')
10531053
self.assertEqual(res.total, 1)
10541054

1055+
def testFieldsAsName(self):
1056+
conn = self.redis()
1057+
1058+
with conn as r:
1059+
r.flushdb()
1060+
if not check_version(r, 20200):
1061+
return
1062+
1063+
# create index
1064+
SCHEMA = (
1065+
TextField("$.name", sortable=True, as_name='name'),
1066+
NumericField("$.age", as_name='just_a_number'),
1067+
)
1068+
definition = IndexDefinition(index_type=IndexType.JSON)
1069+
json_client = Client('idxJson')
1070+
json_client.create_index(SCHEMA, definition=definition)
1071+
1072+
# insert json data
1073+
rj = rejson.Client(host='localhost', port=conn.port, decode_responses=True)
1074+
res = rj.jsonset('doc:1', rejson.Path.rootPath(), {'name': 'Jon', 'age': 25})
1075+
self.assertTrue(res)
1076+
1077+
total = json_client.search(Query('Jon').return_fields('name', 'just_a_number')).docs
1078+
self.assertEqual(1, len(total))
1079+
self.assertEqual('doc:1', total[0].id)
1080+
self.assertEqual('Jon', total[0].name)
1081+
self.assertEqual('25', total[0].just_a_number)
1082+
1083+
def testSearchReturnFields(self):
1084+
conn = self.redis()
1085+
1086+
with conn as r:
1087+
r.flushdb()
1088+
if not check_version(r, 20200):
1089+
return
1090+
1091+
# insert json data
1092+
rj = rejson.Client(host='localhost', port=conn.port, decode_responses=True)
1093+
res = rj.jsonset('doc:1', rejson.Path.rootPath(),
1094+
{"t": "riceratops", "t2": "telmatosaurus", "n": 9072, "flt": 97.2})
1095+
self.assertTrue(res)
1096+
1097+
# create index json
1098+
definition = IndexDefinition(index_type=IndexType.JSON)
1099+
SCHEMA = (
1100+
TextField("$.t"),
1101+
NumericField("$.flt"),
1102+
)
1103+
json_client = Client('idxJson')
1104+
json_client.create_index(SCHEMA, definition=definition)
1105+
1106+
total = json_client.search(Query('*').return_field("$.t", as_field="txt")).docs
1107+
self.assertEqual(1, len(total))
1108+
self.assertEqual('doc:1', total[0].id)
1109+
self.assertEqual('riceratops', total[0].txt)
1110+
1111+
total = json_client.search(Query('*').return_field("$.t2", as_field="txt")).docs
1112+
self.assertEqual(1, len(total))
1113+
self.assertEqual('doc:1', total[0].id)
1114+
self.assertEqual('telmatosaurus', total[0].txt)
1115+
10551116

10561117
if __name__ == '__main__':
10571118
unittest.main()

0 commit comments

Comments
 (0)