Re: Sunspot partial keyword matching?

576 views
Skip to first unread message

Mat Brown

unread,
Nov 15, 2009, 2:10:01 AM11/15/09
to Aaron Soules, Sunspot

Hi Aaron,

By partial keywords I assume you mean prefix matching (e.g., "pizz" matches "pizza"). Sunspot uses Solr's DisMax handler for keyword search, which doesn't have any prefix support. You can manually parse out the keyword(s) that you want to prefix and then use a text_fields {} block with a starts_with restriction inside it, though. A future version of Sunspot might provide a more automated way of doing the same.

Mat

On Nov 14, 2009 11:49 AM, "Aaron Soules" <aso...@gmail.com> wrote:

Hello!

First, thanks for Sunspot!

I'm new to Solr, but trying out the WebSolr support on Heroku. I would like to be able to search partial keywords, but I've been searching for hours and cannot find any documentation of how exactly that works with Solr. Any help would be greatly appreciated!

Thanks,
Aaron

Aaron Soules

unread,
Nov 15, 2009, 11:28:42 AM11/15/09
to Sunspot
Thanks Mat,

Just for kicks, what is the name of the Solr handler that does support
prefix matching? I was envisioning using it for something like
stackoverflow's question suggestions when you're writing a new
question. Uservoice also has something similar. Perhaps I'm looking in
the wrong place?

Thanks,
Aaron

On Nov 15, 2:10 am, Mat Brown <m...@patch.com> wrote:
> Hi Aaron,
>
> By partial keywords I assume you mean prefix matching (e.g., "pizz" matches
> "pizza"). Sunspot uses Solr's DisMax handler for keyword search, which
> doesn't have any prefix support. You can manually parse out the keyword(s)
> that you want to prefix and then use a text_fields {} block with a
> starts_with restriction inside it, though. A future version of Sunspot might
> provide a more automated way of doing the same.
>
> Mat
>

Mat Brown

unread,
Nov 15, 2009, 12:18:26 PM11/15/09
to ruby-s...@googlegroups.com
Hey Aaron,

The standard Solr/Lucene boolean parser accepts prefixes - the :fq
param is the best way to get that to Solr when you're doing a fulltext
search, and that's the param that attribute scoping uses. So I'm
suggesting something along the lines of:

words = params[:keywords].split(/\s+/)
prefix, full_words = words.pop, words.join(' ')
Sunspot.search(Post) do
keywords(full_words, :fields => :title)
text_fields do
with(:title).starts_with(prefix)
end
end

Hope that helps!
Mat

Randy

unread,
Nov 28, 2009, 6:55:43 PM11/28/09
to Sunspot
I am using Sunspot 0.10.8 but I get an exception when I try to use the
starts_with restriction.

undefined method `starts_with' for #<Sunspot::DSL::Restriction:
0x6432974>
/Users/randysimon/src/iptvcms/trunk/app/controllers/
episodes_controller.rb:666:in `get_episodes_in_show'
/opt/local/lib/ruby/gems/1.8/gems/sunspot-0.10.8/lib/sunspot/
util.rb:86:in `instance_eval'
/opt/local/lib/ruby/gems/1.8/gems/sunspot-0.10.8/lib/sunspot/
util.rb:86:in `instance_eval_or_call'
/opt/local/lib/ruby/gems/1.8/gems/sunspot-0.10.8/lib/sunspot/dsl/
scope.rb:221:in `text_fields'

After poking around through the code I found that it is really
"starting_with" and not "starts_with". Hope this helps others.

Randy

Randy

unread,
Nov 29, 2009, 12:12:47 PM11/29/09
to Sunspot
Mat's example above only show how you can do partial keyword matching
for a single field. In my case, I want to do partial matching on all
text fields and I don't want to have to know the name of all the
fields when I construct the search. Therefore, I modified Mat's
example code to show how you can do partial keyword matching for all
text fields.

all_text_fields = Sunspot::Setup.for(Post).all_text_fields.collect
(&:name)
words = params[:keywords].split(/\s+/)
prefix, full_words = words.pop, words.join(' ')
Sunspot.search(Post) do |query|
query.keywords(full_words, :fields => all_text_fields)
text_fields do |text_fields_query|
text_fields_query.any_of do |any_of_query|
all_text_fields.each do |text_field|
any_of_query.with(text_field).starting_with(prefix)
end
end
end
end

This works pretty good for me. However, there is a problem that this
does not include dynamic text fields. I'm not sure if there is a way
to include all dynamic fields since I don't know the names of those
fields. I have filed this related bug.

http://outoftime.lighthouseapp.com/projects/20339-sunspot/tickets/56-dynamic_test-fields-not-search-for-keyword-search

Mat Brown

unread,
Nov 29, 2009, 12:24:29 PM11/29/09
to ruby-s...@googlegroups.com

That looks like it'll work, but keep in mind that going outside the public API exposes you to the risk that any new version of Sunspot could break your code.

As far as the dynamic text fields go, I'd never intended for that to be a feature - more just a metaprogramming coincidence that it's possible to define them - but I'll look into whether it's possible to fully support that and, if not, remove it from the public API.

Mat

On Nov 28, 3:55 pm, Randy <randy.si...@gmail.com> wrote: > I am using Sunspot 0.10.8 but I get an ex...

Reply all
Reply to author
Forward
0 new messages