Skip to content

Commit dce2801

Browse files
Add support for --fields parameter in search command
--------- Co-authored-by: carlevison <[email protected]>
1 parent 7821773 commit dce2801

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

cloudinary_cli/core/search.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717
e.g. cld search cat AND tags:kitten -s public_id desc -f context -f tags -n 10
1818
""")
1919
@argument("query", nargs=-1)
20-
@option("-f", "--with_field", multiple=True, help="Specify which asset attribute to include in the result.")
20+
@option("-f", "--with_field", multiple=True, help="Specify which non-default asset attributes to include "
21+
"in the result as a comma separated list. ")
22+
@option("-fi", "--fields", multiple=True, help="Specify which asset attributes to include in the result "
23+
"(together with a subset of the default attributes) as a comma separated"
24+
" list. This overrides any value specified for with_field.")
2125
@option("-s", "--sort_by", nargs=2, help="Sort search results by (field, <asc|desc>).")
2226
@option("-a", "--aggregate", nargs=1,
2327
help="Specify the attribute for which an aggregation count should be calculated and returned.")
@@ -26,28 +30,31 @@
2630
@option("-c", "--next_cursor", nargs=1, help="Continue a search using an existing cursor.")
2731
@option("-A", "--auto_paginate", is_flag=True, help="Return all results. Will call Admin API multiple times.")
2832
@option("-F", "--force", is_flag=True, help="Skip confirmation when running --auto-paginate.")
29-
@option("-ff", "--filter_fields", multiple=True, help="Filter fields to return.")
33+
@option("-ff", "--filter_fields", multiple=True, help="Specify which attributes to show in the response. "
34+
"None of the others will be shown.")
3035
@option("-t", "--ttl", nargs=1, default=300, help="Set the Search URL TTL in seconds. Default: 300.")
3136
@option("-u", "--url", is_flag=True, help="Build a signed search URL.")
37+
@option("-sq", "--search-query", is_flag=True, help="Show the search request query.", hidden=True)
3238
@option("--json", nargs=1, help="Save JSON output to a file. Usage: --json <filename>")
3339
@option("--csv", nargs=1, help="Save CSV output to a file. Usage: --csv <filename>")
3440
@option("-d", "--doc", is_flag=True, help="Open Search API documentation page.")
35-
def search(query, with_field, sort_by, aggregate, max_results, next_cursor,
36-
auto_paginate, force, filter_fields, ttl, url, json, csv, doc):
41+
def search(query, with_field, fields, sort_by, aggregate, max_results, next_cursor,
42+
auto_paginate, force, filter_fields, ttl, url, search_query, json, csv, doc):
3743
if doc:
3844
return launch("https://cloudinary.com/documentation/search_api")
3945

4046
fields_to_keep = []
4147
if filter_fields:
42-
fields_to_keep = tuple(normalize_list_params(filter_fields)) + with_field
48+
fields_to_keep = tuple(normalize_list_params(filter_fields)) + tuple(normalize_list_params(with_field))
4349

4450
search = cloudinary.search.Search().expression(" ".join(query))
4551

4652
if auto_paginate:
4753
max_results = DEFAULT_MAX_RESULTS
4854
if with_field:
49-
for f in with_field:
50-
search.with_field(f)
55+
search.with_field(normalize_list_params(with_field))
56+
if fields:
57+
search.fields(normalize_list_params(fields))
5158
if sort_by:
5259
search.sort_by(*sort_by)
5360
if aggregate:
@@ -63,6 +70,10 @@ def search(query, with_field, sort_by, aggregate, max_results, next_cursor,
6370
print(search.to_url())
6471
return True
6572

73+
if search_query:
74+
print_json(search.as_dict())
75+
return True
76+
6677
res = execute_single_request(search, fields_to_keep)
6778

6879
if auto_paginate:

test/test_cli_search_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def test_search(self, mocker):
2121
self.assertEqual(0, result.exit_code)
2222
self.assertIn('"foo": "bar"', result.output)
2323

24+
def test_search_fields(self):
25+
result = self.runner.invoke(cli, ['search', 'cat', '-fi', 'url,tags', '-fi', 'context', '--search-query'])
26+
27+
self.assertEqual(0, result.exit_code)
28+
self.assertIn('"fields": [\n "url",\n "tags",\n "context"\n ]', result.output)
29+
2430
def test_search_url(self):
2531
result = self.runner.invoke(cli, ['search', 'cat', '-c', 'NEXT_CURSOR', '--ttl', '1000', '--url'])
2632

0 commit comments

Comments
 (0)