Gitlab Community Edition Instance

search.py 1.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
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))