Question

In my app engine Java application, i want to use RegEx in datastore query to check prefix in one of my fields of DB. I have a column which stores "UserName" . Now i want to implement "Search user" functionality. For that i'll have to use regular expression to which will search users by some particular prefix value.And I referred pattern in Google App engine link.I want to do exactly described in this link. In that they have used Gqlquery with python but i want to implement that in JAVA app engine application. Further i am using this app engine application in my android client. Please any one can help me out to implement Regular Expression` on DataStore. Thank you.

Was it helpful?

Solution

Refering to the Python workaround for "GQL kinda like" queries, the Java implementation with the low level datastore API should look like this :

// From user input
String usernamePrefix = "XX";

Filter usernameGreaterThanPrefixFilter =
  new FilterPredicate("username",
                      FilterOperator.GREATER_THAN_OR_EQUAL,
                      usernamePrefix);

Filter usernameLessThanLargestPossibleFilter =
  new FilterPredicate("username",
                      FilterOperator.LESS_THAN,
                      usernamePrefix + "\ufffd");

Filter usernameKindaLikeFilter =
  CompositeFilterOperator.and(usernameGreaterThanPrefixFilter, usernameLessThanLargestPossibleFilter);

Query q = new Query("User").setFilter(usernameKindaLikeFilter);

I have never tested this though, if it works I would have learned something thanks to you.

If it does not work, you can always store a fixed length "userPrefix" property on each entity, which would be a substring of the username. You could then query the userPrefix property with a FilterOperator.EQUAL equality filter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top