Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dp-singh/b70b1befffaac1d88d32a1ce7300dd8e to your computer and use it in GitHub Desktop.
Save dp-singh/b70b1befffaac1d88d32a1ce7300dd8e to your computer and use it in GitHub Desktop.
import android.text.TextUtils;
import io.realm.Case;
import io.realm.Realm;
import io.realm.RealmObject;
import io.realm.RealmResults;
public class RealmFullTextSearch {
public static <T extends RealmObject> RealmResults<T> search(Realm realm, Class<T> modelClass, String query, String fieldName, boolean partialSearch){
RealmResults<T> realmResults = realm.where(modelClass).findAll();
if (TextUtils.isEmpty(query)) {
return realmResults;
}
String[] keywords = query.split(" ");
for (String keyword : keywords) {
String spacedKeyword = " " + keyword;
realmResults = realmResults.where().beginsWith(fieldName, keyword, Case.INSENSITIVE).or().contains(fieldName, spacedKeyword, Case.SENSITIVE).findAll();
}
return realmResults;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment