You are viewing a plain text version of this content. The canonical link for it is here.
Posted to solr-user@lucene.apache.org by Paul <pa...@nines.org> on 2010/08/17 17:00:23 UTC
autocomplete: case-insensitive and middle word
I have a couple questions about implementing an autocomplete function
in solr. Here's my scenario:
I have a name field that usually contains two or three names. For
instance, let's suppose it contains:
John Alfred Smith
Alfred Johnson
John Quincy Adams
Fred Jones
I'd like to have the autocomplete be case insensitive and match any of
the names, preferably just at the beginning.
In other words, if the user types "alf", I want
John Alfred Smith
Alfred Johnson
if the user types "fre", I want
Fred Jones
but not:
John Alfred Smith
Alfred Johnson
I can get the matches using the text_lu analyzer, but the hints that
are returned are lower case, and only one name.
If I use the "string" analyzer, I get the entire name like I want it,
but the user must match the case, that is, must type "Alf", and it
only matches the first name, not the middle name.
How can I get the matches of the "text_lu" analyzer, but get the hints
like the "string" analyzer?
Thanks,
Paul
Re: autocomplete: case-insensitive and middle word
Posted by Paul <pa...@nines.org>.
Here's my solution. I'm posting it in case it is radically wrong; I
hope someone can help straighten me out. It seems to work fine, and
seems fast enough.
In schema.xml:
<fieldType name="autocomplete" class="solr.TextField">
<analyzer type="index">
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory" />
<filter class="solr.PatternReplaceFilterFactory"
pattern="([^a-z0-9])" replacement="" replace="all" />
<filter class="solr.EdgeNGramFilterFactory"
maxGramSize="100" minGramSize="1" />
</analyzer>
<analyzer type="query">
<tokenizer class="solr.KeywordTokenizerFactory"/>
<filter class="solr.LowerCaseFilterFactory" />
<filter class="solr.PatternReplaceFilterFactory"
pattern="([^a-z0-9])" replacement="" replace="all" />
<filter class="solr.PatternReplaceFilterFactory"
pattern="^(.{20})(.*)?" replacement="$1" replace="all" />
</analyzer>
</fieldType>
<field name="ac_name" type="autocomplete" indexed="true" stored="true"
multiValued="true"/>
Then I restarted solr to pick up the changes.
I then ran a script which reads each document out of the current
index, and adds the new field:
for each doc in my solr index:
doc['ac_name'] = doc['name'].split(' ')
and write the record back out.
Then, using rsolr, I make the following query:
response = @solr.select(:params => {
:q=> "ac_name:#{prefix}",
:start=> 0,
:rows=> 500,
:fl => "name"
})
matches = []
docs = response['response']['docs']
docs.each {|doc|
matches.push(doc['name'])
}
"matches" is now an array of the values I want to display.
Re: autocomplete: case-insensitive and middle word
Posted by Avlesh Singh <av...@gmail.com>.
This thread might help -
http://www.lucidimagination.com/search/document/9edc01a90a195336/enhancing_auto_complete
Cheers
Avlesh
@avlesh <http://twitter.com/avlesh> | http://webklipper.com
On Tue, Aug 17, 2010 at 8:30 PM, Paul <pa...@nines.org> wrote:
> I have a couple questions about implementing an autocomplete function
> in solr. Here's my scenario:
>
> I have a name field that usually contains two or three names. For
> instance, let's suppose it contains:
>
> John Alfred Smith
> Alfred Johnson
> John Quincy Adams
> Fred Jones
>
> I'd like to have the autocomplete be case insensitive and match any of
> the names, preferably just at the beginning.
>
> In other words, if the user types "alf", I want
>
> John Alfred Smith
> Alfred Johnson
>
> if the user types "fre", I want
>
> Fred Jones
>
> but not:
> John Alfred Smith
> Alfred Johnson
>
> I can get the matches using the text_lu analyzer, but the hints that
> are returned are lower case, and only one name.
>
> If I use the "string" analyzer, I get the entire name like I want it,
> but the user must match the case, that is, must type "Alf", and it
> only matches the first name, not the middle name.
>
> How can I get the matches of the "text_lu" analyzer, but get the hints
> like the "string" analyzer?
>
> Thanks,
> Paul
>