-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
157 lines (138 loc) · 3.42 KB
/
index.js
File metadata and controls
157 lines (138 loc) · 3.42 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* File: index.js
* Project: es-client-example
* File Created: Tuesday, 5th November 2019 10:16:43 pm
* Author: Jaseem Jas (jaseem@socialanimal.com)
* -----
* Last Modified: Tuesday, 5th November 2019 10:53:25 pm
* Modified By: Jaseem Jas (jaseem@socialanimal.com)
* -----
* Copyright 2016 - 2019 Socialanimal.com
*/
const { Client } = require('@elastic/elasticsearch');
const client = new Client({ node: 'http://localhost:9200' });
async function createTitle() {
const { response } = await client.create({
index: 'titles',
id: 11,
body: {
title: 'My First title',
author: 'Jaseem',
date: new Date()
}
});
}
// createTitle().catch(console.log);
async function getTitle() {
const { body } = await client.get({
index: 'titles',
id: 11
});
console.log(body);
}
// getTitle().catch(console.log);
async function updateTitle() {
const { response } = await client.update({
index: 'titles',
id: 11,
body: {
doc: {
title: 'Awsome title'
}
}
});
}
// updateTitle().catch(console.log);
const titles = [{
id: 1,
title: 'Traditional Marketing Vs Digital Marketing',
author: 'eCommerce FAQs',
date: new Date()
},
{
id: 2,
title: 'Global Digital Marketing Courses Market 2019 Business Strategies ',
author: 'Coursera',
date: new Date()
},
{
id: 3,
title: 'Big Data vs Data Warehouse',
author: 'Igor',
date: new Date()
},
{
id: 4,
title: 'Traditional Marketing Vs Digital Marketing',
author: 'eCommerce FAQs',
date: new Date()
},
{
id: 5,
title: 'Cloudera Data Platform gives big data users multi-cloud path',
author: 'Erpinnews',
date: new Date()
},
{
id: 6,
title: 'IoT Event Blog Affinity IoT',
author: 'infoMegan Davis',
date: new Date()
},
{
id: 7,
title: 'Cloud to cloud backup Solutions Archives',
author: 'Michael Schneider',
date: new Date()
},
{
id: 8,
title: 'Car hire with car insurance',
author: 'Gary Hunter',
date: new Date()
},
{
id: 9,
title: 'Fashion Jobs and Fashion Career Advice',
author: 'Randy C. Marque',
date: new Date()
},
{
id: 10,
title: 'Fashion Designer Zac Posen is Shutting Down his Fashion Label',
author: 'MARCY OSTER, JTA',
date: new Date()
}
];
const body = titles.flatMap((doc, index) => [
{ index: { _index: 'titles', _id: index + 1 } },
doc
]);
async function createTitles() {
const { response } = await client.bulk({ body: body, refresh: true });
if (response) {
console.log(response.errors);
}
}
// createTitles().catch(console.log);
async function countTitles() {
const { body } = await client.count({
index: 'titles'
});
console.log(body);
}
// countTitles().catch(console.log);
async function searchTitles() {
const { body: response } = await client.search({
index: 'titles',
body: {
query: {
match: {
title: 'Fashion'
}
}
}
});
console.log(response.hits.hits);
}
searchTitles().catch(console.log);