[Based on MD Jamal's Firestore Like Query (Video on YouTube)]
If you want to do something like this--
Suppose in Firestore , You have Documents with a field tag
The tag has value - "i am cold like lava"
[Here I am assuming you have implemented searchview and ready to search]
Now if you type something in searchview like "cold" or "like java", and want to get those documents, where the tag has those words(queries), then you have to -
First pass a method searchData(s) inside onQueryTextSubmit and pass the word(query) in it(which is String s, or by default it may be String query)
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
searchData(s);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
Create the searchData(s) method:
private void searchData(String s) {
db.collection("collection")
.get()
.addOnSuccessListener(queryDocumentSnapshots -> {
for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
if (Objects.requireNonNull(document.getString("tags")).contains(s.toLowerCase()))
dataList.add(document.toObject(MainModel.class));
}
// update Adapter
Collections.shuffle(dataList);
adapter.notifyDataSetChanged();
});
}
In the above code,
we are getting the tag with document.getString("tags"). And also converting the word(query) to lowercase with s.toLowerCase() in case you type a capital.
After that, from the line
if (Objects.requireNonNull(document.getString("tags")).contains(s.toLowerCase())),
we are checking if the tag has the word s(means the word you typed), and if it has, then we are adding the document into our dataList, by ,
dataList.add(document.toObject(MainModel.class));
After that we are calling Collections.shuffle(dataList); to randomize the data and then updating the adapter , by
adapter.notifyDataSetChanged();
Now if you search, and if the word matches, you will get that data in your recyclerview/listview/anywhere.
Let me know if it works or not
[Sorry for my English]