Skip to content

Commit

Permalink
contacts_by_type_freetext & reports_by_freetext nouveau-style
Browse files Browse the repository at this point in the history
  • Loading branch information
m5r committed Nov 7, 2024
1 parent baaed8e commit b25ab70
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 9 deletions.
20 changes: 11 additions & 9 deletions ddocs/medic-db/medic-nouveau/nouveau/contacts_by_freetext/index.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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;
Expand All @@ -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`);
}*/
});

Expand Down
Original file line number Diff line number Diff line change
@@ -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 });
}
}
51 changes: 51 additions & 0 deletions ddocs/medic-db/medic-nouveau/nouveau/reports_by_freetext/index.js
Original file line number Diff line number Diff line change
@@ -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 });
}
}

0 comments on commit b25ab70

Please sign in to comment.