-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathfaq.js
200 lines (173 loc) · 4.65 KB
/
faq.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var winston = require('../config/winston');
var { nanoid } = require("nanoid");
const uuidv4 = require('uuid/v4');
var defaultFullTextLanguage = process.env.DEFAULT_FULLTEXT_INDEX_LANGUAGE || "none";
var FaqSchema = new Schema({
// _id: {
// type: mongoose.Schema.Types.ObjectId,
// index: true,
// required: true,
// auto: true,
// },
id_faq_kb: {
type: String,
index: true
},
intent_id: {
type: String,
required: false,
index: true,
default: function () {
return uuidv4();
}
},
intent_display_name: { //documentare
type: String,
required: false,
index: true,
default: function () {
return nanoid(6);
}
},
question: {
type: String,
required: false
},
webhook_enabled: { //usa questo
type: Boolean,
required: false,
default: false,
},
answer: {
type: String,
//required: true
required: false
},
reply: {
type: Object,
required: false,
},
enabled: {
type: Boolean,
required: false,
default: true,
},
id_project: {
type: String,
required: true,
index: true
},
topic: {
type: String,
default: "default",
index: true
// required: true
},
status: {
type: String,
default: "live",
index: true
// required: true
},
language: {
type: String,
required: false,
index: true
},
// "stats":{
// "conversation_count":2,
// "all_done_count":0,
// "wait_for_team_count":2
// }
createdBy: {
type: String,
required: true
},
form: {
type: Object,
required: false
},
actions: {
type: Array,
required: false
},
attributes: {
type: Object,
},
agents_available: {
type: Boolean,
required: false,
default: function () {
return this.isNew ? false : undefined;
},
}
}, {
timestamps: true,
toJSON: { virtuals: true } //used to polulate messages in toJSON// https://mongoosejs.com/docs/populate.html
}
);
FaqSchema.virtual('faq_kb', {
ref: 'faq_kb', // The model to use
localField: 'id_faq_kb', // Find people where `localField`
foreignField: '_id', // is equal to `foreignField`
justOne: false,
//options: { sort: { name: -1 }, limit: 5 } // Query options, see http://bit.ly/mongoose-query-options
});
FaqSchema.index({ id_project: 1, id_faq_kb: 1, question: 1 });
// https://docs.mongodb.com/manual/core/index-text/
// https://docs.mongodb.com/manual/tutorial/specify-language-for-text-index/
// https://docs.mongodb.com/manual/reference/text-search-languages/#text-search-languages
// In testing...
// FaqSchema.index({ question: "text", answer: "text" }, { weights: { question: 5, answer: 3 } })
// FaqSchema.statics = {
// searchPartial: function (q, callback) {
// return this.find({
// $or: [
// { "question": new RegExp(q, "gi") },
// { "answer": new RegExp(q, "gi") },
// ]
// }, callback)
// },
// searchFull: function (q, callback) {
// return this.find({
// $text: { $search: q, $caseSensitive: false }
// }, callback)
// },
// searchFull: function (q, callback) {
// return this.find({
// $text: { $search: q, $caseSensitive: false }
// }, callback)
// },
// search: function (q, callback) {
// this.searchFull(q, (err, data) => {
// if (err) return callback(err, data);
// if (!err && data.length) return callback(err, data);
// if (!err && data.length === 0) return this.searchPartial(q, callback);
// });
// },
// }
// FaqSchema.index({ question: 'text' },
// { "name": "faq_fulltext", "default_language": defaultFullTextLanguage, "language_override": "language" }); // schema level
FaqSchema.index({ id_faq_kb: 1, question: "text" },
{ name: "faq_fulltext", default_language: defaultFullTextLanguage, language_override: "language" }
);
// FaqSchema.index({question: 'text', answer: 'text'},
// {"name":"faq_fulltext","default_language": defaultFqullTextLanguage,"language_override": "language", weights: {question: 10,answer: 1}}); // schema level
FaqSchema.index({ id_project: 1, id_faq_kb: 1, intent_display_name: 1 }, { unique: true });
FaqSchema.index({ id_project: 1, id_faq_kb: 1, intent_id: 1 }, { unique: true });
FaqSchema.index(
{ "actions.namespace": -1 },
{ partialFilterExpression: { "actions.namespace": { $exists: true } } }
);
var faq = mongoose.model('faq', FaqSchema);
faq.on('index', function (error) {
// "_id index cannot be sparse"
winston.debug('index', error);
});
if (process.env.MONGOOSE_SYNCINDEX) {
faq.syncIndexes();
winston.verbose("faq syncIndexes")
}
module.exports = faq;