I'm trying to build an advanced search option (similar to Twitter's). Users can enter the words included, words excluded, words containing, exact phrase, and such in a search query.
I'm using Searchkick to do this. Particularity Searckick's regexp searches.
Here is what I'm doing to find the companies which have the words "Facebook or less" in their slogans.
Company.search("Be", where: { short_desc: /.*(#{ar}).*/ })
This works well. But, how would I do a negative of this search?
Doing something like, Company.search("Be", where: { short_desc: /.*(?!(#{ar})).*/ })
is not yielding results. Also, can I combine a search that has words to be included AND words to be excluded?
May be this helps you, try this:
Business.search("Be", where: { short_desc: {not: /.*(#{ar}).*/} })
And check the results. You can combine this with AND
or OR
operators. Anywhere using the where
clause. Extracted from the gem readme file:
where: {
expires_at: {gt: Time.now}, # lt, gte, lte also available
orders_count: 1..10, # equivalent to {gte: 1, lte: 10}
aisle_id: [25, 30], # in
store_id: {not: 2}, # not
aisle_id: {not: [25, 30]}, # not in
user_ids: {all: [1, 3]}, # all elements in array
category: /frozen .+/, # regexp
or: [
[{in_stock: true}, {backordered: true}]
]
}
This is how you can combine it with query.
Hope this helps.