Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search for substring within an entity using objectify

I have an entity called lastName with value "Benjamin". Is there a way in objectify that if the user put "Ben" or "jam" or "Benja". I still be able to find this entity using query.filter(). I must use the query as there are other search criteria iam checking.

I saw something in "Obgaektify" called "starts with" operator. but it isnot working. Any suggestions would be appreciated. Thanks

like image 481
Shady Hussein Avatar asked Aug 10 '11 16:08

Shady Hussein


1 Answers

There's no "LIKE" type queries for sub-string, however a case sensitive "starts with" could be simulated by taking advantage of the > and < operators on indexes.

// The start string
String searchStr = "Ben";

// emulate a "starts with" query
Query q = new Query("MyEntity")
q.addFilter("name", Query.FilterOperator.GREATER_THAN_OR_EQUAL, searchStr);
q.addFilter("name", Query.FilterOperator.LESS_THAN, searchStr + "\ufffd");

The query will 'search' the name property for items that begining with "Ben", and are less than "Ben\ufffd", where \ufffd is the highest possible unicode character.

like image 136
Chris Farmiloe Avatar answered Oct 03 '22 03:10

Chris Farmiloe