facet.prefix

133 views
Skip to first unread message

Kieran

unread,
Feb 11, 2010, 10:29:06 AM2/11/10
to Sunspot
Hi, I'm wondering whether there are any plans for Sunspot to support
facet prefixes - i.e. to limit the faceted terms to those starting
with a given string prefix?

I ask, because I am trying to implement an autocomplete feature, which
depends on being able to search for terms that start with a given
prefix. It appears that Solr has this feature (via the facet.prefix
parameter):

http://wiki.apache.org/solr/SimpleFacetParameters#facet.prefix

(with an example which illustrates almost exactly what I'm trying to
do: http://wiki.apache.org/solr/SimpleFacetParameters#Facet_prefix_.28term_suggest.29)

It might appear that this behaviour can be obtained in the Sunspot
0.10 release using a "starts_with" query, coupled with faceting on the
field you're searching over. However, if the field has multiple values
(as in my case), then the facet list gets polluted with field values
that you're not interested in. Obviously, I do have a solution which
is to retrieve all facet values, and do some post-processing, however
this seems a shame if there is functionality in Solr to do this.

My experience of Sunspot has been hugely positive. I've found it
tremendously easy to implement, and to work with. Thank you!

Kieran

Mat Brown

unread,
Feb 11, 2010, 10:42:35 AM2/11/10
to ruby-s...@googlegroups.com
Hey Kieran,

Not a bad idea - feel free to file a ticket at
http://outoftime.lighthouseapp.com/projects/20339-sunspot/tickets - of
course a patch attached to the ticket would be even better!

To get that functionality right now, you can use the adjust_solr_params method:

Post.search do
facet(:my_field)
adjust_solr_params do |params|
params[:"f.#{Sunspot::Setup.for(Post).field(:my_field).indexed_name}.facet.prefix"]
= "someprefix"
end
end

The above solution interacts with the non-public API so it's
definitely not an ideal solution, but it should work.

Thanks!
Mat

> --
> You received this message because you are subscribed to the Google Groups "Sunspot" group.
> To post to this group, send email to ruby-s...@googlegroups.com.
> To unsubscribe from this group, send email to ruby-sunspot...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ruby-sunspot?hl=en.
>
>

Kieran

unread,
Feb 11, 2010, 11:25:18 AM2/11/10
to Sunspot
Mat,
Many thanks for such a prompt response.
I must admit I did briefly attempt to patch the code, but ended up
getting a bit lost (I'm still a bit of ruby nuby). Looking again,
though, it looks like I'll just need to amend
AbstractFieldFacet.to_params() such that it generates a facet.prefix
param when it encounters a {:prefix => some_prefix} option.
I'll try both approaches, and post a patch if I get anywhere!
Many thanks again.
Kieran


On Feb 11, 3:42 pm, Mat Brown <m...@patch.com> wrote:
> Hey Kieran,
>

> Not a bad idea - feel free to file a ticket athttp://outoftime.lighthouseapp.com/projects/20339-sunspot/tickets- of


> course a patch attached to the ticket would be even better!
>
> To get that functionality right now, you can use the adjust_solr_params method:
>
> Post.search do
>   facet(:my_field)
>   adjust_solr_params do |params|
>     params[:"f.#{Sunspot::Setup.for(Post).field(:my_field).indexed_name}.facet.prefix"]
> = "someprefix"
>   end
> end
>
> The above solution interacts with the non-public API so it's
> definitely not an ideal solution, but it should work.
>
> Thanks!
> Mat
>
>
>
>
>
> On Thu, Feb 11, 2010 at 10:29, Kieran <kierantopp...@gmail.com> wrote:
> > Hi, I'm wondering whether there are any plans for Sunspot to support
> > facet prefixes - i.e. to limit the faceted terms to those starting
> > with a given string prefix?
>
> > I ask, because I am trying to implement an autocomplete feature, which
> > depends on being able to search for terms that start with a given
> > prefix. It appears that Solr has this feature (via the facet.prefix
> > parameter):
>
> >http://wiki.apache.org/solr/SimpleFacetParameters#facet.prefix
>
> > (with an example which illustrates almost exactly what I'm trying to

> > do:http://wiki.apache.org/solr/SimpleFacetParameters#Facet_prefix_.28ter...)

Mat Brown

unread,
Feb 11, 2010, 11:38:50 AM2/11/10
to ruby-s...@googlegroups.com
Hey Kieran,

Yep, you're on the right track with that. AbstractFieldFacet also has
a private helper method that will generate the appropriate parameter
key for you.

Mat

Kieran

unread,
Feb 12, 2010, 5:21:18 AM2/12/10
to Sunspot
Thanks Mat,
Your "adjust_solr_params" suggestion works great. Will be looking into
creating a proper patch over the next couple of days. In case it's of
interest to anyone else reading this thread, I had to slightly amend
your suggestion - not because it contained an error, but because:
1. I was using it inside a "search do |query| ... end" block. i.e. I
needed to qualify the call to adjust_solr_params with "query."
2. I was querying a dynamic field, so had to use
dynamic_field_factory(:custom).build(:my_field)
instead of the much simpler
field(:my_field)

Amendment follows (from http://gist.github.com/302130):

query.adjust_solr_params do |params|

params[:"f.#{Sunspot::Setup.for(MyModel).dynamic_field_factory(:custom).build(:my_field).indexed_name}.facet.prefix"]
= my_leading_str
end

Kieran


On Feb 11, 4:38 pm, Mat Brown <m...@patch.com> wrote:
> Hey Kieran,
>

> Yep, you're on the right track with that. AbstractFieldFacet also has
> a private helper method that will generate the appropriate parameter
> key for you.
>
> Mat
>

Mat Brown

unread,
Feb 12, 2010, 8:43:35 AM2/12/10
to ruby-s...@googlegroups.com

Sweet!

Reply all
Reply to author
Forward
0 new messages