-
-
Save tuantmdev/36e131e1fbce1f06c7c2dfe35b912aa0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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