You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@lucene.apache.org by James Huang <me...@yahoo.com> on 2005/09/15 18:12:43 UTC

Question: force a field must be matched?

Suppose I have a book index with field="publisher", field="title", etc.
I want to search for books only from "Manning", do I have to do anything special? how?
 
Thanks,
-James

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

Re: Question: force a field must be matched?

Posted by Erik Hatcher <er...@ehatchersolutions.com>.
On Sep 15, 2005, at 12:55 PM, James Huang wrote:
> Thanks Jason.
>
> I wonder if that's the same as
>
>   queryString + " publisher:Manning"
>
> and pass on to the query parser?

I will emphasize the other comments made on this regarding the  
Analyzer.  I recommend against programatically adding to the string  
passed to QueryParser because of these types of issues.  You can  
aggregate a parsed expression Query into a BooleanQuery with other  
programmatically created Query objects (such as TermQuery in this case).

     Erik



>
> -James
>
> --- Jason Haruska <ja...@haruska.com> wrote:
>
>
>> On 9/15/05, James Huang <me...@yahoo.com> wrote:
>>
>>>
>>> Suppose I have a book index with
>>>
>> field="publisher", field="title", etc.
>>
>>> I want to search for books only from "Manning", do
>>>
>> I have to do anything
>>
>>> special? how?
>>>
>>>
>>
>> add new BooleanClause(new TermQuery(new
>> Term("publisher","Manning")), true,
>> false) to your BooleanQuery
>>
>>
>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail: java-user-help@lucene.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question: force a field must be matched?

Posted by Miles Barr <mi...@runtime-collective.com>.
On Thu, 2005-09-15 at 11:56 -0700, James Huang wrote:
> Yes, "+" is what I missed! Thanks.
> 
> Suppose there is a book published by 3 publishers (I
> don't know how that works in real world):
> 
> // At index time:
>   doc.add( Field.Keyword("publisher", "Manning") );
>   doc.add( Field.Keyword("publisher", "SAMS") );
>   doc.add( Field.Keyword("publisher", "O'Reilly") );
> 
> // At search time:
>   queryString += " +publisher:SAMS";
>   ...
> 
> should find me that Document.

That may or may not work depending on your analyzer. 

If you're using the query parser with the standard analyzer it will
search the 'publisher' field for 'sams' not 'SAMS', and hence get no
matches back.

If you want to use the query parser instead of building the query by
hand you can use the PerFieldAnalyzerWrapper class and write a
KeywordAnalyzer, i.e.:

package org.apache.lucene.analysis;

import java.io.IOException;
import java.io.Reader;

/** "Tokenizes" the entire stream as a single token. */
public class KeywordAnalyzer extends Analyzer {
    public TokenStream tokenStream(String fieldName, final Reader reader) {
        
        return new TokenStream() {
                private boolean done;
                private final char[] buffer = new char[1024];
                
                public Token next() throws IOException {
                    if (!done) {
                        done = true;
                        StringBuffer sb = new StringBuffer();
                        int length;
                        while (true) {
                            length = reader.read(this.buffer);
                            if (length == -1) break;
                            
                            sb.append(this.buffer, 0, length);
                        }
                        String text = sb.toString();
                        return new Token(text, 0, text.length());
                    }
                    return null;
                }
            };
    }
}

--------------------

PerFieldAnalyzerWrapper result =
    new PerFieldAnalyzerWrapper(new StandardAnalyzer());

result.addAnalyzer("publisher", new KeywordAnalyzer());

QueryParser parser = new QueryParser(<your regular field>, result);




-- 
Miles Barr <mi...@runtime-collective.com>
Runtime Collective Ltd.


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question: force a field must be matched?

Posted by James Huang <me...@yahoo.com>.
Yes, "+" is what I missed! Thanks.

Suppose there is a book published by 3 publishers (I
don't know how that works in real world):

// At index time:
  doc.add( Field.Keyword("publisher", "Manning") );
  doc.add( Field.Keyword("publisher", "SAMS") );
  doc.add( Field.Keyword("publisher", "O'Reilly") );

// At search time:
  queryString += " +publisher:SAMS";
  ...

should find me that Document.


--- Chris Hostetter <ho...@fucit.org> wrote:

> : I wonder if that's the same as
> :
> :   queryString + " publisher:Manning"
> :
> : and pass on to the query parser?
> 
> assuming queryString is a java variable containing
> your initial query,
> then you are close, but not quite.  If you want to
> tell QueryParser to
> make a clause "required" then you have to prefice it
> with a "+".
> 
> something like...
> 
>   String userQueryString = ...;
>   String queryString = userQueryString + "
> +publisher:Manning";
> 
> ...is probably what you want.
> 
> Alternately, instead of giving QueryParser a string
> you have built up
> from little pieces, I would recommend that you let
> QueryParser build a
> Query just off of the user input, and then either:
> 
>   a) Filter that query at search time (take a look
> at the Filter class and
>      it's subclasses)
>   b) Modify the Query object returned by the
> QueryParser -- either adding
>      a mandatory clause to it if it's a
> BooleanQuery, or wrapping it in a
>      new BooleanQuery that already contains your
> required clause.
> 
> 
> -Hoss
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail:
> java-user-help@lucene.apache.org
> 
> 



		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question: force a field must be matched?

Posted by Chris Hostetter <ho...@fucit.org>.
: I wonder if that's the same as
:
:   queryString + " publisher:Manning"
:
: and pass on to the query parser?

assuming queryString is a java variable containing your initial query,
then you are close, but not quite.  If you want to tell QueryParser to
make a clause "required" then you have to prefice it with a "+".

something like...

  String userQueryString = ...;
  String queryString = userQueryString + " +publisher:Manning";

...is probably what you want.

Alternately, instead of giving QueryParser a string you have built up
from little pieces, I would recommend that you let QueryParser build a
Query just off of the user input, and then either:

  a) Filter that query at search time (take a look at the Filter class and
     it's subclasses)
  b) Modify the Query object returned by the QueryParser -- either adding
     a mandatory clause to it if it's a BooleanQuery, or wrapping it in a
     new BooleanQuery that already contains your required clause.


