-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmappings.js
133 lines (121 loc) · 4.22 KB
/
mappings.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
var elasticsearch = require('elasticsearch');
var fs = require('fs');
const matter = require('gray-matter');
var envConfig = require('dotenv').config()
var client = new elasticsearch.Client({
hosts: ['http://elastic:'+process.env.ES_PASSWORD+'@localhost:9200']
});
function deleteIndexFn(indexName) {
client.indices.exists({ index: indexName }, function (err, resp) {
if (err) {
console.log(err);
return;
}
console.log(resp)
if (resp) { // if index exists - delete
console.log(indexName + ' exists and will be deleted');
client.indices.delete({
index: indexName
})
}
createIndexFn(indexName);
});
}
function createIndexFn(indexName) {
client.indices.exists({ index: indexName }, function (err, resp) {
if (err) {
console.log(err);
return;
}
console.log(resp)
console.log(indexName + ' creation...');
client.indices.create({
index: indexName,
body: {
mappings: {
properties: {
title: { "type": "text" },
subtitle: { "type": "text" },
contributor: { "type": "object"},
pub_date: { "type": "date" },
pub_type: { "type": "text"},
doi: { "type": "text" },
issn: { "type": "integer" },
language: { "type": "text" },
layout: { "type": "text" },
order: { "type": "short" },
references: { "type": "text" },
series_issue_number: { "type":"text" },
series_periodical_name: { "type": "text" },
abstract: { "type": "text" },
acknowledgements: { "type": "text" }
}
}
}
})
})
}
function addDocument(_id, data, indexName, refArray) {
console.log(indexName)
client.index({
index: indexName,
// type: "document",
id: _id,
body: {
title: data.title,
subtitle: data.subtitle,
contributor: data.contributor,
pub_date : data.pub_date,
pub_type: data.pub_type,
doi: data.doi,
issn: data.issn,
language: data.language,
layout: data.layout,
order: data.order,
references: refArray,
series_issue_number: data.series_issue_number,
series_periodical_name: data.series_periodical_name,
abstract: data.abstract,
acknowledgements: data.acknowledgements,
}
}, function (err, resp){
if (err) {
console.log(err)
}
})
}
function readAllFolder(rootDir, includeDirs, includeDirsBoolean, currentDir, indexName) {
const readRootDir = fs.readdirSync(rootDir);
readRootDir.forEach((innerFiles) => {
if (includeDirsBoolean === true) {
if (includeDirs?.includes(innerFiles)) {
console.log(innerFiles);
if (fs.lstatSync(rootDir + "/" + innerFiles).isDirectory()) {
var path = rootDir + "/" + innerFiles
readAllFolder(path, includeDirs, false, path, indexName); // if dir then go through files
}
}
} else {
console.log(innerFiles)
var markdownData = matter.read(currentDir + "/" + innerFiles);
const random_uuid = uuidv4();
if (typeof markdownData.data.references !== 'undefined') {
var refArray = markdownData.data.references.map(String)
}
addDocument(random_uuid, markdownData.data, indexName, refArray)
}
});
};
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
module.exports = {
setupES : deleteIndexFn,
createES : createIndexFn,
getFiles : readAllFolder,
}