可能在firebase中有一个包含搜索查询? [英] It is possible to have a contains search query in firebase?
本文介绍了可能在firebase中有一个包含搜索查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试过:
Query firebaseSearchQuery =
myRef.orderByChild("from").startAt(searchText).endAt(searchText+"\uf8ff");
但是我想要这样的东西
Query firebaseSearchQuery =
myRef.orderByChild("from").contains(searchText);
有可能吗?
推荐答案
对于小型数据集,您可以使用以下代码:
For small datasets you can use the following code:
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<String> list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
String from = ds.child("from").getValue(String.class);
list.add(from);
}
for(String str : list) {
if(str.contains(searchText)) {
Log.d("TAG", "String found!");
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {}
};
myRef.addListenerForSingleValueEvent(valueEventListener);
不建议将此解决方案用于大型数据集,因为您需要下载整个节点才能进行搜索.在这种情况下,阿尔戈利亚或 Elasticsearch .
This solution is not recommended for large datasets because you'll need to download the entire node in order to make a search. In this case, Algolia or Elasticsearch are recommended.
如果您打算使用 Cloud Firestore ,建议您从此查看我的答案 发布 .有关更多信息,我还建议您观看此 视频 .
If you intend to use Cloud Firestore, I recommend you to see my answer from this post. For more information, I also recommend you see this video.
这篇关于可能在firebase中有一个包含搜索查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文