I'm looking for something like:
if(magicSearch("hot pizza","We sell pizza that is really hot") {//Found!}
I only want a "true" if both words appear (any order) and I want it to ignore things like "a", "and", "the", etc., in the text to be searched for.
(I'm sure there is better terminology to explain what I'm looking for)
I've seen various DB engines support this type of text search (e.g. Mongodb) but I need a simply JavaScript way of testing strings. I can build it, but it feels like something that must already be out there somewhere.
Greg
You can use the indexOf function in a simple loop.
//Use this if you don't need to find exact matches of words
function magicSearch(needles, haystack) {
var searchTerms = (needles instanceof Array) ? needles : [needles];
for(var i = 0; i < searchTerms.length; i++) {
if(haystack.indexOf(searchTerms[i]) === -1) {
return false;
}
}
return true;
}
//Use this if you want to find exact matches of words
function magicSearch2(needles, haystack) {
var searchTerms = (needles instanceof Array) ? needles : [needles],
haystackArray = haystack.split(' '),
index,
found = 0;
for(var i = 0; i < haystackArray.length; i++) {
index = searchTerms.indexOf(haystackArray[i]);
if(index !== -1) {
delete searchTerms[i];
found++;
if(found = searchTerms.length) {
return true;
}
}
}
return false;
}
if(magicSearch(["hot", "pizza"],"We sell pizza that is really hot") {
console.log("FOUND");
}
if(magicSearch2(["hot", "pizza"],"We sell pizza that is really hot")) {
console.log("FOUND");
}
If you use underscore.js, you can have some relative simple implementation
var stopwords_set = ["a", "and", "the" ];
var magicSearch = function (keywords, str) {
var content_set = {};
var keywords_set = {};
_.each(keywords.split(' '), function(item){
keywords_set[item.toLowerCase()] = 1;
});
_.each(str.split(' '), function(item){
content_set[item.toLowerCase()] = 1;
});
//convert input to 2 sets excluding stop words
content_set = _.without(_.keys(content_set), stopwords_set);
keywords_set = _.without(_.keys(keywords_set), stopwords_set);
//check the intersecion
var value = _.intersection(content_set, keywords_set).length == keywords_set.length
return value;
}
magicSearch("hot pizza","We sell pizza that is really hot");