Hibernate + MSSQL + Fulltext Search via Contains SQLFunctionTemplate

限于喜欢 提交于 2019-12-25 01:16:48

问题


I've setup a fulltext index on my customer table to be able to quickly search for customers via their info.

I had to create a custom hibernate dialect to simplify the mapping. and do some funky stuff to get hibernate to work Hibernate + MSSQL + Fulltext Search via Contains

My custom dialect has a function that looks like this

registerFunction("contains", new SQLFunctionTemplate(StandardBasicTypes.BOOLEAN, "CONTAINS(?1, ?2) AND 1"));

This makes it possible to do queries as follows

from Customer c where contains(c.name, :term) = true

My problem now is that to be able to return parial matches I need to quote the term and add a *

So the raw query would be

select * from customer c where CONTAINS(c.name, '"mycust*"');

I've tried wrapping the SQLFunctionTemplate with quotes and a star, and tried quoting at the call site, but neither work.

Any suggestions on how to create a sql function that does the raw query above?


回答1:


Just use Hibernates criteria API and sqlRestriction then you can pass any string to the query u want. E.g. searchValue=myCust* etc...

   Restrictions.sqlRestriction(" CONTAINS(name, ?)", searchValue, StandardBasicTypes.STRING)


来源:https://stackoverflow.com/questions/37751008/hibernate-mssql-fulltext-search-via-contains-sqlfunctiontemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!