Commit f6e5d308 authored by Marcel Hellkamp's avatar Marcel Hellkamp
Browse files

Added (and fixed) search command.

parent 661ea598
......@@ -316,16 +316,16 @@ class CDStar:
See iter_search() for a convenient way to fetch more than `limit` results.
"""
query = {"q": q}
params = {"q": q}
if order:
query['order'] = order
params['order'] = order
if limit:
query['limit'] = limit
params['limit'] = limit
if scroll:
query['scroll'] = scroll
params['scroll'] = scroll
if groups:
query['groups'] = groups
return self.rest("GET", vault, query=query)
params['groups'] = groups
return self.rest("GET", vault, params=params)
def iter_search(self, vault, q, scroll=None, **args) -> typing.Iterator[JsonObject]:
""" Yield all search hits of a search.
......@@ -333,10 +333,10 @@ class CDStar:
This method may (lazily) issue more than one request if a search returns more than `limit` results.
"""
while True:
hits = self.search(vault, q, scroll=scroll or "", **args)
if hits['hits']:
yield from hits['hits']
scroll = hits['scroll']
page = self.search(vault, q, scroll=scroll or "", **args)
if page['hits']:
yield from page['hits']
scroll = page['scroll']
else:
break
......
"""
Search a vault.
Query syntax and query-able index fields depend on the installed
search-backend and configuration. Search might be disabled or restricted
on your CDSTAR server instance. Please refer to your instance documentation
for details.
"""
def register(subparsers):
parser = subparsers.add_parser("search",
help=__doc__.strip().splitlines()[0],
description=__doc__)
parser.add_argument("--limit", type=int, default=25, help="Show this many results (default: 25)")
parser.add_argument("--order", action="append", help="Order by index field name. Prefix with '-' to reverse"
" ordering. Multiple (default: -score)")
parser.add_argument("--no-scroll", action="store_true",
help="Disables auto-fetching more results if less than --limit hits were returned.")
parser.add_argument("QUERY", help="Search query. Syntax depends on back-end configuration.")
parser.set_defaults(main=search)
def search(ctx, args):
client = ctx.client
vault = ctx.vault
limit = max(1, args.limit)
order = args.order or ["-score"]
query = args.QUERY
scroll = ""
found = 0
page = None
while limit > found:
page = client.search(vault, query, limit=limit - found, order=order, scroll=scroll)
if not page.hits:
break # no more results
for hit in page.hits[:limit - found] if page.hits else []:
found += 1
if hit.type == 'archive':
print("{}".format(hit.id))
elif hit.type == 'file':
print("{}\t{}".format(hit.id, hit.name))
if args.no_scroll:
break
scroll = page.scroll
ctx.print("Total results: {} ({} shown)".format(page.total, found))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment