Hello friends once again!
I need to implement full text search in my android app and I am looking for the best way.
I have discovered few ways to do it,but I am asking for an advice which one will be the best and cause less problems. I have one collection fulldata, which has HTML text in it that I parse after query and then display it. I need to implement full text search in this collection. I found recommended Algolia text search , but it is not exactly what I really need. Or maybe I could search text from cache that caches data when connection is offline,based on firestore docs?
Is it possible to query all data and then store it in the ArrayList or maybe Array,or what will be the best option?
Here is my viewModel that Queries whole collection.
public class DetailActivityViewModel extends ViewModel {
private final FirebaseListLiveData<Data> liveDataQuery;
public DetailActivityViewModel(String id) {
liveDataQuery = new FirebaseListLiveData<>(Data.class, FirebaseFirestore.getInstance()
.collection("fullData").whereEqualTo("mainId", id)
.orderBy("order", Query.Direction.ASCENDING));
}
public FirebaseListLiveData<Data> getLiveDataQuery() {
return liveDataQuery;
}
}
And lastly my custom LiveData.
public class FirebaseListLiveData<T> extends LiveData<List<T>> implements EventListener<QuerySnapshot> {
private Class<T> typeParameter;
private Query query;
private ListenerRegistration listenerRegistration;
public FirebaseListLiveData(Class<T> typeParameter, Query query) {
this.typeParameter = typeParameter;
this.query = query;
}
@Override
protected void onActive() {
if (listenerRegistration == null) {
listenerRegistration = query.addSnapshotListener(this);
}
}
@Override
protected void onInactive() {
if (listenerRegistration != null) {
listenerRegistration.remove();
listenerRegistration = null;
}
}
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {
if (e == null) {
if (queryDocumentSnapshots != null && !queryDocumentSnapshots.isEmpty()) {
postValue(queryDocumentSnapshots.toObjects(typeParameter));
} else {
postValue(null);
}
} else {
postValue(null);
}
}
}