diff --git a/ddocs/medic-db/medic-nouveau/nouveau/contacts_by_freetext/index.js b/ddocs/medic-db/medic-nouveau/nouveau/contacts_by_freetext/index.js index 907aec546c..ca88167574 100644 --- a/ddocs/medic-db/medic-nouveau/nouveau/contacts_by_freetext/index.js +++ b/ddocs/medic-db/medic-nouveau/nouveau/contacts_by_freetext/index.js @@ -1,8 +1,8 @@ -function(doc) { - const skip = [ '_id', '_rev', 'type', 'refid', 'geolocation' ]; +function (doc) { + const skip = ['_id', '_rev', 'type', 'refid', 'geolocation']; let toIndex = ''; - const types = [ 'district_hospital', 'health_center', 'clinic', 'person' ]; + const types = ['district_hospital', 'health_center', 'clinic', 'person']; let idx; if (doc.type === 'contact') { idx = types.indexOf(doc.contact_type); @@ -15,7 +15,7 @@ function(doc) { const isContactDoc = idx !== -1; if (isContactDoc) { - Object.keys(doc).forEach(function(key) { + Object.keys(doc).forEach(function (key) { const value = doc[key]; if (!key || !value) { return; @@ -28,14 +28,16 @@ function(doc) { if (typeof value === 'string') { toIndex += ' ' + value; + // index('text', key, value, { store: true }); } - /*if (typeof value === 'number') { - index('double', key, value, { store: true }); - }*/ + if (typeof value === 'number') { + // index('double', key, value, { store: true }); + } - /*if (typeof value === 'string') { - index('text', key, value, { store: true }); + /*const fieldNameRegex = /^\$?[a-zA-Z][a-zA-Z0-9_]*$/g + if (fieldNameRegex.test(key)) { + console.log(`key "${key}" doesn't pass regex`); }*/ }); diff --git a/ddocs/medic-db/medic-nouveau/nouveau/contacts_by_type_freetext/index.js b/ddocs/medic-db/medic-nouveau/nouveau/contacts_by_type_freetext/index.js new file mode 100644 index 0000000000..0ef89e84be --- /dev/null +++ b/ddocs/medic-db/medic-nouveau/nouveau/contacts_by_type_freetext/index.js @@ -0,0 +1,49 @@ +function (doc) { + var skip = ['_id', '_rev', 'type', 'refid', 'geolocation']; + let toIndex = ''; + + var types = ['district_hospital', 'health_center', 'clinic', 'person']; + var idx; + var type; + if (doc.type === 'contact') { + type = doc.contact_type; + idx = types.indexOf(type); + if (idx === -1) { + idx = type; + } + } else { + type = doc.type; + idx = types.indexOf(type); + } + if (idx !== -1) { + Object.keys(doc).forEach(function (key) { + var value = doc[key]; + if (!key || !value) { + return; + } + key = key.toLowerCase(); + if (skip.indexOf(key) !== -1 || /_date$/.test(key)) { + return; + } + + if (typeof value === 'string') { + toIndex += ' ' + value; + // index('text', key, value, { store: true }); + } + + if (typeof value === 'number') { + // index('double', key, value, { store: true }); + } + + /*const fieldNameRegex = /^\$?[a-zA-Z][a-zA-Z0-9_]*$/g + if (fieldNameRegex.test(key)) { + console.log(`key "${key}" doesn't pass regex`); + }*/ + }); + } + + toIndex = toIndex.trim(); + if (toIndex) { + index('text', 'default', toIndex, { store: true }); + } +} diff --git a/ddocs/medic-db/medic-nouveau/nouveau/reports_by_freetext/index.js b/ddocs/medic-db/medic-nouveau/nouveau/reports_by_freetext/index.js new file mode 100644 index 0000000000..ca3dd7cbe9 --- /dev/null +++ b/ddocs/medic-db/medic-nouveau/nouveau/reports_by_freetext/index.js @@ -0,0 +1,51 @@ +function (doc) { + var skip = ['_id', '_rev', 'type', 'refid', 'content']; + let toIndex = ''; + + var emitField = function (key, value) { + if (!key || !value) { + return; + } + key = key.toLowerCase(); + if (skip.indexOf(key) !== -1 || /_date$/.test(key)) { + return; + } + + if (typeof value === 'string') { + toIndex += ' ' + value; + // index('text', key, value, { store: true }); + } + + if (typeof value === 'number') { + // index('double', key, value, { store: true }); + } + + const fieldNameRegex = /^\$?[a-zA-Z][a-zA-Z0-9_]*$/g + if (fieldNameRegex.test(key)) { + console.log(`key "${key}" doesn't pass regex`); + } + }; + + if (doc.type === 'data_record' && doc.form) { + Object.keys(doc).forEach(function (key) { + emitField(key, doc[key]); + }); + if (doc.fields) { + Object.keys(doc.fields).forEach(function (key) { + emitField(key, doc.fields[key]); + }); + } + if (doc.contact && doc.contact._id) { + // index('text', 'contact', doc.contact._id.toLowerCase(), { store: true }); + /*const fieldNameRegex = /^\$?[a-zA-Z][a-zA-Z0-9_]*$/g + if (fieldNameRegex.test('contact')) { + console.log(`key "contact" doesn't pass regex`); + }*/ + } + } + + toIndex = toIndex.trim(); + if (toIndex) { + index('text', 'default', toIndex, { store: true }); + } +}