-->
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.  [ 7 posts ] 
Author Message
 Post subject: [search] QueryBuilder "or" request
PostPosted: Thu Dec 30, 2010 12:18 pm 
Newbie

Joined: Thu Dec 30, 2010 12:10 pm
Posts: 9
Location: France
Hello,

I want to make a request like this using the QueryBuilder ( new in hibernate search ).
" state:hidden OR state:frozen OR state:published AND hidden:false "

I try something, but it's not working :
Code:
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
FullTextSession ftSession = Search.getFullTextSession(sessionFactory.getCurrentSession());
QueryBuilder mythQB = ftSession.getSearchFactory().buildQueryBuilder().forEntity( Product.class ).get();
String[] states = {"frozen","published"};
BooleanJunction<BooleanJunction> b = mythQB.bool();
for (String state : states) {
    b.should(
        mythQB.keyword().onField("state").matching(state).createQuery()
    );
}

b.must(
    mythQB.keyword().onField("hidden").matching(String.valueOf(hidden)).createQuery()
);



Also, is it possible to view the query passed to lucene using the query builder ?


Top
 Profile  
 
 Post subject: Re: [search] QueryBuilder "or" request
PostPosted: Thu Dec 30, 2010 12:56 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
this query
Quote:
state:hidden OR state:frozen OR state:published AND hidden:false

is really equivalent to the query:
Code:
state:hidden OR state:frozen OR  (state:published AND hidden:false)

Your programmatic query is equivalent to:
Code:
(state:hidden OR state:frozen) AND hidden:false

Could that be the problem?


Top
 Profile  
 
 Post subject: Re: [search] QueryBuilder "or" request
PostPosted: Thu Dec 30, 2010 1:17 pm 
Newbie

Joined: Thu Dec 30, 2010 12:10 pm
Posts: 9
Location: France
No my problem is not in the lucene query, but in the QueryBuilder.
If you prefer I need this request :
Code:
( state:hidden OR state:frozen OR state:published ) AND hidden:false

But using the Querybuilder, and b.should is not equivalent to OR.


Top
 Profile  
 
 Post subject: Re: [search] QueryBuilder "or" request
PostPosted: Sat Jan 01, 2011 9:26 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
But using the Querybuilder, and b.should is not equivalent to OR.

Why? Should "should behave like" OR
The QueryBuilder is using the "should" term which is more appropriate, also if you build the Query programmatically
via Lucene's org.apache.lucene.search.BooleanClause the OR operator is named "should" which is more appropriate than "OR".
Only the queryParses uses the "OR" name, mostly legacy and ease of use of non-technical query writers (like to parse user input queries).

What makes you think that they are not being equivalent?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: [search] QueryBuilder "or" request
PostPosted: Mon Jan 03, 2011 7:02 am 
Newbie

Joined: Thu Dec 30, 2010 12:10 pm
Posts: 9
Location: France
Ok, I didn't understand that.
You've right.

I'm using this for my request :

Code:
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
FullTextSession ftSession = Search.getFullTextSession(sessionFactory.getCurrentSession());
QueryBuilder mythQB = ftSession.getSearchFactory().buildQueryBuilder().forEntity( Product.class ).get();
String[] states = {"frozen","published"};
BooleanJunction<BooleanJunction> b = mythQB.bool();
BooleanQuery bQuery = new BooleanQuery();
for (String state : states) {
    bQuery.add( mythQB.keyword().onField("state").matching(state).createQuery(), Occur.SHOULD) ;
}

b.must(bQuery);

b.must(
    mythQB.keyword().onField("hidden").matching(String.valueOf(hidden)).createQuery()
);

FullTextQuery ftquery = ftSession .createFullTextQuery( b.createQuery(), Product.class );
List<LogicalItem> results = ftquery.setFirstResult( firstResult ) .setMaxResults( maxResults ).list();
int resultSize = ftquery.getResultSize();


Top
 Profile  
 
 Post subject: Re: [search] QueryBuilder "or" request
PostPosted: Mon Jan 03, 2011 7:18 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi elendrim,
is your problem solved? Works as expected?

Remembed when using the BooleanQuery like that you have to provide pre-Analyzed/Tokenized strings, so your solutions should work fine but is a bit verbose and tricky as you have to match the indexing analyzer. that's what the DSL is doing for you.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: [search] QueryBuilder "or" request
PostPosted: Tue Jan 04, 2011 10:42 am 
Newbie

Joined: Thu Dec 30, 2010 12:10 pm
Posts: 9
Location: France
Yes, my problem is solved,
Thank you ;)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.