Skip to content

Commit a343501

Browse files
authored
feat(search): all and some words for fullTextSearch
Added two new fullTextSearch options some and all some Will do the same as exact but supports multiple search values separated by whitespace. At least one word must match all Same as some but all words have to match in all given searchfields of each record altogether
1 parent 8aa6c67 commit a343501

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/definitions/modules/search.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,10 @@
660660
return [];
661661
}
662662
// iterate through search fields looking for matches
663-
$.each(searchFields, function (index, field) {
664-
$.each(source, function (label, content) {
663+
var lastSearchFieldIndex = searchFields.length - 1;
664+
$.each(source, function (label, content) {
665+
var concatenatedContent = [];
666+
$.each(searchFields, function (index, field) {
665667
var
666668
fieldExists = (typeof content[field] === 'string') || (typeof content[field] === 'number')
667669
;
@@ -670,11 +672,21 @@
670672
text = typeof content[field] === 'string'
671673
? module.remove.diacritics(content[field])
672674
: content[field].toString();
673-
if (text.search(matchRegExp) !== -1) {
675+
if (settings.fullTextSearch === 'all') {
676+
concatenatedContent.push(text);
677+
if (index < lastSearchFieldIndex) {
678+
return true;
679+
}
680+
text = concatenatedContent.join(' ');
681+
}
682+
if (settings.fullTextSearch !== 'all' && text.search(matchRegExp) !== -1) {
674683
// content starts with value (first in results)
675684
addResult(results, content);
676685
} else if (settings.fullTextSearch === 'exact' && module.exactSearch(searchTerm, text)) {
677-
// content fuzzy matches (last in results)
686+
addResult(exactResults, content);
687+
} else if (settings.fullTextSearch === 'some' && module.wordSearch(searchTerm, text)) {
688+
addResult(exactResults, content);
689+
} else if (settings.fullTextSearch === 'all' && module.wordSearch(searchTerm, text, true)) {
678690
addResult(exactResults, content);
679691
} else if (settings.fullTextSearch === true && module.fuzzySearch(searchTerm, text)) {
680692
// content fuzzy matches (last in results)
@@ -695,6 +707,21 @@
695707

696708
return term.indexOf(query) > -1;
697709
},
710+
wordSearch: function (query, term, matchAll) {
711+
var allWords = query.split(/\s+/),
712+
w,
713+
wL = allWords.length,
714+
found = false
715+
;
716+
for (w = 0; w < wL; w++) {
717+
found = module.exactSearch(allWords[w], term);
718+
if ((!found && matchAll) || (found && !matchAll)) {
719+
break;
720+
}
721+
}
722+
723+
return found;
724+
},
698725
fuzzySearch: function (query, term) {
699726
var
700727
termLength = term.length,

0 commit comments

Comments
 (0)