-
Notifications
You must be signed in to change notification settings - Fork 1
/
ElasticFetch.js
55 lines (51 loc) · 1.12 KB
/
ElasticFetch.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
"use strict"
export class ElasticFetch {
constructor(baseUrl, indexName, transport) {
this.ressource = [baseUrl, indexName].join("/")
this._fetch = transport || fetch
}
/**
* Make HTTP requests to ressource
* @param {String} ressource the resource that you wish to fetch
* @param {Object} init custom settings that you want to apply to the request (method, headers ...)
* @returns Promise
*/
fetch_json(init = {}, ressource = "_search") {
init.headers = {
...init.headers,
...{
"Content-Type": "application/json",
},
}
init = {
...init,
...{
method: "POST",
body: JSON.stringify(init.body),
},
}
const request = this._fetch
const url = [this.ressource, ressource]
return request(url.join("/"), init).then((response) => response.json())
}
find(field, value) {
const terms = {}
terms[field] = [value]
return this.search({
query: {
terms,
},
})
}
search(
body = {
query: {
match_all: {},
},
}
) {
return this.fetch_json({
body,
})
}
}