I have a search that's working great for searching an address in a property listing. But I can't figure out how to then filter those results based on the values of a certain text field. Here is what I have:
search = Listing.search do
fulltext attributes[:address] + " -'Off Market'"
with(:field_BedsTotal).greater_than_or_equal_to attributes[:field_BedsTotal]
with(:field_BathsFull).greater_than_or_equal_to attributes[:field_BathsFull]
end
So it's searching for the user inputed address (discarding any that are off market), and only returning results with beds and bath greater or equal to what the user selects.
But there is another field called "field_PropertySubType" that has "Condo" or "Single Family" as values. I have a checkbox that the user can check (attributes[:condo], attributes[:single_family]). How can I filter the search results by these values of field_PropertySubType? I'm guessing with facets but I can't find enough documentation to help for my particular scenario. I did add the field_PropertySubType field to my searchable list as text. But if I add it to "fulltext" it only adds to the results (like anything that matches address OR condo") instead of filtering them (matching address AND condo). Thanks!
EDIT:
Trying out facet, but not quite getting the syntax correct:
searchable do
text :field_StreetNumber, :field_StreetName, :field_StreetSuffix, :field_City
string :field_PropertySubType, :field_PropertyType
end
def self.filter(attributes)
search = Listing.search do
fulltext attributes[:address]
facet :field_PropertySubType, exclude: with(:field_PropertySubType,"Single Family") if params[:single_family].present?
end
end
It ended up being:
facet :field_PropertySubType, exclude: [with(:field_PropertySubType, "Single Family")]
The "with" just needed brackets around it.