How to use MySQL's full text search from JPA

前端 未结 7 1884
谎友^
谎友^ 2020-12-15 13:55

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

7条回答
  •  攒了一身酷
    2020-12-15 14:24

    Just to complete the answer: I had the same problem, but using the criteria builder. This is how you can get around the limitations in the standart implementation, if you are using EclipseLink:

    1. Cast JPA expression to EclipseLink expression
    2. Use the sql method
    3. If you match against a compound index, create it using the function method

    Example:

        JpaCriteriaBuilder cb = (JpaCriteriaBuilder) cb;
    
        List args = new ArrayList();
        args.add("Keyword");
    
        Expression expr = cb.fromExpression (
          cb.toExpression(
             cb.function("", String.class,
               table.get(Table_.text1), table.get(Table_.text2)) 
          )                          
           .sql("MATCH ? AGAINST (?)", args)
         );
    
        query.where(expr);
    

    If you need to cast the expression to a predicate use the following:

    query.where( cb.gt(expr, 0));
    

提交回复
热议问题