-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.js
113 lines (101 loc) · 2.9 KB
/
data.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
const Elasticsearch = require('elasticsearch')
const path = require('path')
const jsonFile = './data/cities.json'
const indexName = 'test'
const typeName = '_doc'
const cities = require(jsonFile)
const elasticClient = new Elasticsearch.Client({ host: process.env.ELASTIC_IP })
elasticClient.ping({ requestTimeout: 30000 }, (err) => {
if (err) {
console.log('Elastic search is down!')
return
}
console.log('Elastc search is ready to run!')
})
const createIndex = async () => {
try {
const result = await elasticClient.indices.create({
index: indexName,
body: {
"settings": {
"analysis": {
"filter": {
"my_ascii_folding": {
"type": "asciifolding",
"preserve_original": true
}
},
"analyzer": {
"turkish_analyzer": {
"tokenizer": "standard",
"filter": [
"lowercase",
"my_ascii_folding"
]
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "turkish_analyzer"
}
}
}
}
})
console.log('Created the new Index: ', result)
} catch (error) {
console.log(error)
}
}
//createIndex()
const addDataToIndex = async () => {
try {
const result = await elasticClient.index({
index: indexName, type: typeName, body: {
Key1: "Content for key one",
Key2: "Content for key two",
key3: "Content for key three"
}
})
console.log(result)
} catch (error) {
console.log(error)
}
}
//addDataToIndex()
let bulk = []
// const pushToBulk = city => {
// cities.forEach(city => {
// bulk.push({
// index: {
// _index: indexName,
// _type: typeName
// }
// })
// bulk.push(city);
// })
// }
// pushToBulk()
const bulkIndex = async () => {
cities.forEach(city => {
bulk.push({
index: {
_index: indexName,
_type: typeName
}
})
bulk.push(city);
})
try {
await elasticClient.bulk({ body: bulk })
console.log('Successfully bulk operation: ', cities.length)
} catch (error) {
console.log(error);
console.log('Failed to bulk')
}
}
//bulkIndex()