0

I need to access my rails model instance from inside the searchable block as indicated below.

class Product

        include MongoMapper::Document
        include Sunspot::Rails::Searchable

        key :field_names, Array

        searchable do |ss|

            self.field_names.each do |field| 
              ss.double field[:name] do 
                field[:value] 
              end 
            end 
        end

end

does anyone know how to do this via Sunspot ?

I have a field_names array on each product instance that is different per product so i need to access it.

Thanks a lot Rick

Rick Moss
  • 926
  • 1
  • 17
  • 34

2 Answers2

0

you mean this?

def Foo

  attr_accessible  :id, :title

  def fields
    ['something']
  end

  searchable do 

    integer :id
    string  :title

    string :fields, :multiple => true do 
      self.fields
    end

  end

end
zagros
  • 134
  • 5
  • zagros thanks but no. i need to access Foo's instance outside of the string :fields block not from inside it. any ideas ? – Rick Moss Feb 11 '14 at 21:13
  • That makes no sense - in that execution context, self would be the `Foo` class, not an instance of `Foo`. Sunspot sets up its stuff at class definition time. Instance values have no bearing on it. – Chris Heald Feb 11 '14 at 21:55
  • Chris Heald thanks this is my problem. can you offer a solution? – Rick Moss Feb 11 '14 at 22:06
  • i can't find any reference to what i'm trying to demonstrate you but here's the __[closest post](https://groups.google.com/forum/#!topic/ruby-sunspot/5xanVMY6ekQ)__ ... so to dynamically add fields, you're best bet is the approach above (in 2nd answer) or a modification of it – zagros Feb 11 '14 at 22:09
  • I don't think that you're coming at this right. Creating arbitrary searchable fields on your table is going to make those fields searchable for *all* records on that table, not just the ones with that field. This doesn't map well to Sunspot - you may need to roll some custom behavior to get what you want. – Chris Heald Feb 11 '14 at 22:10
  • Chris Heald thanks. Actually my model is a mongo_mapper not active record but didnt want to confuse if people didnt know mango_mapper. So different fields for different documents / products. Does that make more sense ? – Rick Moss Feb 11 '14 at 22:25
  • It does, but you basically need to create indexes on all your possible fields. If users can define fields at runtime, that is not going to play well with Solr at all. – Chris Heald Feb 11 '14 at 22:27
  • Chris Heald thanks. users can not define fields at runtime. I control the fields being created and they are only a few fields per product category but to get to the fields on the category for a product i need to access the product instance to know which category it is in, to lookup the fields i want to search on for that product. Does that make more sense ? sorry was trying to simplify the problem obviously too much!! – Rick Moss Feb 11 '14 at 22:31
0

well inside there, you're inside a different evaluation context (Solr::DSL or something like that). That's to provide the ability to have those keywords like "integer, string". Looks like you're trying to evaluate dynamic attributes/filters .. .. so see my modified response (below)

you mean this?

def Foo

  attr_accessible  :id, :title

  @fields_to_dynamically_add = ['title']


  searchable do |s|

    s.integer :id
    s.string  :title

    @fields_to_dynamically_add.each do |f|
      s.string f.to_sym
    end

  end

end

PS: have not added fields to searchable blocks dynamically every myself (although the above works)

zagros
  • 134
  • 5