Return ONLY total_hits without querying from real database


#1

Hey guys,

I know I can run search(q).total_hits, but if I try to put :limit=>0 it
gives me an error. I don’t want it actually query any of the results, I
just want it to tell me how many total_hits I would have if I wanted to
search it.

How can I do this?


#2

On Wed, May 16, 2007 at 02:17:44AM +0200, Aryk G. wrote:

Hey guys,

I know I can run search(q).total_hits, but if I try to put :limit=>0 it
gives me an error. I don’t want it actually query any of the results, I
just want it to tell me how many total_hits I would have if I wanted to
search it.

How can I do this?

Model.total_hits(query)

Jens


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
removed_email_address@domain.invalid | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa


#3

Hi Jens,

I don’t have that method, could it have something to do with the fact
that Im using

Ferret::MixinActs::ARFerret

Do I need to upgrade or can I just add that function?

Aryk

Jens K. wrote:

On Wed, May 16, 2007 at 02:17:44AM +0200, Aryk G. wrote:

Hey guys,

I know I can run search(q).total_hits, but if I try to put :limit=>0 it
gives me an error. I don’t want it actually query any of the results, I
just want it to tell me how many total_hits I would have if I wanted to
search it.

How can I do this?

Model.total_hits(query)

Jens


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
removed_email_address@domain.invalid | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa


#4

On Wed, May 16, 2007 at 09:29:56PM +0200, Aryk G. wrote:

Hi Jens,

I don’t have that method, could it have something to do with the fact
that Im using

Ferret::MixinActs::ARFerret

Do I need to upgrade or can I just add that function?

I’m not sure what you’re using there - I was thinking it’s
acts_as_ferret because of the method name.

Jens

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
removed_email_address@domain.invalid | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa


#5

On Fri, May 18, 2007 at 08:33:22PM +0200, Aryk G. wrote:

Here is what acts_as_ferret.rb says, maybe this is a really old version
or something.

yes, seems so. You should upgrade to the current stable version.

Jens


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
removed_email_address@domain.invalid | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa


#6

Here is what acts_as_ferret.rb says, maybe this is a really old version
or something.

The Rails ActiveRecord Ferret Mixin.

This mixin adds full text search capabilities to any Rails model.

The current version emerged from on the original acts_as_ferret plugin

done by

Kasper W. and a modified version done by Thomas L., which

both can be

found on the Ferret Wiki:

http://ferret.davebalmain.com/trac/wiki/FerretOnRails.

basic usage:

include the following in your model class (specifiying the fields you

want to get indexed):

acts_as_ferret :fields => [ ‘title’, ‘description’ ]

now you can use ModelClass.find_by_contents(query) to find instances

of your model

whose indexed fields match a given query. All query terms are required

by default, but

explicit OR queries are possible. This differs from the ferret

default, but imho is the more

often needed/expected behaviour (more query terms result in less

results).

Released under the MIT license.

Authors:

Kasper W. Nielsen-Refs (original author)

Jens K. removed_email_address@domain.invalid (active maintainer)


#7

On Tue, May 22, 2007 at 04:18:05AM +0200, Aryk G. wrote:

Hey Jens,

I upgraded to the latest version. Everything works great, it even
queries the results more efficiently. Great job!

thanks :slight_smile:

I don’t see any method for total_hits for multi_search. The
model.total_hits works fine when you are just searching on one model.

Any idea how to do only total_hits with multi_search?

That was indeed missing. I just committed a fix for this to trunk. now
you can give total_hits additional model classes with the :models
option:

A.total_hits(query, :models => [B, C])

Also, I added a little fix to multi_search to allow users to include
associations even if it works on one model but not the other. Here is
what the code looks like. It currently works great on one-level include
and somewhat hacky on two-level associations. Maybe you can find some
use for it?

http://pastie.caboo.se/63435

thanks, I integrated this :slight_smile:

Jens


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
removed_email_address@domain.invalid | www.webit.de

Amtsgericht Dresden | HRB 15422
GF Sven Haubold, Hagen Malessa


#8

Hey Jens,

I upgraded to the latest version. Everything works great, it even
queries the results more efficiently. Great job!

I don’t see any method for total_hits for multi_search. The
model.total_hits works fine when you are just searching on one model.

Any idea how to do only total_hits with multi_search?

Also, I added a little fix to multi_search to allow users to include
associations even if it works on one model but not the other. Here is
what the code looks like. It currently works great on one-level include
and somewhat hacky on two-level associations. Maybe you can find some
use for it?

http://pastie.caboo.se/63435

Aryk