· 9 years ago · Sep 14, 2016, 12:04 PM
1'use strict';
2const _ = require('lodash');
3const Promise = require('bluebird');
4
5const es = require('elasticsearch').Client({
6 host: sails.config.app.elasticsearch.host,
7 connectionClass: require('http-aws-es'),
8 amazonES: {
9 region: sails.config.app.elasticsearch.region,
10 accessKey: sails.config.app.elasticsearch.accessKey,
11 secretKey: sails.config.app.elasticsearch.secretKey
12 }
13});
14
15const indexName = sails.config.app.elasticsearch.index;
16
17var initContactsMapping = function () {
18 return es.indices.putMapping({
19 index: indexName,
20 type: 'contacts',
21 body: {
22 properties: {
23 id: {type: 'string'},
24 firstName: {type: 'string'},
25 lastName: {type: 'string'},
26 jobTitle: {type: 'string'},
27 email: {type: 'string'},
28 phone1: {type: 'string'},
29 phone2: {type: 'string'},
30 country: {type: 'string'},
31 state: {type: 'string'},
32 city: {type: 'string'},
33 street: {type: 'string'},
34 postalCode: {type: 'string'},
35 role: {type: 'string'},
36 suggest: {
37 type: 'completion',
38 analyzer: 'simple',
39 search_analyzer: 'simple',
40 payloads: true
41 }
42 }
43 }
44 });
45};
46
47var initPropertiesMapping = function () {
48 return es.indices.putMapping({
49 index: indexName,
50 type: 'properties',
51 body: {
52 _routing: {
53 required: false
54 },
55 properties: {
56 id: {type: 'string'},
57 country: {type: 'string'},
58 state: {type: 'string'},
59 city: {type: 'string'},
60 street: {type: 'string'},
61 postalCode: {type: 'string'},
62 geoLocation: {type: 'string'},
63 suggest: {
64 type: 'completion',
65 analyzer: 'simple',
66 search_analyzer: 'simple',
67 payloads: true
68 }
69 }
70 }
71 });
72};
73
74var initUnitsMapping = function () {
75 return es.indices.putMapping({
76 index: indexName,
77 type: 'units',
78 body: {
79 _routing: {
80 required: false
81 },
82 properties: {
83 id: {type: 'string'},
84 description: {type: 'string'},
85 name: {type: 'string'},
86 publicationDescription: {type: 'string'},
87 publicationTitle: {type: 'string'},
88 type: {type: 'string'},
89 suggest: {
90 type: 'completion',
91 analyzer: 'simple',
92 search_analyzer: 'simple',
93 payloads: true
94 }
95 }
96 }
97 });
98};
99
100
101var initMappings = function () {
102 return initPropertiesMapping()
103 .then(initContactsMapping)
104 .then(initUnitsMapping);
105};
106
107var add = function (type, obj) {
108 var body = _.clone(obj);
109 var id = body.id;
110 delete body.id;
111 return new Promise((resolve, reject) => {
112 es.index({
113 index: indexName,
114 type: type,
115 id: id.toString(),
116 body: body
117 }, (err, result) => {
118 if (err) {
119 return reject(new HttpError(400, err));
120 }
121 resolve(result);
122 });
123 });
124};
125
126var search = function (type, q, filter) {
127 var reg = {_all: '.*' + q + '.*'};
128 if (filter) {
129 _.each(filter, (item) => {
130 _.assign(reg, item);
131 });
132 }
133
134 return new Promise((resolve, reject) => {
135 es.search({
136 type: type,
137 index: indexName,
138 body: {
139 query: {
140 regexp: reg
141 }
142 }
143 }, (err, result) => {
144 if (err) {
145 return reject(new HttpError(400, err));
146 }
147 resolve(result);
148 });
149 });
150};
151
152var reset = function () {
153 return es.indices.exists({
154 index: indexName
155 })
156 .then(function (exists) {
157 if (exists) {
158 return es.indices.delete({
159 index: indexName
160 });
161 }
162 })
163 .then(init);
164};
165
166function init() {
167 es.indices.exists({
168 index: indexName
169 })
170 .then(function (exists) {
171 if (!exists) {
172 return es.indices.create({
173 index: indexName
174 })
175 }
176 })
177 .then(initMappings)
178 .catch(function (err) {
179 sails.log.error(`Can't get elastic search index`, err);
180 });
181}
182
183
184const ESService = module.exports = {
185 add: add,
186 search: search,
187 reset: reset,
188 init: init
189};