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!