Skip to content

Commit

Permalink
feat(search): all and some words for fullTextSearch
Browse files Browse the repository at this point in the history
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
  • Loading branch information
lubber-de authored Jan 13, 2023
1 parent 8aa6c67 commit a343501
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions src/definitions/modules/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,10 @@
return [];
}
// iterate through search fields looking for matches
$.each(searchFields, function (index, field) {
$.each(source, function (label, content) {
var lastSearchFieldIndex = searchFields.length - 1;
$.each(source, function (label, content) {
var concatenatedContent = [];
$.each(searchFields, function (index, field) {
var
fieldExists = (typeof content[field] === 'string') || (typeof content[field] === 'number')
;
Expand All @@ -670,11 +672,21 @@
text = typeof content[field] === 'string'
? module.remove.diacritics(content[field])
: content[field].toString();
if (text.search(matchRegExp) !== -1) {
if (settings.fullTextSearch === 'all') {
concatenatedContent.push(text);
if (index < lastSearchFieldIndex) {
return true;
}
text = concatenatedContent.join(' ');
}
if (settings.fullTextSearch !== 'all' && text.search(matchRegExp) !== -1) {
// content starts with value (first in results)
addResult(results, content);
} else if (settings.fullTextSearch === 'exact' && module.exactSearch(searchTerm, text)) {
// content fuzzy matches (last in results)
addResult(exactResults, content);
} else if (settings.fullTextSearch === 'some' && module.wordSearch(searchTerm, text)) {
addResult(exactResults, content);
} else if (settings.fullTextSearch === 'all' && module.wordSearch(searchTerm, text, true)) {
addResult(exactResults, content);
} else if (settings.fullTextSearch === true && module.fuzzySearch(searchTerm, text)) {
// content fuzzy matches (last in results)
Expand All @@ -695,6 +707,21 @@

return term.indexOf(query) > -1;
},
wordSearch: function (query, term, matchAll) {
var allWords = query.split(/\s+/),
w,
wL = allWords.length,
found = false
;
for (w = 0; w < wL; w++) {
found = module.exactSearch(allWords[w], term);
if ((!found && matchAll) || (found && !matchAll)) {
break;
}
}

return found;
},
fuzzySearch: function (query, term) {
var
termLength = term.length,
Expand Down

0 comments on commit a343501

Please sign in to comment.