-Hoss


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question: force a field must be matched?

Posted by James Huang <me...@yahoo.com>.
Thanks Jason.

I wonder if that's the same as

  queryString + " publisher:Manning"

and pass on to the query parser?

-James

--- Jason Haruska <ja...@haruska.com> wrote:

> On 9/15/05, James Huang <me...@yahoo.com> wrote:
> > 
> > Suppose I have a book index with
> field="publisher", field="title", etc.
> > I want to search for books only from "Manning", do
> I have to do anything 
> > special? how?
> > 
> 
> add new BooleanClause(new TermQuery(new
> Term("publisher","Manning")), true, 
> false) to your BooleanQuery
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question: force a field must be matched?

Posted by Jason Haruska <ja...@haruska.com>.
On 9/15/05, James Huang <me...@yahoo.com> wrote:
> 
> Suppose I have a book index with field="publisher", field="title", etc.
> I want to search for books only from "Manning", do I have to do anything 
> special? how?
> 

add new BooleanClause(new TermQuery(new Term("publisher","Manning")), true, 
false) to your BooleanQuery

Re: Question: dynamic sorting

Posted by James Huang <me...@yahoo.com>.
Hi Otis,

Thanks for your answer. I do have LIA (but not with me
now physically), and have the impression that the
search ordering is predetermined (at index time); what
I want is search-time ordering, e.g.,

"I'm at (x,y) now and low on gas; find me the closest
airports that can land 747, the closest first,
please".

I'll re-read the book/chapter tonight, but look
forward to any expert advises.

Thanks,
-James

--- Otis Gospodnetic <ot...@yahoo.com>
wrote:

> Hi James,
> 
> Check out the org.apache.lucene.search.package,
> there are several sort
> classes that will let you write  a custom sorter. 
> If you have a copy
> of LIA, look at chapter 6 for an example (
>
http://www.lucenebook.com/search?query=custom+sort+section%3A6*
> )
> 
> Otis
> 
> --- James Huang <me...@yahoo.com> wrote:
> 
> > Suppose I have a book index with
> field="publisher", field="title",
> > etc.
> > If a user has bought Manning books, then I like to
> sort the result
> > with Manning books listed first.
> >  
> > In essence, I'm asking for a parameterized custom
> sorting. Is there a
> > way to do this?
> >  
> > Thanks,
> > -James
> > 
> > 
> > 		
> > ---------------------------------
> > Yahoo! for Good
> >  Click here to donate to the Hurricane Katrina
> relief effort. 
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> java-user-unsubscribe@lucene.apache.org
> For additional commands, e-mail:
> java-user-help@lucene.apache.org
> 
> 



		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Re: Question: dynamic sorting

Posted by Otis Gospodnetic <ot...@yahoo.com>.
Hi James,

Check out the org.apache.lucene.search.package, there are several sort
classes that will let you write  a custom sorter.  If you have a copy
of LIA, look at chapter 6 for an example (
http://www.lucenebook.com/search?query=custom+sort+section%3A6* )

Otis

--- James Huang <me...@yahoo.com> wrote:

> Suppose I have a book index with field="publisher", field="title",
> etc.
> If a user has bought Manning books, then I like to sort the result
> with Manning books listed first.
>  
> In essence, I'm asking for a parameterized custom sorting. Is there a
> way to do this?
>  
> Thanks,
> -James
> 
> 
> 		
> ---------------------------------
> Yahoo! for Good
>  Click here to donate to the Hurricane Katrina relief effort. 


---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscribe@lucene.apache.org
For additional commands, e-mail: java-user-help@lucene.apache.org


Question: dynamic sorting

Posted by James Huang <me...@yahoo.com>.
Suppose I have a book index with field="publisher", field="title", etc.
If a user has bought Manning books, then I like to sort the result with Manning books listed first.
 
In essence, I'm asking for a parameterized custom sorting. Is there a way to do this?
 
Thanks,
-James


		
---------------------------------
Yahoo! for Good
 Click here to donate to the Hurricane Katrina relief effort.