-->
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.  [ 5 posts ] 
Author Message
 Post subject: MySQL's fulltext search doesn't work with Hibernate 3
PostPosted: Fri Jun 24, 2005 4:48 am 
Newbie

Joined: Fri Jun 24, 2005 4:36 am
Posts: 14
Location: Germany
MySQL's fulltext search works with Hibernate 2. Since update to Hibernate 3 it does not work any more.

Hibernate version: 3.0.5

Name and version of the database you are using: MySQL 4.1.11

Full stack trace of any exception that occurs:
Code:
org.hibernate.hql.ast.QuerySyntaxError:
unexpected token: AGAINST near line 1, column 78
[from db.Product p where MATCH (name) AGAINST ('test' IN BOOLEAN MODE)]
   at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:63)
   at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:215)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:127)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:83)
   at org.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:427)
   at org.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:884)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:834)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 5:52 am 
Newbie

Joined: Fri Jun 24, 2005 4:36 am
Posts: 14
Location: Germany
It seems that you have to use something like
Code:
Restrictions.sqlRestriction("MATCH (name) AGAINST ('test' IN BOOLEAN MODE)")


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 10:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Add your own SQLFunction to a custom subclass of MySQLDialect.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 16, 2005 11:10 am 
Newbie

Joined: Tue Aug 16, 2005 10:56 am
Posts: 2
And how exactly do I do that? Are there any examples available?
Here's what I did: I created a custom class

Code:
public class MyMySQLDialect extends org.hibernate.dialect.MySQLDialect {
   public MyMySQLDialect() {
      super();
      registerFunction("match", new MatchFunction(new AnyType(),"?1 AGAINST ?2"));
   }
}


And tried to implement the custom MatchFunction.class copying the code of the SQLFunctionTemplate.class. But it doesn't seem to work, I'm still getting the unexpected token Exception.

Thanks for further help,
Kat


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 06, 2005 11:22 am 
Newbie

Joined: Tue Apr 12, 2005 9:50 am
Posts: 13
Location: Karlsruhe, Germany
The following is one way to implement a working solution:

Just register the a new function name (e.g.
Code:
registerFunction("match_against", new MatchAgainstFunction() );
)

and provide a rendering method for the new function (e.g.
Code:
    public String render(List args, SessionFactoryImplementor factory) throws QueryException {
       
        StringBuffer buf = new StringBuffer();
       
        buf.append("MATCH(");
       
        for ( int i=0; i<args.size()-1; i++) {
            buf.append(args.get(i));
            if (i<args.size()-2) buf.append(",");
        }
        buf.append(")");
       
        buf.append(" AGAINST(");
        buf.append(args.get(args.size()-1));
        buf.append(")");
       
        return buf.toString();
    }
)

With the above example you can use a HQL-Query like
Code:
FROM searchObject WHERE match_against(field1,field2,'searchtext') > 0


The trick is to use the fact that MATCH(...) AGAINST (..) returns a double that shows the relevance of the hit. With this, match_against() just works like any other aggregation-function and you don't need to tweak the grammar.

HTH,
greets, Stephan


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