Karthick
Karthick

Reputation: 1

How do I get a entity from the datastore by filtering a string property?

I have few entities in the datastore. This is the structure of the enitity.

public class YTLink {
    @Id
    String videoId;
    @Index
    String videoTitle;
    @Index
    String videoLink;
    @Index
    String videoThumbnail;
}

when I send a request with a keyword, I should check for the YTLink entities in the datastore which contains that specific keyword in its videoTitle property.

I'm using Objectify btw.

I tried to get all the YTList entities from the datastore and Iterated through that list to check for a match.

List<YTLink> ytLinkList = ObjectifyInitializer.ofy().load().type(YTLink.class).list();

ytLinkList.removeIf(video -> video.getVideoTitle() == null || !video.getVideoTitle().toLowerCase().contains(searchKeyword.trim().toLowerCase()));

I need to know is there a way to apply filter while retrieving the data, Like ObjectifyInitializer.ofy().load().type(YTLink.class).filter("videoTitle",searchKeyword).list(); Something like this

Please let me know if anyone knows how to filter the entities based on the String property. Thanks in advance.

Upvotes: 0

Views: 73

Answers (1)

stickfigure
stickfigure

Reputation: 13556

There isn't a partial match operator in the datastore (ie LIKE '%foo%' in sql).

You can however create a synthetic index that holds all your potential search terms. Add a field to your entity like this:

List<String> searchTerms = new ArrayList<>();

Populate it in an @OnSave method. Then you can filter by the searchTerms field. Be sure to normalize (eg lowercase) both the term generation and the term in the query.

The good thing about this approach is that you can put anything you want in there, not just words from the title. If you want the author name, keywords, etc - just add them. You might also choose to ignore stop words like 'a', 'the', 'and', etc. You can use the lucene library to filter out stop words and even perform stemming if you want.

Upvotes: 1

Related Questions