forked from kuzzleio/kourou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.ts
76 lines (66 loc) · 2.18 KB
/
search.ts
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { flags } from '@oclif/command'
import { Kommand } from '../../common'
import { kuzzleFlags } from '../../support/kuzzle'
export default class DocumentSearch extends Kommand {
static description = 'Searches for documents'
static examples = [
'kourou document:search iot sensors \'{ equals: { name: "corona" } }\'',
'kourou document:search iot sensors \'{ match: { name: "cOrOnna" } }\' -a lang=elasticsearch',
'kourou document:search iot sensors --editor',
]
static flags = {
sort: flags.string({
description: 'Sort in JS or JSON format.',
default: '{}'
}),
from: flags.string({
description: 'Optional offset'
}),
size: flags.string({
description: 'Optional page size',
}),
scroll: flags.string({
description: 'Optional scroll TTL'
}),
lang: flags.string({
description: 'Specify the query language to use',
default: 'koncorde'
}),
editor: flags.boolean({
description: 'Open an editor (EDITOR env variable) to edit the request before sending'
}),
help: flags.help(),
...kuzzleFlags
}
static args = [
{ name: 'index', description: 'Index name', required: true },
{ name: 'collection', description: 'Collection name', required: true },
{ name: 'query', description: 'Search query in JS or JSON format.' },
]
async runSafe() {
let request: any = {
controller: 'document',
action: 'search',
index: this.args.index,
collection: this.args.collection,
from: this.flags.from,
size: this.flags.size,
scroll: this.flags.scroll,
lang: this.flags.lang,
body: {
query: this.parseJs(this.args.query || '{}'),
sort: this.parseJs(this.flags.sort)
}
}
// allow to edit request before send
if (this.flags.editor) {
request = this.fromEditor(request, { json: true })
}
const { result } = await this.sdk.query(request)
for (const document of result?.hits) {
this.logInfo(`Document ID: ${document._id}`)
this.log(`Content: ${JSON.stringify(document._source, null, 2)}`)
}
this.logOk(`${result?.hits.length} documents fetched on a total of ${result?.total}`)
}
}