Basically what you're asking for sounds like substring matching. My answer is a "brute force" style of querying and it will become brittle / fall apart really quickly in real-world usage:
# keywords is an array of strings
def keyword_search(keywords)
query = User
keywords.each do |keyword|
query = query.where('department like ?', "%#{keyword}%")
end
query.to_a
end
So for your examples you'd get the following queries:
# Returns both users for "staff"
SELECT "users".* FROM "users" WHERE (department like '%staff%')
# Returns first user for "maintaining devops"
SELECT "users".* FROM "users" WHERE (department like '%maintaining%') AND (department like '%devops%')
# Returns first user for "maintaining devops"
SELECT "users".* FROM "users" WHERE (department like '%maintaining%')
# Returns first users for "facility"
SELECT "users".* FROM "users" WHERE (department like '%facility%')
Notice for this type of solution, it's an AND
query, so all the keywords will have to match to get a result. The %
sign in the query makes the query slightly fuzzier, so for %staff%
you'd get matches for the following:
staff
staffroom
flagstaffs
overstaff
Which you can adjust by dropping the %
s if you're looking for more exact matches.
As Eric suggested, anything more complicated than this is going to require a more advanced solution.
Personally I've used Solr/Websolr
and have had good success with it, but the query and search tuning takes a bit of work to understand and implement.