-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Free Text searching using Hibernate
PostPosted: Mon Dec 08, 2003 8:19 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 8:09 am
Posts: 25
Hi there,
I'm a new Hibernate user and I'm trying to find a way to perform free text searching using Hibernate. I'm using mySQL as my underlying database, which has excellent free text search features- unfortunately after reading the Hibernate documentation I have been able to find anything that would allow me to utilize this feature with HQL.

Its possible that I missed something. I was looking for something along the lines of Oracle's CONTAINS keyword, or mySQL's MATCH(foo) AGAINST(bar) syntax, but didnt find anything. Heres a link to the relevant section of the mySQL manual if you're not too sure what I'm on about:

http://www.mysql.com/doc/en/Fulltext_Search.html

Is there a way of performing this kind of search through Hibernate?

Thanks for your time

Jon


Top
 Profile  
 
 Post subject: Use Lucene
PostPosted: Mon Dec 08, 2003 8:43 am 
Beginner
Beginner

Joined: Wed Dec 03, 2003 10:59 am
Posts: 47
Use Lucene: http://jakarta.apache.org/lucene/


Top
 Profile  
 
 Post subject: Hibernate and Lucine
PostPosted: Mon Dec 08, 2003 9:10 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 8:09 am
Posts: 25
Thank you for the link otis.

I am actually tied to using Hibernate and after reading the Lucine FAQ, it appears that its a stand-alone application rather than something I could plug into Hibernate- is this correct?

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 9:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
you can use createSQLQuery()


Top
 Profile  
 
 Post subject: HQL combined with SQL
PostPosted: Mon Dec 08, 2003 10:28 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 8:09 am
Posts: 25
Thanks.

OK. So, after reading up on the createSQLQuery() Session class method I now know I can do my Free Text Search using native SQL just by passing the native SQL query as an argument.

I still have a small problem though. Some of the queries I'm going to be writing will have to use a mixture of SQL for the free text search and HQL at the same time (for additional constraints on the results).

Is there any way of using SQL and HQL together? Or could I set additional constraints on the Query object once I've got it back from the createSQLQuery() method?

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 10:39 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can use arbitrary SQL functions in the WHERE clause of an HQL query.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Cheers
PostPosted: Mon Dec 08, 2003 10:46 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 8:09 am
Posts: 25
Thank you I'll give that a shot.

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 08, 2003 11:16 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
If you really want advanced free text searching (wo killing you DB) I strongly recommend you Lucene as a standalone product.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: mySQL specific SQL and HQL in the same query?
PostPosted: Wed Dec 10, 2003 10:26 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 8:09 am
Posts: 25
Hi,
OK. Going on what has been said in this thread so far I am under the impression that the following HQL/SQL query would work...

from Group
where match(name, description) against ('Some Text')

To clarify: Group is an class which maps onto a table which has text columns called "name" and "description". The "match" and "against" keywords are mySQL specific (In Oracle you would use "contains"). So basically everything after the word "where" is specific to mySQL.

Does Hibernate allow Database-specific SQL after the where clause in a query?

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 10:29 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Just try it.

Reference Documentation, 10.8:

"Scalar SQL functions supported by the underlying database may be used."

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 10:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Of course, it would need to be:


Code:
from Group g
where match(g.name, g.description) against ('Some Text')


Top
 Profile  
 
 Post subject: Didnt work
PostPosted: Wed Dec 10, 2003 11:51 am 
Beginner
Beginner

Joined: Mon Dec 08, 2003 8:09 am
Posts: 25
It didnt work. Running the following query:

from Group g
where match(g.name, g.description) against('foreign')

Caused an exception to be thrown. The error message was of the form:

Caused by: net.sf.hibernate.QueryException: Incorrect query syntax [from Group g where match(g.name, g.description) against('foreign')]

I read something in the manual about table aliases being inside braces {}, but didnt really understand it. Could this be something to do with it?

Logging into the test database and running the following SQL query:

select * from group where match(name, description) against('foreign');

Produced the expected results.

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 11:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
OK, so it perhaps doesn't work. Looks like the query parser doesn't like two "function calls" right beside each other.

So, do like I said the first time, and use createSQLQuery().


Top
 Profile  
 
 Post subject: Development
PostPosted: Wed Dec 10, 2003 12:48 pm 
Beginner
Beginner

Joined: Mon Dec 08, 2003 8:09 am
Posts: 25
OK. I'll need to talk to my boss about this, he's not about at the moment.

I think the likelihood is that I will be asked to alter Hibernate to support these Text Searches without using SQL at all.

I can see it coming.

I've had a breif look at your CVS repository, but are there any classes you recommend I pay special attension to in light of this possibility.

Thank you for your help so far.

Jon


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 10, 2003 2:37 pm 
Beginner
Beginner

Joined: Tue Sep 30, 2003 10:09 am
Posts: 34
Location: London, UK
gavin wrote:
OK, so it perhaps doesn't work. Looks like the query parser doesn't like two "function calls" right beside each other.

So, do like I said the first time, and use createSQLQuery().


We're going to want to be able to do searches that combine freetext searches with constraints on other columns that are already mapped in hbm.xml files. Using createSQLQuery() would mean having to wire in table column names, creating what would be unnecessary duplication if we could mix and match the HQL and SQL we need. We'd have to do this for all our tables that have fulltext indexes.

(Additionally, though I think it's unlikely, if we wanted to search over multiple types of Domain Object then even those objects that don't have fulltext indexes would have to be searched with a regular SQL query so they can be joined with those that have and have the search results paged back by Hibernate.)

Wouldn't it be better to modify the query parser to enable it to deal with juxtaposed function calls?

Presumably you're saying that in the query:

Code:
from Group g
where match(g.name, g.description) against ('Some Text')


that
Code:
match
and
Code:
against
are parsed as separate function calls?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.