You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@lucenenet.apache.org by Eric Svensson <sa...@gmail.com> on 2009/07/29 14:15:45 UTC

Search autocomplete/suggestion

I would like to implement automatic phrase suggestion as one type in the
searchfield. I have tried different Lucene.net quries without any luck.
The functionality I would like is simialr to the SQL Like % method.

For instance, if one types "g", "go", "goo", "goog"...  I would like "
google.com" in the search result, but I don't want "docs.google.com".
My main problem is that Lucene.net is searching by terms and I can't figure
out how to force it to exclude "docs.google.com" from the results.

RE: Search autocomplete/suggestion

Posted by Digy <di...@gmail.com>.
Terms are stored sorted in the index. So you cannot avoid this sort. You
cannot also use any other criteria to sort since they are not search
results, just  unique terms of your documents stored in the index.

DIGY.



-----Original Message-----
From: Eric Svensson [mailto:sarkgo@gmail.com] 
Sent: Thursday, July 30, 2009 2:23 PM
To: lucene-net-user@incubator.apache.org
Subject: Re: Search autocomplete/suggestion

 List<string> items = new List<string>();
Lucene.Net.Store.RAMDirectory ramDir = new Lucene.Net.Store.
RAMDirectory(LUCENE_INDEX_PATH);
Lucene.Net.Index.IndexReader reader = IndexReader.Open(ramDir);
Lucene.Net.Index.TermEnum tEnum = reader.Terms(new Lucene.Net.Index.Term(
"title", prefix));
int k = 0;
while (tEnum.Next())
{
    if (!tEnum.Term().Text().StartsWith(prefix))
        break;
    if(k++ >= count)
        break;
    items.Add(tEnum.Term().Text());
}

The above code give me correct results, but unfortunately, it sorts the
results by field.
Is it possible to avoid this sort or is it possible to order by an other
field?

On Wed, Jul 29, 2009 at 5:33 PM, Digy <di...@gmail.com> wrote:

> Assuming that you store the URLs untokenized you can use TermEnum like
> below:
>
>
>
> Lucene.Net.Index.IndexReader rdr =
> Lucene.Net.Index.IndexReader.Open(INDEX);
>
>
>
>            string field = "URL";
>
>            string prefix = "go";
>
>
>
>            Lucene.Net.Index.TermEnum tEnum =  rdr.Terms( new
> Lucene.Net.Index.Term(field,prefix) );
>
>            while(tEnum.Next())
>
>            {
>
>                if(! tEnum.Term().Text().StartsWith(prefix) ) break;
>
>                Console.WriteLine( tEnum.Term().Text() );
>
>            }
>
>
>
>
>
> DIGY
>
>
>
> -----Original Message-----
> From: Eric Svensson [mailto:sarkgo@gmail.com]
> Sent: Wednesday, July 29, 2009 3:16 PM
> To: lucene-net-user@incubator.apache.org
> Subject: Search autocomplete/suggestion
>
>
>
> I would like to implement automatic phrase suggestion as one type in the
>
> searchfield. I have tried different Lucene.net quries without any luck.
>
> The functionality I would like is simialr to the SQL Like % method.
>
>
>
> For instance, if one types "g", "go", "goo", "goog"...  I would like "
>
> google.com" in the search result, but I don't want "docs.google.com".
>
> My main problem is that Lucene.net is searching by terms and I can't
figure
>
> out how to force it to exclude "docs.google.com" from the results.
>
>


Re: Search autocomplete/suggestion

Posted by Eric Svensson <sa...@gmail.com>.
 List<string> items = new List<string>();
Lucene.Net.Store.RAMDirectory ramDir = new Lucene.Net.Store.
RAMDirectory(LUCENE_INDEX_PATH);
Lucene.Net.Index.IndexReader reader = IndexReader.Open(ramDir);
Lucene.Net.Index.TermEnum tEnum = reader.Terms(new Lucene.Net.Index.Term(
"title", prefix));
int k = 0;
while (tEnum.Next())
{
    if (!tEnum.Term().Text().StartsWith(prefix))
        break;
    if(k++ >= count)
        break;
    items.Add(tEnum.Term().Text());
}

The above code give me correct results, but unfortunately, it sorts the
results by field.
Is it possible to avoid this sort or is it possible to order by an other
field?

On Wed, Jul 29, 2009 at 5:33 PM, Digy <di...@gmail.com> wrote:

