-->
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.  [ 6 posts ] 
Author Message
 Post subject: Results returned using QueryParser but not Query API
PostPosted: Fri Mar 27, 2009 10:35 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
I am building a lucene query using the lucene query api. When certain criteria is triggered, no results are returned. When I debug to figure out what's going on I can see the text representation of the query that is being generated. If I take that query and run it through luke, results are returned. If I take that query and run it through a query parser and then through hibernate search I get results. If the text version of the query generated works, why does it not work when using the query API? How can I determine what is causing no results to be returned?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 28, 2009 7:40 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi, could you show the API query and the QueryParser code?
You probably have forgotten to apply the Analyzer to the values used inside the query construction, the QueryParses is applying Analyzers automatically, whereas the TermQuery and others are not doing it for you, the the values need to be for example lowercased.

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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 30, 2009 10:42 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
Below is the code I'm using to build the query. The query works with only town criteria. As soon as Style criteria is added, the search returns no results (see the comment "styles" below).

Code:
FullTextSession fullTextSession = Factory.getFullTextSession();
      BooleanQuery query = new BooleanQuery();
      
      // bathrooms
      query.add(new RangeQuery(new Term("bathrooms", Utility.zeroPad(searchCriteria.getBathrooms(), 2)), new Term("bathrooms", "999"), true), Occur.MUST);
      
      // bedrooms
      query.add(new RangeQuery(new Term("bedrooms", Utility.zeroPad(searchCriteria.getBedrooms(), 2)), new Term("bedrooms", "999"), true), Occur.MUST);      
      
      // price
      if (searchCriteria.getMinPrice() > 0 || searchCriteria.getMaxPrice() > 0)
         query.add(new RangeQuery(new Term("listPrice", Utility.zeroPad(searchCriteria.getMinPrice(), 9)), new Term("listPrice", Utility.zeroPad(searchCriteria.getMaxPrice() > 0 ? searchCriteria.getMaxPrice() : 999999999, 9)), true), Occur.MUST);

      
      // map search
      if (searchCriteria.getArea() != null) {
         LatLngBridge converter = new LatLngBridge();
         BooleanQuery subQuery = new BooleanQuery();
         
         subQuery.add(new RangeQuery(new Term("latitude", converter.objectToString(searchCriteria.getArea().getSouthWest().getLatitude())), new Term("latitude", converter.objectToString(searchCriteria.getArea().getNorthEast().getLatitude())), true), Occur.MUST);
         subQuery.add(new RangeQuery(new Term("longitude", converter.objectToString(searchCriteria.getArea().getSouthWest().getLongitude())), new Term("longitude", converter.objectToString(searchCriteria.getArea().getNorthEast().getLongitude())), true), Occur.MUST);
         
         query.add(subQuery, Occur.MUST);

      // towns and regions
      } else if (searchCriteria.getTowns().size() > 0) {
         BooleanQuery subQuery = new BooleanQuery();
         
         for (Town town : searchCriteria.getTowns()) {
            BooleanQuery townQuery = new BooleanQuery();
            
            townQuery.add(new TermQuery(new Term("town.name", town.getName().toLowerCase())), Occur.MUST);
            townQuery.add(new TermQuery(new Term("town.state", town.getState().toLowerCase())), Occur.MUST);
            
            subQuery.add(townQuery, Occur.SHOULD);
         }
         
         query.add(subQuery, Occur.MUST);
      }
      
      // zip code
      if (!Utility.isEmpty(searchCriteria.getZipCode()))
         query.add(new TermQuery(new Term("zipCode", searchCriteria.getZipCode())), Occur.MUST);
      
      // styles
      if (searchCriteria.getStyles().size() > 0) {
         BooleanQuery subQuery = new BooleanQuery();
         
         for (Style style : searchCriteria.getStyles()) {
            subQuery.add(new TermQuery(new Term("style.name", "\"" + style.getName().toLowerCase() + "\"")), Occur.SHOULD);
         }
         
         query.add(subQuery, Occur.MUST);
      }
      
      // type
      if (searchCriteria.getTypes().size() > 0) {
         BooleanQuery subQuery = new BooleanQuery();
         
         for (ListingType type : searchCriteria.getTypes()) {
            subQuery.add(new TermQuery(new Term("type", type.name().toLowerCase())), Occur.SHOULD);            
         }
         
         query.add(subQuery, Occur.MUST);
      }
      
      // garage
      if (searchCriteria.getHasGarage())
         query.add(new TermQuery(new Term("hasGarage", "true")), Occur.MUST);
      
      // basement
      if (searchCriteria.getHasBasement())
         query.add(new TermQuery(new Term("hasBasement", "true")), Occur.MUST);
      
      // description
      if (!Utility.isEmpty(searchCriteria.getDescription())) {
         try {
            query.add(MultiFieldQueryParser.parse(new String [] { searchCriteria.getDescription(), searchCriteria.getDescription() }, new String [] { "description", "features.values.value" }, new StandardAnalyzer()), Occur.MUST);
         } catch (ParseException ex) {
            ApplicationLog.warning("Error parsing description: " + searchCriteria.getDescription());
         }
      }
      
      // features
      if (searchCriteria.getFeatures().size() > 0) {
         BooleanQuery subQuery = new BooleanQuery();
         
         for (FeatureValue feature : searchCriteria.getFeatures()) {
            subQuery.add(new TermQuery(new Term("featureValueId", Integer.toString(feature.getId()))), Occur.MUST);
         }
         
         query.add(subQuery, Occur.MUST);
      }
      
      // status
      query.add(new TermQuery(new Term("status", ListingStatus.Withdrawn.name())), Occur.MUST_NOT);
      
      FullTextQuery searchQuery = fullTextSession.createFullTextQuery(query, Listing.class);
      // set up the transformer
      searchQuery.setResultTransformer(new ListingTransformer());
      // set projection, this will allow all listing info on search pages to be pulled from lucene
      // this means no database hit is necessary!
      searchQuery.setProjection("basement", "garage", "latitude", "longitude", "mlsNumber", "town.state", "town.name", "streetName", "streetNumber", "style.name", "type", "yearBuilt", "features.values.featureValueID", "description", "photoCount", "listPrice", "bedrooms", "bathrooms");
      
      return searchQuery;


Top
 Profile  
 
 Post subject:
PostPosted: Sat Apr 04, 2009 10:19 pm 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
Any ideas? Should I be using an analyzer in there anywhere? I couldn't find any method calls that accept analyzers. Also, the strange thing is if I debug, grab the query as a string and run it through Luke it returns the results I expect.


Top
 Profile  
 
 Post subject: Re: Results returned using QueryParser but not Query API
PostPosted: Tue Jun 02, 2009 9:35 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
bump...


Top
 Profile  
 
 Post subject: Re: Results returned using QueryParser but not Query API
PostPosted: Tue Jun 02, 2009 9:50 am 
Beginner
Beginner

Joined: Wed Dec 17, 2008 12:10 pm
Posts: 47
For the record...I did a toString() on my query object, then ran it through the query parser and everything is good. This seems like a very roundabout way of doing things though...


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