0

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.

2
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on Stack Overflow. See "Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?. Commented May 27, 2015 at 6:01
  • To make it more clear, please use quotations around strings you are reffering to, e.g. in the phrase: "New article" it shows "this article", whearas if I search for "article New" it doesn't Commented May 27, 2015 at 6:07

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi @bond, I don't want to use containsAll, I would like any string from the array of search terms to be contained in the title of the article. Can I do that? So if it has "article" but not "New" then it should still show the article titled "New article".
Yes you may pass "article" to searchArticles and use containedIn instead of containsAll. I just gave the sample solution, you may modify it accordingly.
Great - working through this now. I'm trying to figure out how I pass searchKw but am guessing this is on the Parse docs so will work through that and come back to you.. Much appreciated @bond.
I will thanks... can you confirm in your code above, should searchKw be input if I am passing in an array to this cloud function?
I think I'm having issues passing the data in. Would I be correct in thinking that I can receive the string (which is working) then create an array using var searchTerms = searchQuery.split(" ") and then use Parse.Cloud.run('searchArticles', str, { to call the cloud function? Note: When the user saves a new article it creates an array of terms and saves them to "titleArray" in the Parse database.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.