Wrong total_hits when using conditions in find_by_contents

In my model Topic:

acts_as_ferret({ :fields => {:username => {:store => :yes, :boost =>
30}, :subject => {:store => :yes, :boost => 20}, :body => {:store =>
:yes, :boost => 10}}, :remote => true }, { :analyzer =>
Ferret::Analysis::RegExpAnalyzer.new(/./, false) })

def self.full_text_search(q, options = {}, find_options = {})
return nil if q.nil? or q==“”
default_options = {:limit => 10, :page => 1}
options = default_options.merge options

get the offset based on what page we’re on

options[:offset] = options[:limit] * (options.delete(:page).to_i-1)

now do the query with our options

results = Topic.find_by_contents(q, options, find_options)
return [results.total_hits, results]
end

in my SearchController:

if params[:doSearch] == “true”
if params[:query] == “”
flash[:notice] = ‘Please enter some words to search on.’
else
@conditions = " 1 = 1";
if params[:dateRange] != “”
@conditions += " and creationDate >= " + params[:dateRange]
end
if params[:forumID] != “”
@conditions += " and forum_id = " + params[:forumID]
end

@total, @topics = Topic.full_text_search(params[:query], {:page =>
(params[:page]||1)}, {:conditions => @conditions})
@pages = pages_for(@total)
end

it always return only 10 search results. no more search result. i don’t
know why!

and this article doesn’t work!
http://www.ruby-forum.com/topic/93822

thanks!

I’ve input the username into the index, when i input the username to
search, it will return all the topics posted by the user, more than 10.
but when i use the
conditions in find_by_contents, it only return 10 results! I’m
completely losted!

it generate the mysql sql statement:
SELECT * FROM topics WHERE (topics.id in
(‘35’,‘68’,‘36’,‘69’,‘37’,‘17’,‘30’,‘29’,‘18’,‘31’) and 1 = 1 and
creationDate >= 20070201)
in production.log.

Hi!

That’s a nice bug you’ve found there - getting the real result count is
a bit tricky when the result set is limited by both ferret’s :limit
option and active record conditions. It’s one of those things I always
wanted to fix but finally forgot about :wink:

I committed a possible fix right now that comes at the cost of an
additional ferret query and a “select count(*) where …”. Would be nice
if you could try out the current trunk of aaf to see if this fixes the
problem.

If the additional queries and counting are too slow for you (but first,
give it a try ;-), you could eliminate the need for active record
conditions by indexing the forum_id and creationDate columns as
untokenized values and let ferret handle them.

Jens

On Wed, May 02, 2007 at 12:12:08PM +0200, Chengcai He wrote:

options = default_options.merge options

end
Wrong total_hits when using conditions in find_by_contents - Ferret - Ruby-Forum

thanks!


Posted via http://www.ruby-forum.com/.


Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk


Jens Krämer
webit! Gesellschaft für neue Medien mbH
Schnorrstraße 76 | 01069 Dresden
Telefon +49 351 46766-0 | Telefax +49 351 46766-66
[email protected] | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa

I just ran into a similar problem using find_by_contents through
has_many relationships.

Jens’ class_methods.rb Rev 187 solved my issues. Thanks!

masone

Jens K. wrote:

Hi!

That’s a nice bug you’ve found there - getting the real result count is
a bit tricky when the result set is limited by both ferret’s :limit
option and active record conditions. It’s one of those things I always
wanted to fix but finally forgot about :wink:

I committed a possible fix right now that comes at the cost of an
additional ferret query and a “select count(*) where …”. Would be nice
if you could try out the current trunk of aaf to see if this fixes the
problem.

If the additional queries and counting are too slow for you (but first,
give it a try ;-), you could eliminate the need for active record
conditions by indexing the forum_id and creationDate columns as
untokenized values and let ferret handle them.

Jens

On Wed, May 02, 2007 at 12:12:08PM +0200, Chengcai He wrote:

options = default_options.merge options

end
Wrong total_hits when using conditions in find_by_contents - Ferret - Ruby-Forum

thanks!


Posted via http://www.ruby-forum.com/.


Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk


Jens Kr�mer
webit! Gesellschaft f�r neue Medien mbH
Schnorrstra�e 76 | 01069 Dresden
Telefon +49 351 46766-0 | Telefax +49 351 46766-66
[email protected] | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa