Skip to content

Commit de9a64a

Browse files
committed
fix(forms) Fix search for multiple words
1 parent 614ae03 commit de9a64a

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

server/data_form.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ DataForm.prototype.internalSearch = function (req, resourcesToSearch, includeRes
244244
searchCriteria = { $regex: '^(' + searchFor.split(' ').join('|') + ')', $options: 'i' };
245245
this.searchFunc(searches, function (item, cb) {
246246
var searchDoc = {};
247-
// console.log(searchCriteria);
248247
if (filter) {
249248
extend(searchDoc, filter);
250249
if (filter[item.field]) {
@@ -268,12 +267,7 @@ DataForm.prototype.internalSearch = function (req, resourcesToSearch, includeRes
268267
if (!err && docs && docs.length > 0) {
269268
async.map(docs, function (aDoc, cbdoc) {
270269
// Do we already have them in the list?
271-
var thisId = aDoc._id, resultObject, resultPos;
272-
for (resultPos = results.length - 1; resultPos >= 0; resultPos--) {
273-
if (results[resultPos].id.id === thisId.id) {
274-
break;
275-
}
276-
}
270+
var thisId = aDoc._id.toString(), resultObject, resultPos;
277271
function handleResultsInList() {
278272
resultObject.searchImportance = item.resource.options.searchImportance || 99;
279273
if (item.resource.options.localisationData) {
@@ -284,6 +278,11 @@ DataForm.prototype.internalSearch = function (req, resourcesToSearch, includeRes
284278
results.splice(_.sortedIndex(results, resultObject, calcResultValue), 0, resultObject);
285279
cbdoc(null);
286280
}
281+
for (resultPos = results.length - 1; resultPos >= 0; resultPos--) {
282+
if (results[resultPos].id.toString() === thisId) {
283+
break;
284+
}
285+
}
287286
if (resultPos >= 0) {
288287
resultObject = {};
289288
extend(resultObject, results[resultPos]);
@@ -310,7 +309,7 @@ DataForm.prototype.internalSearch = function (req, resourcesToSearch, includeRes
310309
}
311310
else {
312311
resultObject = {
313-
id: thisId,
312+
id: aDoc._id,
314313
weighting: 9999,
315314
text: description
316315
};
@@ -621,7 +620,7 @@ DataForm.prototype.reportInternal = function (req, resource, schema, options, ca
621620
if (schema.columnTranslations) {
622621
toDo.applyTranslations = ['runAggregation', function (cb, results) {
623622
function doATranslate(column, theTranslation) {
624-
results.runAggregation.forEach(function (resultRow) {
623+
results['runAggregation'].forEach(function (resultRow) {
625624
var valToTranslate = resultRow[column.field];
626625
valToTranslate = (valToTranslate ? valToTranslate.toString() : '');
627626
var thisTranslation = _.find(theTranslation.translations, function (option) {
@@ -1057,4 +1056,3 @@ DataForm.prototype.entityList = function () {
10571056
});
10581057
}, this);
10591058
};
1060-
//# sourceMappingURL=data_form.js.map

server/data_form.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ DataForm.prototype.internalSearch = function (req, resourcesToSearch, includeRes
291291
searches,
292292
function (item, cb) {
293293
var searchDoc = {};
294-
// console.log(searchCriteria);
295294
if (filter) {
296295
extend(searchDoc, filter);
297296
if (filter[item.field]) {
@@ -313,16 +312,10 @@ DataForm.prototype.internalSearch = function (req, resourcesToSearch, includeRes
313312
that.filteredFind(item.resource, req, null, searchDoc, item.resource.options.searchOrder, limit + 60, null, function (err, docs) {
314313
if (!err && docs && docs.length > 0) {
315314
async.map(docs, function(aDoc, cbdoc) {
316-
317315
// Do we already have them in the list?
318-
var thisId = aDoc._id,
319-
resultObject,
320-
resultPos;
321-
for (resultPos = results.length - 1; resultPos >= 0; resultPos--) {
322-
if (results[resultPos].id.id === thisId.id) {
323-
break;
324-
}
325-
}
316+
var thisId:string = aDoc._id.toString(),
317+
resultObject: any,
318+
resultPos: number;
326319

327320
function handleResultsInList() {
328321
resultObject.searchImportance = item.resource.options.searchImportance || 99;
@@ -334,6 +327,12 @@ DataForm.prototype.internalSearch = function (req, resourcesToSearch, includeRes
334327
results.splice(_.sortedIndex(results, resultObject, calcResultValue), 0, resultObject);
335328
cbdoc(null);
336329
}
330+
331+
for (resultPos = results.length - 1; resultPos >= 0; resultPos--) {
332+
if (results[resultPos].id.toString() === thisId) {
333+
break;
334+
}
335+
}
337336
if (resultPos >= 0) {
338337
resultObject = {};
339338
extend(resultObject, results[resultPos]);
@@ -357,7 +356,7 @@ DataForm.prototype.internalSearch = function (req, resourcesToSearch, includeRes
357356
cbdoc(err);
358357
} else {
359358
resultObject = {
360-
id: thisId,
359+
id: aDoc._id,
361360
weighting: 9999,
362361
text: description
363362
};
@@ -508,7 +507,7 @@ DataForm.prototype.preprocess = function (resource: Resource, paths, formSchema)
508507
if (paths[element].options.match) {
509508
outPath[element].options.match = paths[element].options.match.source;
510509
}
511-
let schemaListInfo = paths[element].options.list;
510+
let schemaListInfo: any = paths[element].options.list;
512511
if (schemaListInfo) {
513512
var listFieldInfo:ListField = {field: element};
514513
if (typeof schemaListInfo === 'object' && Object.keys(schemaListInfo).length > 0) {
@@ -691,10 +690,10 @@ DataForm.prototype.reportInternal = function (req, resource, schema, options, ca
691690
var translations = []; // array of form {ref:'lookupname',translations:[{value:xx, display:' '}]}
692691
// if we need to do any column translations add the function to the tasks list
693692
if (schema.columnTranslations) {
694-
toDo.applyTranslations = ['runAggregation', function (cb, results) {
693+
toDo.applyTranslations = ['runAggregation', function (cb, results: any) {
695694

696695
function doATranslate(column, theTranslation) {
697-
results.runAggregation.forEach(function (resultRow) {
696+
results['runAggregation'].forEach(function (resultRow) {
698697
var valToTranslate = resultRow[column.field];
699698
valToTranslate = (valToTranslate ? valToTranslate.toString() : '');
700699
var thisTranslation = _.find(theTranslation.translations, function (option) {

test/api/CRUD-APISpec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,29 @@ describe('API', function () {
347347
var mockRes = {
348348
send: function (data) {
349349
assert.notEqual(data.moreCount, 0);
350-
assert.equal(data.results[0].text, 'Smith04 John04'); // Double hit
350+
assert.equal(data.results[0].text, 'Smith04 John04'); // Double hit should come first
351351
assert.equal(data.results[1].text, 'Smith00 John00'); // normal weighting
352352
done();
353353
}
354354
};
355355
fng.searchAll()(mockReq, mockRes);
356356
});
357357

358+
it('should not repeat a record in the results', function (done) {
359+
var mockReq = {
360+
url: '/search?q=smith04 john04',
361+
route: {path : '/api/search'}
362+
};
363+
var mockRes = {
364+
send: function (data) {
365+
assert.equal(data.results.length, 1);
366+
assert.equal(data.results[0].text, 'Smith04 John04');
367+
done();
368+
}
369+
};
370+
fng.searchAll()(mockReq, mockRes);
371+
});
372+
358373

359374
it('should support searchResultFormat option', function (done) {
360375
var mockReq = {

0 commit comments

Comments
 (0)