如何使用JPA的MySQL全文搜索 [英] How to use MySQL's full text search from JPA

查看:342
本文介绍了如何使用JPA的MySQL全文搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 MySQL的全文搜索功能使用JPA,而不必使用本机查询。



我使用EclipseLink,它有一个支持原生SQL命令的功能: FUNC 。但是,帮助示例只显示了这个功能与简单的MySQL功能一起使用。我尽最大努力试图让它与MATCH&反对如下:

  @PersistenceContext(name =test)
EntityManager em;

Query query = em.createQuery(SELECT person FROM People person WHERE FUNC('MATCH',person.name)FUNC('AGAINST',:searchTerm));
...
query.getResultList();

以下例外:

引起:NoViableAltException(32 @ [()* 822:9的循环:(m = MULTIPLY右= arithmeticFactor | d = DIVIDE右= arithmeticFactor)*])
at org .eclipse.persistence.internal.libraries.antlr.runtime.DFA.noViableAlt(DFA.java:159)$ or $
at org.eclipse.persistence.internal.libraries.antlr.runtime.DFA.predict(DFA.java:
at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.arithmeticTerm(JPQLParser.java:4557)
... 120 more

我对使用FUNC方法的其他选择开放。



我正在使用EJB 3和EclipseLink 2.3.1。

解决方案

FUNC只适用于正常的打印功能,




MATCH(arg1,arg2)



由于MATCH arg1 AGAINST arg2不是以通常打印函数的方式打印,FUNC不能用于调用它。 / p>

EclipseLink ExpressionOperators确实支持这样的打印功能,因此您可以定义自己的ExpressionOperator,但ExpressionOperators目前仅支持通过EclipseLink Expression查询,而不支持JPQL。您可以登录增强功能以​​在JPQL中获得操作员支持。



您也可以使用原生SQL查询。


I want to use MySQL's full text search features using JPA, without having to use a native query.

I am using EclipseLink, which has a function to support native SQL commands: FUNC. However, the help examples only show this being use with simple MySQL functions. My best effort attempt to get it to work with MATCH & AGAINST is as follows:

@PersistenceContext(name="test")
EntityManager em;

Query query = em.createQuery("SELECT person FROM People person WHERE FUNC('MATCH', person.name) FUNC('AGAINST', :searchTerm)");
...
query.getResultList();

Which gives the following exception:

Caused by: NoViableAltException(32@[()* loopback of 822:9: (m= MULTIPLY right= arithmeticFactor | d= DIVIDE right= arithmeticFactor )*])
    at org.eclipse.persistence.internal.libraries.antlr.runtime.DFA.noViableAlt(DFA.java:159)
    at org.eclipse.persistence.internal.libraries.antlr.runtime.DFA.predict(DFA.java:116)
    at org.eclipse.persistence.internal.jpa.parsing.jpql.antlr.JPQLParser.arithmeticTerm(JPQLParser.java:4557)
    ... 120 more

I am open to alternatives other that using the FUNC method.

I am using EJB 3 and EclipseLink 2.3.1.

解决方案

FUNC only works with normal printed functions,

i.e. MATCH(arg1, arg2)

since MATCH arg1 AGAINST arg2 is not printed the way a function is normally printed, FUNC cannot be used to call it.

EclipseLink ExpressionOperators do support printing functions like this, so you could define your own ExpressionOperator, but ExpressionOperators are only supported through EclipseLink Expression queries currently, not through JPQL. You could log an enhancement to have operator support in JPQL.

You could also use a native SQL query.

这篇关于如何使用JPA的MySQL全文搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