-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmine.js
58 lines (58 loc) · 1.58 KB
/
mine.js
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
export class ESQuery {
constructor(url, size, query) {
this.store = {};
this.store.results = [];
this.store.errors = [];
this.url = url;
this.size = size;
this.query = {
"query": {
"query_string": {
"query": `${query}`
}
},
"size": 1000,
"sort": [
{
"insert_time": {
"order": "desc"
}
}]
};
}
async search(index) {
try {
const resp = await fetch(this.url + index + '/_search', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(this.query)
});
const status = resp.status;
const data = await resp.json();
if (status !== 200) {
if (status === 400) {
this.store.errors.push('Bad Request');
}
console.log(data, "Error");
}
if (data && data.hits && data.hits.hits) {
this.store.results = data.hits.hits;
}
} catch (error) {
this.store.errors.push(error);
console.log(error);
}
}
getResults(limit) {
return this.store.results.slice(0, limit);
}
getErrors() {
return this.store.errors;
}
getSize() {
return this.store.results.length;
}
}
// module.exports = ESQuery;