forked from webiny/webiny-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
138 lines (126 loc) · 3.63 KB
/
index.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const { Client } = require("@elastic/elasticsearch");
const client = new Client({
// The Elasticsearch endpoint to use.
node: "http://localhost:9200",
// Max number of retries for each request.
maxRetries: 5,
// Max request timeout in milliseconds for each request.
requestTimeout: 60000
});
/**
* For this blog we're doing a very simple setup. No API. No Server.
* The point is to demonstrate the "Elasticsearch" feature and not to build a full-fledged API server of any sort.
* This function performs all the example operation mentioned in the blog post.
*/
const run = async () => {
try {
// Create index
let response = await client.indices.create({
// Name of the index you wish to create.
index: "products"
});
// Index a single document
response = await client.create({
// Unique identifier for the document.
// To automatically generate a document ID omit this parameter.
id: 1,
type: "doc",
// The name of the index.
index: "products",
body: {
id: 1,
name: "iPhone 12",
price: 699,
description: "Blast past fast"
}
});
// Index multiple documents using `bulk`
const dataset = [
{
id: 2,
name: "iPhone 12 mini",
description: "Blast past fast.",
price: 599
},
{
id: 3,
name: "iPhone 12 Pro",
description: "It's a leap year.",
price: 999
},
{
id: 4,
name: "iPhone 12 Pro max",
description: "It's a leap year.",
price: 1199
}
];
const body = dataset.flatMap((doc) => [
{ index: { _index: "products" } },
doc
]);
const { body: bulkResponse } = await client.bulk({ refresh: true, body });
if (bulkResponse.errors) {
const erroredDocuments = [];
// The items array has the same order of the dataset we just indexed.
// The presence of the `error` key indicates that the operation
// that we did for the document has failed.
bulkResponse.items.forEach((action, i) => {
const operation = Object.keys(action)[0];
if (action[operation].error) {
erroredDocuments.push({
// If the status is 429 it means that you can retry the document,
// otherwise it's very likely a mapping error, and you should
// fix the document before to try it again.
status: action[operation].status,
error: action[operation].error,
operation: body[i * 2],
document: body[i * 2 + 1]
});
}
});
// Do something useful with it.
}
// Update document
response = await client.update({
// The name of the index.
index: "products",
// Document ID.
id: 1,
body: {
script: {
source: "ctx._source.price += params.price_diff",
params: {
price_diff: 99
}
}
}
});
// Delete a indexed document
response = await client.delete({
// The name of the index.
index: "products",
// Document ID.
id: 1
});
/* ----------------- Perform search ----------------- */
// Let's search!
const { body: searchBody } = await client.search({
// The name of the index.
index: "products",
body: {
// Defines the search definition using the Query DSL.
query: {
match: {
description: "blast"
}
}
}
});
} catch (error) {
console.log("Oops! Something went wrong. Let's see...\n");
console.log(error);
}
};
// Let's run the main function.
run();