Tzahi Moyal
Tzahi Moyal

Reputation: 83

Parse - Recipe search by ingredient

I have the following 2 classes in my parse, and am having some trouble querying them for the results I want. I'm trying to search for recipes by ingredients.

Here are my class: Ingredients

+---------------+---------+
| ingredientId  | name    |
+---------------+---------+
|             1 | tomato  |
|             2 | garlic  |
|             3 | banana  |
|             4 | chicken |
|             5 | beef    |
|             6 | onion   |
|             7 | salt    |
+---------------+---------+

Recipes

+-----------+------------------+----------------------+
| recipeId  | name             | Ingredients (array)  |
+-----------+------------------+----------------------+
|         1 | hamburger        | {5,7,2}              |
|         2 | salad            | {1,2,6}              |
|         3 | garlic chicken   | {2,4}                |
+-----------+------------------+----------------------+

I'm trying to make query with to find what recipes i can make with given ingredients.

Example: ingredients (1,2,4) will response with garlic chicken recipe because i have all the ingredients in this recipe.

any help would be much appreciated!

Upvotes: 1

Views: 298

Answers (2)

userAndroid
userAndroid

Reputation: 586

Make a query to save all the data of first table in two array ..one for ingredients name and another is for ingredients id.

then again move to second table apply query for fetching arrays for a perticular receipe and save them in List (in android) then....

when any ingredients will be entered to searched any recipe..you have to check that ingredients from the array of ingredients (which you have made from first table)..then pass the perticular ingredients id to the List (from second table) to get receipe name..

If you are using android os for coding ,then use entity for this..or let me know I will add code for this.

Upvotes: 0

David Jirman
David Jirman

Reputation: 1216

Is there a specific reason why do you use Array instead of Relation here? I believe Relations will untie your hands ...

I would suggest to convert Recipes.Ingredients into Relation datatype and keep assigning ingredients as single relations.

Querying then will be easier (you didn't specify the language so I'll stick to objective-c here):

// for demonstration purposes these are the ingredients I have and am looking for 
// suitable recipe
NSArray *_arrIngredients = @[@"water", @"butter", @"meat", @"bread", @"salt"];

// get objectId for all ingredients I'm searching for
PFQuery *_query = [PFQuery queryWithClassName:@"Ingredients"];
[_query whereKey:@"name" containedIn:_arrIngredients];

// query existing relations
PFQuery *mainQuery = [PFQuery queryWithClassName:@"Recipes"];
[mainQuery whereKey:@"Ingredients" matchesQuery:_query];
[mainQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if(error)
    {
        NSLog(@"%@", [error localizedDescription]);
    }
    else
    {
        // print out matching objects
        // your code here
        for(PFObject *object in objects)
        {
            NSLog(@"%@: %@", [object objectForKey:@"name"], [object objectId]);
        }
    }
}];

Hope it helps!

Upvotes: 0

Related Questions