Reputation: 10891
I have a User
Entity class that looks like this:
@Entity
public class User {
@Id
private Long id;
@Index
private String username;
private Long dateOfBirth;
// More fields...
}
I would like to create an API method that searches datastore for all User
entities that more or less match the search text and return a list of possible results. For example;
public CollectionResponse<User> searchForUser(@Named("text") searchText searchText) {
// Some code here.
}
This API method will be used in an Android app where a user types in a username one character at a time. Each time a character is typed I will have the searchForUser()
method run with what they have typed so far and return the list of possible suggestions.
Does anyone have any suggestions on how to get started on such a method if you have done it before? Thanks.
Upvotes: 1
Views: 47
Reputation: 41089
Based on your use case, you will always return a number of results up to a certain limit (e.g. 5). Thus, you can run a standard query with an inequality filter on username property and set a limit on the number of results returned. Make sure you add a sort order on username.
For example, if a user typed "an", you will search for all entities with username >= "an".
When you get your results back, remove those that do not start with the user input. For example,
andre - add to response
andrei - add to response
boris - do not add and stop - all subsequent entries won't match
Now you know that only two results matched user input.
Upvotes: 2