4

I am new to ROR. i am using thinking sphinx. I need to index with one boolean field. that is, i list out records which are active is true.

define_index do
  indexes car.name, :as => :car
  indexes car_model.car_make.name, :as => :car_make
  indexes city_name.city , :as=> :city_name
  indexes car_active, :as=>:is_active, :type=>:boolean, :default=>true
end

I need to list out car details which are belong to active is true. can you help me?

pat
  • 16,116
  • 5
  • 40
  • 46
Anandh L.v
  • 95
  • 1
  • 2
  • 11

1 Answers1

9

If you want to filter on a boolean, then it's much better to have it as an attribute in Sphinx, instead of a field. Fields are the text data people will search for, attributes are the things you as a developer will order and filter by.

So, to set up that boolean column as an attribute:

define_index do
  # fields
  indexes car.name, :as => :car
  indexes car_model.car_make.name, :as => :car_make
  indexes city_name.city , :as=> :city_name

  # attributes
  has car_active
end

And then filtering:

Model.search 'foo', :with => {:car_active => true}
pat
  • 16,116
  • 5
  • 40
  • 46
  • @pat when to use has and when to use indexes ? btw it helped me thanks :) – kamal Sep 21 '15 at 16:36
  • 2
    @kamal: when it's string data that someone may enter into a search field, use `indexes` (these are what Sphinx calls fields). For anything you as a developer will filter by, sort by, or group by - and often these are boolean, integer and timestamp values - use `has` (these are what Sphinx calls attributes). – pat Sep 21 '15 at 19:44