views:

117

answers:

1

What methods are there to get JPQL to match similar strings?

By similar I mean:

  • Contains: search string is found within the string of the matches entity
  • Case-insensitive
  • Small mispellings: e.g. "arow" matches "arrow"

I suspect the first two will be easy, however, I would appreciate help with the last one

Thank you

+2  A: 

The first two are indeed easy to do using the LIKE and LOWER or UPPER keywords. The last one is very hard to do, since it requires you to define how similar two strings need to be. There is no basic keyword to do this easily in JPQL (as far as I know). You could use an algorithm like Levenshtein distance to determine if there is a small mispelling (distance of 1 or 2). This is not done in JPQL though...

Marc
may guess would be that for 3) you should use a database that supports this kind of feature and run it as a native query. this will get you much further than anything you can do in JPQL, while breaking portability
seanizer
@seanizer : Which databases support this feature?
bguiz
postgres supports it: http://www.postgresql.org/docs/8.3/static/fuzzystrmatch.html and in most others it can be implemented using functions / stored procedures. e.g. in Oracle: http://richmurnane.blogspot.com/2006/02/levenshtein-distance-algorithm-oracle.html mysql: http://codejanitor.com/wp/2007/02/10/levenshtein-distance-as-a-mysql-stored-function/ mssql: http://www.bigresource.com/MS_SQL-Levenshtein-Edit-Distance-Algorithm-zRMNNbbv.html
seanizer