Reputation: 1155
I'm working on a search box for my site and want to build if from scratch (I know I probably shouldn't but it's a learning exercise as much as anything).
I am receiving a string from a text box and using the Jquery autocomplete function to display any results from Parse.
So far this works OK but when I type in a string that includes all the words in my database, but not in the correct order, I get no results.
e.g. if I search for "New article" it shows the article, whereas if I search for "article New" it doesn't.
I am retrieving the data with:
...
var searchString = document.getElementById("tags").value; // Get the search string
var str = searchString.split(" "); // Split it up into an array
var Articles = Parse.Object.extend("Articles");
var query = new Parse.Query(Articles);
query.containedIn("title", str); // This part doesn't work.
// query.contains("title", searchString); // I was using this and it works OK.
query.find({
...
EDIT:
Using the method suggested by bond below my cloud code is:
Parse.Cloud.define('searchArticles', function(request, response) {
console.log("About to request search terms"); // This never appears in cloud code logs
var input = request.params.searchTerms;
input = _.uniq(input);
input = _.filter(list, function(w) { return w.match(/^\w+$/); });
var searchQuery = new Parse.Query("Articles");
searchQuery.containedIn("titleArray", input);
searchQuery.find().then(function(articles) {
console.log("Success");
}, function(err) {
console.log("Failure");
});
});
and the function i'm using to call this is:
$("#searchBox").keyup(function() {
var availableArticles = [];
var searchString = document.getElementById("tags").value;
var str = searchString.split(" ");
Parse.Cloud.run('searchArticles', {"searchTerms":str}, {
success: function(articles) {
alert("Successful");
},
error: function(error) {
alert("Unsuccessful");
}
});
Note that none of the console.log's are ever written, and the alerts in the Parse.Cloud.run
function never appear. The cloud code is running as it is displayed in Parse cloud code logs.
Upvotes: 0
Views: 171
Reputation: 74
For search functions you may use a separate array column say keyWords. Where you save the keywords in title column in beforeSave
function of Articles class. Then you may call separate search function to search while you type to search. Something like this:
Parse.Cloud.beforeSave("Articles", function(request, response) {
// set/update Articles keywords
}
Parse.Cloud.define("searchArticles", function(request, response) {
var input = request.params.query; //
// optimizing input
input = _.uniq(input);
input = _.filter(list, function(w) { return w.match(/^\w+$/); });
var searchQuery = new Parse.Query("AddressInfo");
searchQuery.containsAll("keyWords", input);
searchQuery.find().then(function(articles) {
// return success response
}, function(err) {
// handle error
});
}
You may call searchArticles
from client. Running queries like containedIn
in Cloud than on client would be faster to give your search results.
Upvotes: 1