Hi!
On Wed, Jul 18, 2007 at 05:41:46PM +0200, Chengcai He wrote:
[..]
> 
> speak in shortly: searching with conditions in find_by_contents, it got
> 16 total_hits, it display the first 10 result messages in first page,
> when i click to the next page, it display the next 6 results, but the
> total_hits changes to 6, and only got one page to display.

The reason for the odd behaviour you experience is as follows:
Aaf first queries ferret for your search results, it gets a
number of ids matching your query (and your paging). Now it uses this
set of ids to run a query against the DB, combining it with your own
conditions.

So in the end there is no simple way for aaf to determine the total
number of hits (without running the query more than once). 

Additionally it might happen that you get less results than expected,
since Ferret is delivering :limit hits, but these might well be cut down
to less than :limit by your active record conditions.


There are several ways to work around this:

- Get the total_hits separately by calling Model.find_by_contents
  without the paging options. Doesn't solve the second problem I
  mentioned above.

- Use :limit and :offset as part of your find_options, so the paging
  takes place in the database. Might work okay, didn't ever try it.

- Put the additional data in your index, too, and do not use 
  active_record conditions at all. Just append your additional query
  criteria to the query entered by the user.

- If you find a way how aaf could deliver consistent results in this
  scenario with any other method I can't think of right now, submit a
  patch :-)


Btw, do never ever directly append request parameters to conditions 
like this:

> @conditions = " 1 = 1";
> if !params[:dateRange].nil? && params[:dateRange] != ""
> @conditions += " and creationDate >= " + params[:dateRange]
> end
> if !params[:forumID].nil? && params[:forumID] != ""
> @conditions += " and forum_id = " + params[:forumID]
> end

but use something like

conditions = [ 'creationDate >= ? AND forum_id = ?', 
               params[:dateRange], forum_id  ]

to avoid exploitation of your app via sql injection. In newer Rails
there's a Hash based notation for the conditions, too (that might be
easier to assemble in your case).

cheers,
Jens



-- 
Jens Krämer
http://www.jkraemer.net/ - Blog
http://www.omdb.org/     - The new free film database
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk

Reply via email to