-->
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.  [ 2 posts ] 
Author Message
 Post subject: BooleanQuery should criteria not working as expexted
PostPosted: Mon Jul 18, 2011 4:24 pm 
Newbie

Joined: Mon Jul 11, 2011 5:16 pm
Posts: 12
I am currently trying to execute a search for products(software) in a catalog.

I have hibernate search working for product name and manufacturer name however I am having issues
making the search work for a platform criteria(Windows, Mac, Mobile, etc).

The object of platform is to only return results that will run on the platforms selected. The issue is that multiple platforms can be selected but each software product can only have one platform so using the MUST command when more than one platform is selected results in nothing being returned.

I have implemented the query like this:
Code:
  BooleanQuery query = new BooleanQuery();

        if (StringUtils.isNotBlank(productName)) {
            productName = productName.toLowerCase();
            Term productNameTerm = new Term("productName", productName);
            boolClause = true;
            BooleanClause clause = new BooleanClause(new TermQuery(productNameTerm), BooleanClause.Occur.SHOULD);
            query.add(clause);
        }

        if (StringUtils.isNotBlank(companyName)) {
            companyName = companyName.toLowerCase();
            Term companyNameTerm = new Term("manufacturer.companyName", companyName);
            if (!boolClause) {
                BooleanClause clause = new BooleanClause(new TermQuery(companyNameTerm), BooleanClause.Occur.SHOULD);
                boolClause = true;
                query.add(clause);
            } else {
                query.add(new TermQuery(companyNameTerm), BooleanClause.Occur.SHOULD);
            }
        }

        if (platforms != null) {
            Term platformTerm;
            for (int i = 0; i < platforms.length; i++) {
                platforms[i] = platforms[i].toLowerCase();
                platformTerm = new Term("currentPlatforms.id", platforms[i]);
                if (!boolClause) {
                    BooleanClause clause = new BooleanClause(new TermQuery(platformTerm), BooleanClause.Occur.SHOULD);
                    boolClause = true;
                    query.add(clause);
                } else {
                    query.add(new TermQuery(platformTerm), BooleanClause.Occur.SHOULD);
                }
            }
        }

        return query;


What seems to happen is the results are prioritized if the platform matches but it will still return all results.

The effect I am wanting is productName AND companyName AND (platform 1 OR platform 2 OR ...)

Any ideas on how I can make this happen? Also I want to note that because I am trying to implement this in an existing system I do not have the current version of Hibernate or Hibernate search available. My supervisor is attempting to upgrade us to 3.3.0 right now but for now I am using 3.1.1.

Thanks for any help you can provide!


Top
 Profile  
 
 Post subject: Re: BooleanQuery should criteria not working as expexted
PostPosted: Mon Jul 18, 2011 6:52 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
I see two issues:
1) You're using BooleanClause.Occur.SHOULD instead of MUST
2) look out in how you combine the boolean clauses: doing "b.should, should, should, must" you're getting something like ( OR OR OR OR MUST) and so the "must" operator is applied only to the last clause.
You actually want a ( (product names on fields using SHOULD) MUST (platforms boolean clause using MUST) ) : you have to nest different BooleanQueries.

A simpler alternatives is to apply filters, which is quite likely to perform better as well.

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


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