> Assuming that you store the URLs untokenized you can use TermEnum like
> below:
>
>
>
> Lucene.Net.Index.IndexReader rdr =
> Lucene.Net.Index.IndexReader.Open(INDEX);
>
>
>
>            string field = "URL";
>
>            string prefix = "go";
>
>
>
>            Lucene.Net.Index.TermEnum tEnum =  rdr.Terms( new
> Lucene.Net.Index.Term(field,prefix) );
>
>            while(tEnum.Next())
>
>            {
>
>                if(! tEnum.Term().Text().StartsWith(prefix) ) break;
>
>                Console.WriteLine( tEnum.Term().Text() );
>
>            }
>
>
>
>
>
> DIGY
>
>
>
> -----Original Message-----
> From: Eric Svensson [mailto:sarkgo@gmail.com]
> Sent: Wednesday, July 29, 2009 3:16 PM
> To: lucene-net-user@incubator.apache.org
> Subject: Search autocomplete/suggestion
>
>
>
> I would like to implement automatic phrase suggestion as one type in the
>
> searchfield. I have tried different Lucene.net quries without any luck.
>
> The functionality I would like is simialr to the SQL Like % method.
>
>
>
> For instance, if one types "g", "go", "goo", "goog"...  I would like "
>
> google.com" in the search result, but I don't want "docs.google.com".
>
> My main problem is that Lucene.net is searching by terms and I can't figure
>
> out how to force it to exclude "docs.google.com" from the results.
>
>

RE: Search autocomplete/suggestion

Posted by Digy <di...@gmail.com>.
Assuming that you store the URLs untokenized you can use TermEnum like
below:

 

Lucene.Net.Index.IndexReader rdr = Lucene.Net.Index.IndexReader.Open(INDEX);

 

            string field = "URL";

            string prefix = "go";

 

            Lucene.Net.Index.TermEnum tEnum =  rdr.Terms( new
Lucene.Net.Index.Term(field,prefix) );

            while(tEnum.Next())

            {

                if(! tEnum.Term().Text().StartsWith(prefix) ) break;

                Console.WriteLine( tEnum.Term().Text() );

            }

 

 

DIGY

 

-----Original Message-----
From: Eric Svensson [mailto:sarkgo@gmail.com] 
Sent: Wednesday, July 29, 2009 3:16 PM
To: lucene-net-user@incubator.apache.org
Subject: Search autocomplete/suggestion

 

I would like to implement automatic phrase suggestion as one type in the

searchfield. I have tried different Lucene.net quries without any luck.

The functionality I would like is simialr to the SQL Like % method.

 

For instance, if one types "g", "go", "goo", "goog"...  I would like "

google.com" in the search result, but I don't want "docs.google.com".

My main problem is that Lucene.net is searching by terms and I can't figure

out how to force it to exclude "docs.google.com" from the results.


Re: Search autocomplete/suggestion

Posted by Eric Svensson <sa...@gmail.com>.
2. Search your new index using a prefix query and everything will work as
you expected.

IndexSearcher searcher = new IndexSearcher(reader);
Term term = new Term("title", prefix);
PrefixQuery query = new PrefixQuery(term);
Hits hits = searcher.Search(query);

This PrefixQuery returns "my dog" when the prefix is "dog". How do I avoid
that?



On Wed, Jul 29, 2009 at 2:59 PM, Andrew C. Smith <po...@gmail.com>wrote:

> I would suggest two things to do in order to make this work like you are
> wanting to do.
>
> 1. Use the keyword analyzer to analyze the url field. It may make sense to
> look into using the PerFieldAnalyzerWrapper if you want to index your other
> fields differently. Or you can consider building a separate index for this,
> it's up to you. But the goal here is to use the KeywordAnalyzer.
>
> 2. Search your new index using a prefix query and everything will work as
> you expected.
>
> Hope this helps!
> ~Andrew
>
>
>
> On Wed, Jul 29, 2009 at 8:15 AM, Eric Svensson <sa...@gmail.com> wrote:
>
> > I would like to implement automatic phrase suggestion as one type in the
> > searchfield. I have tried different Lucene.net quries without any luck.
> > The functionality I would like is simialr to the SQL Like % method.
> >
> > For instance, if one types "g", "go", "goo", "goog"...  I would like "
> > google.com" in the search result, but I don't want "docs.google.com".
> > My main problem is that Lucene.net is searching by terms and I can't
> figure
> > out how to force it to exclude "docs.google.com" from the results.
> >
>

Re: Search autocomplete/suggestion

Posted by "Andrew C. Smith" <po...@gmail.com>.
I would suggest two things to do in order to make this work like you are
wanting to do.

1. Use the keyword analyzer to analyze the url field. It may make sense to
look into using the PerFieldAnalyzerWrapper if you want to index your other
fields differently. Or you can consider building a separate index for this,
it's up to you. But the goal here is to use the KeywordAnalyzer.

2. Search your new index using a prefix query and everything will work as
you expected.

Hope this helps!
~Andrew



On Wed, Jul 29, 2009 at 8:15 AM, Eric Svensson <sa...@gmail.com> wrote:

> I would like to implement automatic phrase suggestion as one type in the
> searchfield. I have tried different Lucene.net quries without any luck.
> The functionality I would like is simialr to the SQL Like % method.
>
> For instance, if one types "g", "go", "goo", "goog"...  I would like "
> google.com" in the search result, but I don't want "docs.google.com".
> My main problem is that Lucene.net is searching by terms and I can't figure
> out how to force it to exclude "docs.google.com" from the results.
>