Gitlab Community Edition Instance
Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
cdstar
pycdstar3
Commits
f6e5d308
Commit
f6e5d308
authored
Oct 26, 2019
by
Marcel Hellkamp
Browse files
Added (and fixed) search command.
parent
661ea598
Changes
2
Show whitespace changes
Inline
Side-by-side
src/pycdstar3/api.py
View file @
f6e5d308
...
@@ -316,16 +316,16 @@ class CDStar:
...
@@ -316,16 +316,16 @@ class CDStar:
See iter_search() for a convenient way to fetch more than `limit` results.
See iter_search() for a convenient way to fetch more than `limit` results.
"""
"""
query
=
{
"q"
:
q
}
params
=
{
"q"
:
q
}
if
order
:
if
order
:
query
[
'order'
]
=
order
params
[
'order'
]
=
order
if
limit
:
if
limit
:
query
[
'limit'
]
=
limit
params
[
'limit'
]
=
limit
if
scroll
:
if
scroll
:
query
[
'scroll'
]
=
scroll
params
[
'scroll'
]
=
scroll
if
groups
:
if
groups
:
query
[
'groups'
]
=
groups
params
[
'groups'
]
=
groups
return
self
.
rest
(
"GET"
,
vault
,
query
=
query
)
return
self
.
rest
(
"GET"
,
vault
,
params
=
params
)
def
iter_search
(
self
,
vault
,
q
,
scroll
=
None
,
**
args
)
->
typing
.
Iterator
[
JsonObject
]:
def
iter_search
(
self
,
vault
,
q
,
scroll
=
None
,
**
args
)
->
typing
.
Iterator
[
JsonObject
]:
""" Yield all search hits of a search.
""" Yield all search hits of a search.
...
@@ -333,10 +333,10 @@ class CDStar:
...
@@ -333,10 +333,10 @@ class CDStar:
This method may (lazily) issue more than one request if a search returns more than `limit` results.
This method may (lazily) issue more than one request if a search returns more than `limit` results.
"""
"""
while
True
:
while
True
:
hits
=
self
.
search
(
vault
,
q
,
scroll
=
scroll
or
""
,
**
args
)
page
=
self
.
search
(
vault
,
q
,
scroll
=
scroll
or
""
,
**
args
)
if
hits
[
'hits'
]:
if
page
[
'hits'
]:
yield
from
hits
[
'hits'
]
yield
from
page
[
'hits'
]
scroll
=
hits
[
'scroll'
]
scroll
=
page
[
'scroll'
]
else
:
else
:
break
break
...
...
src/pycdstar3/cli/commands/search.py
0 → 100644
View file @
f6e5d308
"""
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
))
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment