Search code examples
ruby-on-railsrubysolrsunspot

indexing jsonb fields with sunspot


How can I index field that I needed in jsonb fields with sunspot? I have the model Man:

 #<Man id: 1, name: "John Doe", type: "Man", r_id: "5734", fields: {"message"=>"hello world!", "sex"=>2, "uid"=>5734, "domain"=>"www.old-man.com"}, created_at: "2018-07-12 12:44:07", updated_at: "2018-07-12 12:44:07">

So, I'm trying to indicate searchable fields like this:

  searchable do
      text :fields, stored: true
    end

and it will index all fields. But how to indicate only field "message" in fields as a searchable field?


Solution

  • From gem documentation:

    searchable do
      text :title, :body
      text :comments do
        comments.map { |comment| comment.body }
      end
      ...
    end
    

    That doesn't work for you?

    searchable do
      text :fields do
        fields["message"]
      end
    end