-->
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.  [ 8 posts ] 
Author Message
 Post subject: Hibernate Search: Query Works in Luke But Not in Code
PostPosted: Wed Feb 11, 2009 8:28 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
I am running some TestNG tests to make sure my JPA search works correctly. As a precondition I manually generate indexes in this way:

Code:
fullTextEntityManager = org.hibernate.search.jpa.Search.createFullTextEntityManager(em);
List searchables = em.createQuery("from Customer as customer").getResultList();

for (Object obj : searchables) {
         fullTextEntityManager.index(obj);
}


And the indexes are generated in the right place defined in persistence.xml.

Then when I run my tests I find at this code:

Code:
fullTextQuery = em.createFullTextQuery(query, Customer.class);
totalResultCount = fullTextQuery.getResultSize();


that the totalResultCount = 0 even when I put in a literal term (no fuzzy or anything fancy) that corresponds identically with what's in the database like name:John. I generate the query using the Lucene API.

That by itself could be anything, but what is weird is that the same query, which I obtain from my logs, does in fact generate results when I run it using Luke against the same indexes with the same analyzer (StandardAnalyzer).

I could supply code, but I thought I would ask first if there are any obvious things to check in cases like this.

This has frustrated me for a day now, so any insight is appreicated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 11:19 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
did you commit the indexing fulltextEntityManager before creating the query?
The changes are applied only at commit time.

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 1:54 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
I thought of the same thing actually after consulting Hibernate Search in Action, so I was happy we had the same idea on this. Here is my new index generation code:

Code:
      emf = Persistence.createEntityManagerFactory("persistenceTest");
      em = emf.createEntityManager();
      fullTextEntityManager = org.hibernate.search.jpa.Search.createFullTextEntityManager(em);
      fullTextEntityManager.getTransaction().begin();
      List searchables = em.createQuery("from Customer as customer").getResultList();
      logger.debug("Number of searchables: " + searchables.size());

      for (Object obj : searchables) {
         fullTextEntityManager.index(obj);
      }

      fullTextEntityManager.getTransaction().commit();


Unfortunately, I am still having the same issue. The queries I generate work in Luke but generate no results in Hibernate Search.

Any other ideas are greatly appreciated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 5:29 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
Well, I can't say I have figured out the issue, but I have discovered some interesting clues.

As I mentioned, I generate a Lucene query using the programmatic API. It is the result of this query that I print out in my logs and execute successfully against Luke--though the query fails in code.

Well, when I take that query I generate and pass its string equivalent to the MultiFieldQueryParser, then finally the code works too. I am not sure what this tells me though. It strikes me as very weird that the identical query needs to be run through MultiFieldQueryParser to work.

Can anyone explain why a query won't work in code but will work when run through the MultiFieldQueryParser? Put another way, what does MultiFieldQueryParser do that I am not doing?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 12, 2009 6:08 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
It turns out the search fails because all the letters were capitalized. The MultiFieldQueryParser takes care of that, which is why things worked.

I thought that the StandardAnalyzer normalized for case among search terms. Do I need to run my terms through a filter as well?

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 14, 2009 7:56 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Can anyone explain why a query won't work in code but will work when run through the MultiFieldQueryParser?

How are you running it "in code" ? Are you sure the query terms get Analyzed?
Please show the Query code if possible.

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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 14, 2009 3:45 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
That must be it. When I say "in code," I mean nothing more than something like this:

Code:
Query query = new TermQuery(new Term("name", "JOHN"));


and passing that to the FullTextEntityManager. My queries are of course much more complicated than that, but that complexity is immaterial. The point is that I take precisely the search term as provided and use it to generate a query.

I figured the Query itself would analyze the term provided, but I suppose I would need to do that manually. Or just do what Luke does and take whatever I generate and run it through the MultiFieldQueryParser.

Any tips or best practices are appreciated.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 14, 2009 7:36 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
yes that's it: you are not supposed to build your Queries without using an Analyzer. Generally a good approach is to use Lucene's QueryParser, or build your custom one by extending it, configuring it or from scratch.
You may notice that the TermQuery is never mentioned in the reference documentation, and in the Book it is only used in advanced examples (like filtering by a constant untokenized value).

As you have seen, Luke always parses the user input: you should do the same with your user imput. Also when using Luke you are asked for which Analyzer to use during parsing: you should provide the same to the QueryParser, or use it to preprocess the terms.

_________________
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.  [ 8 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.