Reputation: 4171
Using Rails 4.2 with searchkick and elasticsearch, we have some where conditions set up to find users:
where_data = {
region: "North America"
}
results = Person.search( query, where: where_data )
This works as expected and finds a person when Person.region
is "North America".
But there are other situations where the person has multiple regions, including North America: "Africa; North America". In these cases the person is not found.
How can I customize the where data or calls so that it will operate more like LIKE
in sql, and find any matching instances of the full string?
Upvotes: 1
Views: 2627
Reputation: 61
You can pass instead of a hash an array. So you can have:
where_data = [
"region LIKE North America"
]
results = Person.search( query, where: where_data )
This way the where clause will use LIKE to look up for your results.
Upvotes: 3
Reputation: 792
To search for a Person where region contains "North America", first try:
Person.search "North America", fields: [:region]
If that doesn't work, try adding this to the Person Model:
class Person < ActiveRecord::Base
searchkick word_middle: [:region]
And then (after you reindex)
Person.search "North America", fields: [:region], match: :word_middle
If that still doesn't work, I would suggest making :region
a collection in ES (please let me know if you are interested, I have done this before using searchkick but don't recall the exact code. I can pull it up and post for you later)
Upvotes: 2
Reputation: 26812
I'm not familiar with Searchkik but I am a daily elastic user. I would think that if you submitted a space delimited string of the region options it would match them all.
option_string = array_of_options.join(" ")
Using the JSON syntax with elasticsearch-rails gems you should be able to achieve this.
where_data = {
region: option_string
}
results = Person.search( query, where: where_data )
Upvotes: 0