Skip to content

Support 'AS' on fields and on RETURN #135

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

Merged
merged 13 commits into from
Jul 22, 2021
11 changes: 9 additions & 2 deletions redisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ class Field(object):
TAG = 'TAG'
SORTABLE = 'SORTABLE'
NOINDEX = 'NOINDEX'
AS = 'AS'

def __init__(self, name, args=[], sortable=False, no_index=False):
def __init__(self, name, args=[], sortable=False, no_index=False, as_name=None):
self.name = name
self.args = args
self.args_suffix = list()
self.as_name = as_name

if sortable:
self.args_suffix.append(Field.SORTABLE)
Expand All @@ -38,7 +40,12 @@ def append_arg(self, value):
self.args.append(value)

def redis_args(self):
return [self.name] + self.args + self.args_suffix
args = [self.name]
if self.as_name:
args += [self.AS, self.as_name]
args += self.args
args += self.args_suffix
return args


class TextField(Field):
Expand Down
13 changes: 11 additions & 2 deletions redisearch/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,18 @@ def limit_ids(self, *ids):

def return_fields(self, *fields):
"""
Only return values from these fields
Add fields to return fields
"""
self._return_fields = fields
self._return_fields += fields
return self

def return_field(self, field, as_field=None):
"""
Add field to return fields (Optional: add 'AS' name to the field)
"""
self._return_fields.append(field)
if as_field is not None:
self._return_fields += ("AS", as_field)
return self

def _mk_field_list(self, fields):
Expand Down
61 changes: 61 additions & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,67 @@ def testCreateClientDefinitionJson(self):
self.assertEqual(res.docs[0].json, '{"name":"henry"}')
self.assertEqual(res.total, 1)

def testFieldsAsName(self):
conn = self.redis()

with conn as r:
r.flushdb()
if not check_version(r, 20200):
return

# create index
SCHEMA = (
TextField("$.name", sortable=True, as_name='name'),
NumericField("$.age", as_name='just_a_number'),
)
definition = IndexDefinition(index_type=IndexType.JSON)
json_client = Client('idxJson')
json_client.create_index(SCHEMA, definition=definition)

# insert json data
rj = rejson.Client(host='localhost', port=conn.port, decode_responses=True)
res = rj.jsonset('doc:1', rejson.Path.rootPath(), {'name': 'Jon', 'age': 25})
self.assertTrue(res)

total = json_client.search(Query('Jon').return_fields('name', 'just_a_number')).docs
self.assertEqual(1, len(total))
self.assertEqual('doc:1', total[0].id)
self.assertEqual('Jon', total[0].name)
self.assertEqual('25', total[0].just_a_number)

def testSearchReturnFields(self):
conn = self.redis()

with conn as r:
r.flushdb()
if not check_version(r, 20200):
return

# insert json data
rj = rejson.Client(host='localhost', port=conn.port, decode_responses=True)
res = rj.jsonset('doc:1', rejson.Path.rootPath(),
{"t": "riceratops", "t2": "telmatosaurus", "n": 9072, "flt": 97.2})
self.assertTrue(res)

# create index json
definition = IndexDefinition(index_type=IndexType.JSON)
SCHEMA = (
TextField("$.t"),
NumericField("$.flt"),
)
json_client = Client('idxJson')
json_client.create_index(SCHEMA, definition=definition)

total = json_client.search(Query('*').return_field("$.t", as_field="txt")).docs
self.assertEqual(1, len(total))
self.assertEqual('doc:1', total[0].id)
self.assertEqual('riceratops', total[0].txt)

total = json_client.search(Query('*').return_field("$.t2", as_field="txt")).docs
self.assertEqual(1, len(total))
self.assertEqual('doc:1', total[0].id)
self.assertEqual('telmatosaurus', total[0].txt)


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