fivetwentysix
fivetwentysix

Reputation: 7487

How do I search for a phrase using search logic?

Imagine case scenario, you have a list of recipes that have ingredients as a text.

You want to see how many recipes contain "sesame oil".

The problem with default searchlogic searching using Recipe.ingredients_like("sesame oil") is that any recipe with sesame AND oil would come up, when I'm searching for "sesame oil" which is a problem when the recipe may contain things like "sesame seeds" + "corn oil"

Upvotes: 0

Views: 113

Answers (1)

maček
maček

Reputation: 77778

This will return Recipes that contain sesame oil (two words separated by a space)

Recipe.ingredients_like("sesame oil")
#=> SELECT * FROM `recipes` WHERE (recipes.ingredients LIKE '%sesame oil%') 

This will return Recipes that contain sesame or oil (any one of these words)

Recipe.ingredients_like_any("sesame", "oil")
#=> SELECT * FROM `recipes` WHERE (recipes.ingredients LIKE '%sesame%' OR recipes.ingredients LIKE '%oil%') 

This will return Recipes that contain sesame and oil (both words in any order)

Recipe.ingredients_like_all("sesame", "oil")
#=> SELECT * FROM `recipes` WHERE (recipes.ingredients LIKE '%sesame%' AND recipes.ingredients LIKE '%oil%') 

Upvotes: 1

Related Questions