-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
78 lines (63 loc) · 2.41 KB
/
Copy pathindex.js
File metadata and controls
78 lines (63 loc) · 2.41 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
import { connectToDb } from "./functions/sql_queries/connect_to_db.js";
import {
getAlpacaRegistries,
getAlpacaIdsFromNorwegianRegistry,
getAlpacaDetails,
} from "./functions/sql_queries/get_alpacas.js";
import fileTransformer from "./functions/fileTransformer.js";
import { farmsFromAlpacas } from "./functions/farmsFromAlpacas.js";
import bulkSyntax from "./functions/elasticsearch_commands/bulkSyntax.js";
// ELASTICSEARCH
import createIndexWithDocuments from "./functions/elasticsearch_commands/index.js";
import alpacaComponentTemplate from "./functions/elasticsearch_commands/component_templates/alpacas.js";
import farmsComponentTemplate from "./functions/elasticsearch_commands/component_templates/farms.js";
import companiesComponentTemplate from "./functions/elasticsearch_commands/component_templates/companies.js";
/******** SQL -> Elastic ********/
console.log(`[LOG] START SQL -> Elastic`);
const connection = await connectToDb();
const [alpacaRegistries] = await getAlpacaRegistries(connection);
console.log(
`[LOG] Retrieving ${alpacaRegistries.length} alpaca registry ids from database`
);
const [alpacaIdsFromNorwegianRegistry] =
await getAlpacaIdsFromNorwegianRegistry(connection);
console.log(
`[LOG] Retrieving ${alpacaIdsFromNorwegianRegistry.length} alpaca ids from database`
);
const [alpacaDetailsArray] = await getAlpacaDetails(connection);
console.log(
`[LOG] Retrieving ${alpacaDetailsArray.length} alpaca details from database`
);
await connection.end();
const animals = await fileTransformer(alpacaDetailsArray, {
geoDecodeEnrich: true,
animal: "alpaca",
});
await createIndexWithDocuments(
"animals_all",
bulkSyntax(animals),
alpacaComponentTemplate
);
const farms_all = bulkSyntax(
farmsFromAlpacas(animals, { publicFarmsOnly: false })
);
await createIndexWithDocuments("farms_all", farms_all, farmsComponentTemplate);
const farms_public = bulkSyntax(
farmsFromAlpacas(animals, { publicFarmsOnly: true })
);
await createIndexWithDocuments(
"farms_public",
farms_public,
farmsComponentTemplate
);
const companies_all = bulkSyntax(
farmsFromAlpacas(animals, { publicFarmsOnly: false, includeAlpacas: true })
);
// The order of templates matters. Otherwise the nested property of alpacas is lost, GOD only knows why!!
await createIndexWithDocuments(
"companies_all",
companies_all,
companiesComponentTemplate,
farmsComponentTemplate
);
console.log(`[LOG] END SQL -> Elastic`);