Chris Humpry
Chris Humpry

Reputation: 39

query using Parse

I want to create a query that will check in a specific class if something exist in 2 fields (I want to display for each one meaning OR and not AND) this is what I did but it's not working (Based on some examples I saw)

    PFQuery *query = [PFQuery queryWithClassName: self.parseClassName];
   [query whereKeyExists:@"name"];  //this is based on whatever query you are trying to accomplish
    [query whereKeyExists:@"city"]; //this is based on whatever query you are trying to accomplish
    [query whereKey:@"description" containsString:searchTerm];

how should I do this?

** I want to get in my query all the result the contain the searchTeam from all fields (name,city,description)

Thanks

Upvotes: 2

Views: 2094

Answers (3)

Logan
Logan

Reputation: 53142

You want this:

NSString *className = @"YourParseClassName";
NSString *searchTerm = @"YourSearchTerm";
PFQuery *nameQuery = [PFQuery queryWithClassName:className];
[nameQuery whereKey:@"name" containsString:searchTerm];
PFQuery *cityQuery = [PFQuery queryWithClassName:className];
[cityQuery whereKey:@"city" containsString:searchTerm];
PFQuery *descriptionQuery = [PFQuery queryWithClassName:className];
[descriptionQuery whereKey:@"description" containsString:searchTerm];

PFQuery *allQuery = [PFQuery orQueryWithSubqueries:@[nameQuery, cityQuery, descriptionQuery]];
[allQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        NSLog(@"Found objects: %@", objects);
    } else {
        NSLog(@"Received error: %@", error);
    }
}];

This way, you create your three separate queries, but then combine them into one greater query to return all the objects at once.

But keep in mind, Parse Regex searches are pretty limiting, and this might not be the most scalable option if you want a lot of searching to be done. For lightweight work or an app with occasional searching, this should be fine.

Upvotes: 0

royherma
royherma

Reputation: 4203

the answer to this question is quite simple, just needed to add multiple whereKey method calls to the same query object. For Example:

   PFQuery *query = [PFQuery queryWithClassName: self.parseClassName];
   [query whereKey:@"name" equal:@"someName"];
   [query whereKey:@"city" equal:@"someCity"];
   [query whereKey:@"description" containsString:searchTerm];

This will evaluate as the original desired statement.

Upvotes: 3

Zoe
Zoe

Reputation: 11

If I understood your question properly, you want to check if your searchTerm is in either name, city, or description. The way to do this is:

PFQuery * nameQuery = [PFQuery queryWithClassName: self.parseClassName];
[nameQuery whereKey:"name" containsString:searchTerm];
PFQuery * cityQuery = [PFQuery queryWithClassName: self.parseClassName];
[nameQuery whereKey:"city" containsString:searchTerm];
PFQuery * descriptionQuery = [PFQuery queryWithClassName: self.parseClassName];
[nameQuery whereKey:"description" containsString:searchTerm];

PFQuery * query = [PFQuery orQueryWithSubqueries:@[nameQuery, cityQuery, descriptionQuery]];

This creates a compound or query from an array of queries. What you were doing before by adding more constraints to the same query was the same as writing an AND query.

IE: keyExists:@"name" AND keyExists:@"city" AND description CONTAINS searchTerm

Upvotes: 1

Related Questions