sunspot icon indicating copy to clipboard operation
sunspot copied to clipboard

Wrong "qf" when joining text fields

Open ronyvieira opened this issue 11 years ago • 4 comments

Sunspot is generating wrong "qf" when joining text. When you search for a text field, sunspot looks for a qf named "my_attribute_texts" and it stores a array of strings on solr like:

<doc>
    <str name="id">Customer 1</str>
    <arr name="my_attribute_texts">
         <str>MY NAME</str>
    </arr>
</doc>

But when joining, on the subquery it looks for "my_attribute_text" without the "s"

So when the search reaches solr it simply answer 0 results.

ronyvieira avatar Jul 13 '15 21:07 ronyvieira

Hi @ronyvieira - can you provide an example of your "joining text fields" query? And what is meant by "subquery"? Thanks!

davidjoliver avatar Jul 28 '15 15:07 davidjoliver

Hey @davidjoliver

Here is the example in the doc:

Models:

class Photo < ActiveRecord::Base
  searchable do
    text :description
    string :caption
    time :created_at
    integer :photo_container_id
  end
end

class PhotoContainer < ActiveRecord::Base
  searchable do
    text :name
    integer :id # missing in the doc
    join(:description, :target => Photo, :type => :text, :join => { :from => :photo_container_id, :to => :id })
    join(:caption, :target => Photo, :type => :string, :join => { :from => :photo_container_id, :to => :id })
    join(:photos_created, :target => Photo, :type => :time, :join => { :from => :photo_container_id, :to => :id }, :as => 'created_at_d')
  end
end

When i try to search for description field in the PhotoContainer it returns nothing

PhotoContainer.search do
   fulltext("description 1", :fields => :description)
end

# ...produces:
# <Sunspot::Search:
#    {:fq=>["type:PhotoContainer"], 
#      :q=>"_query_:\"{!join from=photo_container_id_i to=id_i v=$qPhoto44495900         
#     fq=$fqPhoto44495900}\"", "qPhoto44495900"=>"_query_:\"{!edismax    
#     qf='description_text'}description 1\"", 
#    "fqPhoto44495900"=>"type:Photo", :fl=>"* score", :start=>0, :rows=>30}> 

#  []

if i do the same query for the photo it returns the photo i was looking for

Photo.search do
   fulltext("description 1", :fields => :description)
end

# ...produces:
# <Sunspot::Search:
# {:fq=>["type:Photo"], 
# :q=>"description 1", :fl=>"* score", 
# :qf=>"description_text", :defType=>"edismax", :start=>0, :rows=>30}> 

 # [#<Photo id: 1, description: "Description 1", caption: "caption 1", photo_container_id: 1, created_at: "2000-01-01 17:29:26">] 

ronyvieira avatar Aug 06 '15 18:08 ronyvieira

I'm having the same problem as @ronyvieira. Is there already a solution?

davycardinaal avatar Mar 21 '16 14:03 davycardinaal

Suffix s added when you define a field with stored: true. You have to mention the same in your join configuration also like below.

    join(:description, :target => Photo, :type => :text, :join => { :from => :photo_container_id, :to => :id }, stored: true)
    join(:caption, :target => Photo, :type => :string, :join => { :from => :photo_container_id, :to => :id }, stored: true)
    join(:photos_created, :target => Photo, :type => :time, :join => { :from => :photo_container_id, :to => :id }, stored: true)

By default it will be query only with description_text, Once you added stored: true to your join query sunspot will append suffix s with the field type field_name + type + stored.

Otherwise you can add as: :description_texts, This does't requrire stored: true. you should provide the exact field name you are going to query.

join(:description, :target => Photo, :type => :text, :join => { :from => :photo_container_id, :to => :id }, :as => 'description_texts')
join(:caption, :target => Photo, :type => :string, :join => { :from => :photo_container_id, :to => :id }, :as => 'caption_ss')
join(:photos_created, :target => Photo, :type => :time, :join => { :from => :photo_container_id, :to => :id }, :as => 'created_at_ds')
[6] pry(main)> PhotoContainer.search {fulltext("description 1", :fields => :description)}.hits
D, [2016-11-20T19:03:28.241685 #27357] DEBUG -- :   SOLR Request (64.3ms)  [ path=select parameters={fq: ["type:PhotoContainer"], q: "_query_:"{!join from=photo_container_id_is to=id_is v=$qPhoto35041520 fq=$fqPhoto35041520}"", qPhoto35041520: "_query_:"{!edismax qf='description_texts'}description 1"", fqPhoto35041520: "type:Photo", fl: "* score", start: 0, rows: 30} ]
=> [#<Sunspot::Search::Hit:PhotoContainer 1>]

ponnusamygit avatar Nov 20 '16 13:11 ponnusamygit