UNPKG

1.15 kBPlain TextView Raw
1var mongoosastic = require("mongoosastic");
2var elasticsearch = require("elasticsearch");
3
4import {Config} from "../config";
5
6/**
7 * ElastticSearchUtils
8 */
9class ElastticSearchUtils {
10 private esClient: any;
11 constructor() {
12 if (Config.ApplyElasticSearch) {
13 this.esClient = new elasticsearch.Client({ host: Config.ElasticSearchConnection });
14 }
15 }
16
17 insertMongoosasticToSchema(schema: any) {
18 if (Config.ApplyElasticSearch) {
19 schema.plugin(mongoosastic, {
20 esClient: this.esClient
21 });
22 }
23 }
24
25 registerToMongoosastic(mongooseModel: any) {
26 // This will be called only in the case if the mongoosastic plugin was attached to the mongoose model.
27 if (Config.ApplyElasticSearch && mongooseModel.createMapping) {
28 mongooseModel.createMapping((err: any, mapping: any) => {
29 if (err) {
30 console.error(err);
31 }
32 else {
33 console.log(mapping);
34 }
35 });
36 }
37 }
38}
39
40export var searchUtils = new ElastticSearchUtils();
\No newline at end of file