-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Make all searches "AND" by default using QueryParser
PostPosted: Wed Nov 30, 2011 5:42 am 
Regular
Regular

Joined: Tue May 17, 2011 1:45 am
Posts: 52
Hi,

I am using QueryParser for obtaining my query as per the following syntax

org.apache.lucene.search.Query query=new QueryParser(Version.LUCENE_31,field, analyzer).parse(keyword);

However the issue is this that users can enter any kind of values for searching

1) New York AND Park Avenue
2) "New York"
3) Times square

While (1) and (2) above is handled well by QueryParser, (3) is an issue because Query parser is trying to do an OR clause with the words. I would like this to default to as AND i.e Times and Square should occur in all the results found. How do I do this?

Thanks
David


Top
 Profile  
 
 Post subject: Re: Make all searches "AND" by default using QueryParser
PostPosted: Wed Nov 30, 2011 6:24 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

use QueryParser#setDefaultOperator and make sure you understand all the implications. What behavior do you for example for "New York Park Avenue"?

--Hardy


Top
 Profile  
 
 Post subject: Re: Make all searches "AND" by default using QueryParser
PostPosted: Mon Dec 12, 2011 4:06 am 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
Using BooleanQuery is another option.

In this example, i apply my analyzer to a query string and then i create an and query.

Code:
BooleanQuery andQuery = new BooleanQuery();

                TokenStream tokenStream = analyzer.tokenStream(campos[cont], new StringReader(q));
               
                CharTermAttribute charTermAttribute = tokenStream.getAttribute(CharTermAttribute.class);
   
                while (tokenStream.incrementToken())
                {
                    String term = charTermAttribute.toString();
                    andQuery.add(new TermQuery(new Term(campos[cont], term)), Occur.MUST);
                }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.