Sunspot highlights not appearing
Asked Answered
C

1

9

I've gone through the docs in github: http://github.com/outoftime/sunspot to find solutions for highlighting, but it doesn't seem to work for me.

My Job model has something like this block (omitted some fields on purpose):

  searchable do
    text :name

    string :name, :stored => true
    time :updated_at
    time :created_at
    time :expires_on

I have this, which returns results:

search = Sunspot.search(Job) do
  keywords 'Senior', :fields => "name", :highlight => true
end

=> 0, :fl=>"* score", :"hl.simple.pre"=>"@@@hl@@@", :qf=>"name_text", :rows=>30, :"hl.simple.post"=>"@@@endhl@@@", :hl=>"on", :q=>"senior", :fq=>["type:Job"], :defType=>"dismax"}>

and getting the hits as such:

>> search.hits
=> [#, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #, #]

and here were the results of the query, displaying the name:

>> search.results.collect{|x| x.name}
=> ["Senior Associate, Executive Membership", "Senior International Costing Analyst", "Senior Process Engineer", "Deputy Senior Process Manager", "Senior Engineer (Rotating Equipment)", "Senior Technical Expert, Indonesia", "Senior Combustion Engineer", "Senior Project Engineer/ Engineering Manager", "Senior Substructure Design Specialist, Bangladesh", "Senior Supervision Engineer (Superstructure), Bangladesh", "Senior Program and Strategy Development Advisor- Consultant", "Senior Associate (Natural Resource Management Specialist)", "Senior Manager (Agriculture Market Development, Afghanistan)", "Senior Material Engineer (Main Bridge), Bangladesh", "Senior Resident Engineer (Main Bridge), Bangladesh", "Senior Resident Engineer (Main Bridge), Bangladesh", "Senior Material Engineer (Main Bridge), Bangladesh"]

and here comes my problem.. when I get the highlights, none were returned:

>> search.hits.collect{|x| x.highlight(:name)}
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]

and even this, doesn't return highlight hits:

>> search.hits.collect{|x| x.highlights}
=> [[], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], []]

Am I missing other flags or arguments?

Caiman answered 30/7, 2010 at 8:30 Comment(0)
C
6

I believe it has to do with whether the text field is stored. Does this make a difference:

searchable do
    text :name, :stored => true
    ...

That's how the :body field is configured in this example:

http://github.com/outoftime/sunspot-rails-example/blob/master/app/models/post.rb

The :body field is one that the highlights are queried for in the controller:

http://github.com/outoftime/sunspot-rails-example/blob/master/app/controllers/searches_controller.rb

Hope that does it!

Cryoscopy answered 22/10, 2010 at 4:22 Comment(1)
This is the point, I tried this answer and it worked. The fields which you want to highlight has to be stored. Do not forget to reindex solr again.. and have the necessary dynamic fields in place (such as *_texts).Lakisha

© 2022 - 2024 — McMap. All rights reserved.