forms-angular 0.12.0-beta.341 → 0.12.0-beta.342
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/server/data_form.js +115 -57
- package/dist/server/index.d.ts +2 -0
- package/package.json +1 -1
package/dist/server/data_form.js
CHANGED
|
@@ -437,7 +437,10 @@ export class FormsAngular {
|
|
|
437
437
|
return (new Array(1 + reqLength - String(score).length).join(str) + score);
|
|
438
438
|
}
|
|
439
439
|
let sortString = "";
|
|
440
|
-
sortString += padLeft(obj.addHits
|
|
440
|
+
sortString += padLeft(obj.addHits ?? 9, 1);
|
|
441
|
+
// Within the same number of hits, the more of the search string that matched the better (so in a search for
|
|
442
|
+
// "sue e", a record that only matched "sue" beats one that only matched "e")
|
|
443
|
+
sortString += padLeft(99 - Math.min(matchedChars(obj), 99), 2);
|
|
441
444
|
sortString += padLeft(obj.searchImportance || 99, 2);
|
|
442
445
|
sortString += padLeft(obj.weighting || 9999, 4);
|
|
443
446
|
sortString += obj.text;
|
|
@@ -480,6 +483,15 @@ export class FormsAngular {
|
|
|
480
483
|
}
|
|
481
484
|
let synonymObj = resource.options?.synonyms?.find((s) => s.name.toLowerCase() === collectionNameLower);
|
|
482
485
|
const synonymFilter = synonymObj?.filter;
|
|
486
|
+
if (searchFields.length > 0) {
|
|
487
|
+
// A search across all the fields at once, which finds the strongest matches (every word of a multi-word
|
|
488
|
+
// search, or an exact match for a single word) even when the per-field searches below are swamped
|
|
489
|
+
let combinedObj = { resource, combined: true };
|
|
490
|
+
if (synonymFilter) {
|
|
491
|
+
combinedObj.filter = synonymFilter;
|
|
492
|
+
}
|
|
493
|
+
searches.push(combinedObj);
|
|
494
|
+
}
|
|
483
495
|
for (let m = 0; m < searchFields.length; m++) {
|
|
484
496
|
let searchObj = {
|
|
485
497
|
resource: resource,
|
|
@@ -496,49 +508,96 @@ export class FormsAngular {
|
|
|
496
508
|
let results = [];
|
|
497
509
|
let moreCount = 0;
|
|
498
510
|
let searchCriteria;
|
|
499
|
-
let
|
|
511
|
+
let combinedCriteria;
|
|
512
|
+
let searchStrings; // lower case, unescaped - for comparing with field values
|
|
500
513
|
let multiMatchPossible = false;
|
|
501
|
-
|
|
514
|
+
let startAnchor = "^";
|
|
515
|
+
// Support for searching anywhere in a field by starting with *
|
|
516
|
+
if (searchFor.slice(0, 1) === "*") {
|
|
517
|
+
startAnchor = "";
|
|
518
|
+
searchFor = searchFor.slice(1);
|
|
519
|
+
}
|
|
520
|
+
// Ignore empty "words" caused by leading, trailing or repeated spaces (which would otherwise match everything)
|
|
521
|
+
searchStrings = searchFor.toLowerCase().split(" ").filter((word) => word !== "");
|
|
522
|
+
if (searchFor === "?" || searchStrings.length === 0) {
|
|
502
523
|
// interpret this as a wildcard (so there is no way to search for ?
|
|
503
524
|
searchCriteria = null;
|
|
504
525
|
}
|
|
505
526
|
else {
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
multiMatchPossible = searchFor.includes(" ");
|
|
515
|
-
if (multiMatchPossible) {
|
|
516
|
-
searchStrings = searchFor.split(" ");
|
|
517
|
-
}
|
|
518
|
-
let modifiedSearchStr = multiMatchPossible
|
|
519
|
-
? searchStrings.join("|")
|
|
520
|
-
: searchFor;
|
|
521
|
-
searchFor = searchFor.toLowerCase(); // For later case-insensitive comparison
|
|
527
|
+
multiMatchPossible = searchStrings.length > 1;
|
|
528
|
+
searchFor = searchStrings.join(" "); // For later case-insensitive comparison
|
|
529
|
+
// The snippet to escape the special characters comes from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
|
|
530
|
+
const escapedStrings = searchStrings.map((word) => word.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"));
|
|
531
|
+
const regex = (pattern) => ({ $regex: pattern, $options: "i" });
|
|
532
|
+
const anyField = (fields, pattern) => ({
|
|
533
|
+
$or: fields.map((field) => ({ [field]: regex(pattern) })),
|
|
534
|
+
});
|
|
522
535
|
// Removed the logic that preserved spaces when collection was specified because Louise asked me to.
|
|
523
|
-
searchCriteria = {
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
536
|
+
searchCriteria = regex(`${startAnchor}(${escapedStrings.join("|")})`);
|
|
537
|
+
combinedCriteria = multiMatchPossible
|
|
538
|
+
? // every word matches at least one field
|
|
539
|
+
(fields) => ({ $and: escapedStrings.map((word) => anyField(fields, `${startAnchor}${word}`)) })
|
|
540
|
+
: // a field matches the search string exactly
|
|
541
|
+
(fields) => anyField(fields, `^${escapedStrings[0]}$`);
|
|
527
542
|
}
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
543
|
+
function matchedChars(resultObject) {
|
|
544
|
+
return (resultObject.matched || []).reduce((total, i) => total + searchStrings[i].length, 0);
|
|
545
|
+
}
|
|
546
|
+
// Lower addHits sorts higher: one point for each extra word matched, two for matching the whole phrase in
|
|
547
|
+
// one field and one for an exact match
|
|
548
|
+
function calcAddHits(resultObject) {
|
|
549
|
+
return Math.max(9 -
|
|
550
|
+
Math.max((resultObject.matched?.length || 0) - 1, 0) -
|
|
551
|
+
(resultObject.wholePhrase ? 2 : 0) -
|
|
552
|
+
(resultObject.exact ? 1 : 0), 0);
|
|
553
|
+
}
|
|
554
|
+
// Record what this document matched on, and return true if the result has got stronger
|
|
555
|
+
function recordMatches(resultObject, aDoc, item) {
|
|
556
|
+
const before = calcAddHits(resultObject);
|
|
557
|
+
const charsBefore = matchedChars(resultObject);
|
|
558
|
+
resultObject.matched = resultObject.matched || [];
|
|
559
|
+
if (item.combined) {
|
|
560
|
+
if (multiMatchPossible) {
|
|
561
|
+
resultObject.matched = searchStrings.map((word, i) => i);
|
|
562
|
+
}
|
|
563
|
+
else {
|
|
564
|
+
resultObject.exact = true;
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
else {
|
|
568
|
+
const fieldValue = typeof aDoc[item.field] === "string" ? aDoc[item.field].toLowerCase() : undefined;
|
|
569
|
+
if (fieldValue !== undefined) {
|
|
532
570
|
if (multiMatchPossible) {
|
|
533
|
-
resultObject.matched = resultObject.matched || [];
|
|
534
571
|
// record the index of string that matched, so we don't count it against another field
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
572
|
+
// where more than one word matches (searching for "su sue" and finding "Susan") take the longest
|
|
573
|
+
let i = -1;
|
|
574
|
+
searchStrings.forEach((word, idx) => {
|
|
575
|
+
if (!resultObject.matched.includes(idx) &&
|
|
576
|
+
fieldValue.indexOf(word) === 0 &&
|
|
577
|
+
(i === -1 || word.length > searchStrings[i].length)) {
|
|
578
|
+
i = idx;
|
|
539
579
|
}
|
|
580
|
+
});
|
|
581
|
+
if (i > -1) {
|
|
582
|
+
resultObject.matched.push(i);
|
|
583
|
+
}
|
|
584
|
+
if (fieldValue.indexOf(searchFor) === 0) {
|
|
585
|
+
resultObject.wholePhrase = true;
|
|
540
586
|
}
|
|
541
587
|
}
|
|
588
|
+
else if (fieldValue === searchFor) {
|
|
589
|
+
resultObject.exact = true;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
resultObject.addHits = calcAddHits(resultObject);
|
|
594
|
+
return resultObject.addHits < before || matchedChars(resultObject) > charsBefore;
|
|
595
|
+
}
|
|
596
|
+
let handleSearchResultsFromIndex = function (err, docs, item, cb) {
|
|
597
|
+
function handleSingleSearchResult(aDoc, cbdoc) {
|
|
598
|
+
let thisId = aDoc._id.toString(), resultObject, resultPos;
|
|
599
|
+
function handleResultsInList() {
|
|
600
|
+
recordMatches(resultObject, aDoc, item);
|
|
542
601
|
resultObject.resourceCollection = item.resource.resourceName;
|
|
543
602
|
resultObject.searchImportance =
|
|
544
603
|
item.resource.options.searchImportance || 99;
|
|
@@ -559,33 +618,18 @@ export class FormsAngular {
|
|
|
559
618
|
}
|
|
560
619
|
if (resultPos >= 0) {
|
|
561
620
|
resultObject = Object.assign({}, results[resultPos]);
|
|
621
|
+
resultObject.matched = [...(resultObject.matched || [])];
|
|
562
622
|
// If they have already matched then improve their weighting
|
|
563
|
-
if (
|
|
564
|
-
//
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
resultObject.matched.push(i);
|
|
569
|
-
resultObject.addHits = Math.max((resultObject.addHits || 9) - 1, 0);
|
|
570
|
-
// remove it from current position
|
|
571
|
-
results.splice(resultPos, 1);
|
|
572
|
-
// and re-insert where appropriate
|
|
573
|
-
results.splice(_.sortedIndexBy(results, resultObject, calcResultValue), 0, resultObject);
|
|
574
|
-
break;
|
|
575
|
-
}
|
|
576
|
-
}
|
|
623
|
+
if (recordMatches(resultObject, aDoc, item)) {
|
|
624
|
+
// remove it from current position
|
|
625
|
+
results.splice(resultPos, 1);
|
|
626
|
+
// and re-insert where appropriate
|
|
627
|
+
results.splice(_.sortedIndexBy(results, resultObject, calcResultValue), 0, resultObject);
|
|
577
628
|
}
|
|
578
629
|
cbdoc(null);
|
|
579
630
|
}
|
|
580
631
|
else {
|
|
581
632
|
// Otherwise add them new...
|
|
582
|
-
let addHits;
|
|
583
|
-
if (multiMatchPossible) {
|
|
584
|
-
// If they match the whole search phrase in one index they get smaller addHits (so they sort higher)
|
|
585
|
-
if (aDoc[item.field]?.toLowerCase().indexOf(searchFor) === 0) {
|
|
586
|
-
addHits = 7;
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
633
|
let disambiguationId;
|
|
590
634
|
const opts = item.resource.options;
|
|
591
635
|
const disambiguationResource = opts.disambiguation?.resource;
|
|
@@ -597,7 +641,6 @@ export class FormsAngular {
|
|
|
597
641
|
if (specialListingFormat) {
|
|
598
642
|
specialListingFormat.apply(aDoc, [req]).then((resultObj) => {
|
|
599
643
|
resultObject = resultObj;
|
|
600
|
-
resultObject.addHits = addHits;
|
|
601
644
|
resultObject.disambiguationResource = disambiguationResource;
|
|
602
645
|
resultObject.disambiguationId = disambiguationId;
|
|
603
646
|
handleResultsInList();
|
|
@@ -612,7 +655,6 @@ export class FormsAngular {
|
|
|
612
655
|
resultObject = {
|
|
613
656
|
id: aDoc._id,
|
|
614
657
|
weighting: 9999,
|
|
615
|
-
addHits,
|
|
616
658
|
disambiguationResource,
|
|
617
659
|
disambiguationId,
|
|
618
660
|
text: description,
|
|
@@ -637,7 +679,21 @@ export class FormsAngular {
|
|
|
637
679
|
this.searchFunc(searches, function (item, cb) {
|
|
638
680
|
let searchDoc = {};
|
|
639
681
|
let searchFilter = filter || item.filter;
|
|
640
|
-
if (
|
|
682
|
+
if (item.combined) {
|
|
683
|
+
if (!combinedCriteria) {
|
|
684
|
+
// wildcard search - the per-field searches will find everything
|
|
685
|
+
return cb();
|
|
686
|
+
}
|
|
687
|
+
if (searchFilter) {
|
|
688
|
+
that.hackVariables(searchFilter);
|
|
689
|
+
extend(searchDoc, searchFilter);
|
|
690
|
+
}
|
|
691
|
+
searchDoc["$and"] = [
|
|
692
|
+
...(searchDoc["$and"] || []),
|
|
693
|
+
combinedCriteria(item.resource.options.searchFields),
|
|
694
|
+
];
|
|
695
|
+
}
|
|
696
|
+
else if (searchFilter) {
|
|
641
697
|
that.hackVariables(searchFilter);
|
|
642
698
|
extend(searchDoc, searchFilter);
|
|
643
699
|
if (searchFilter[item.field]) {
|
|
@@ -682,9 +738,11 @@ export class FormsAngular {
|
|
|
682
738
|
callback(err);
|
|
683
739
|
}
|
|
684
740
|
else {
|
|
685
|
-
// Strip weighting from the results
|
|
741
|
+
// Strip weighting and match details from the results
|
|
686
742
|
results = _.map(results, function (aResult) {
|
|
687
743
|
delete aResult.weighting;
|
|
744
|
+
delete aResult.wholePhrase;
|
|
745
|
+
delete aResult.exact;
|
|
688
746
|
return aResult;
|
|
689
747
|
});
|
|
690
748
|
if (limit && results.length > limit) {
|
package/dist/server/index.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ declare module fngServer {
|
|
|
18
18
|
searchImportance? : number;
|
|
19
19
|
addHits?: number;
|
|
20
20
|
matched: number[];
|
|
21
|
+
wholePhrase?: boolean; // a single field starts with the whole (multi-word) search phrase
|
|
22
|
+
exact?: boolean; // a single field exactly equals the (single-word) search string
|
|
21
23
|
resourceCollection: string;
|
|
22
24
|
// the next two are only set where the resource options includes disambiguation instructions and multiple search results with the same text value are found...:
|
|
23
25
|
disambiguationId?: any; // this will identify the record (from another resource) that will be used to disambiguate them
|
package/package.json
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
},
|
|
7
7
|
"description": "A form builder that sits on top of Angular.js, Twitter Bootstrap, jQuery UI, Angular-UI, Express and Mongoose. Opinionated or what?",
|
|
8
8
|
"homepage": "http://forms-angular.org",
|
|
9
|
-
"version": "0.12.0-beta.
|
|
9
|
+
"version": "0.12.0-beta.342",
|
|
10
10
|
"packageManager": "npm@10.2.4",
|
|
11
11
|
"type": "module",
|
|
12
12
|
"engines": {
|