How to do a simple search in string in Firebase database?
In my case I was able to partly achieve a SQL LIKE in the following way:
databaseReference.orderByChild('_searchLastName')
.startAt(queryText)
.endAt(queryText+"\uf8ff")
The character \uf8ff
used in the query is a very high code point in the Unicode range (it is a Private Usage Area [PUA] code). Because it is after most regular characters in Unicode, the query matches all values that start with queryText
.
In this way, searching by "Fre" I could get the records having "Fred, Freddy, Frey" as value in _searchLastName
property from the database.
Querying part of a string in firebase
Is it possible to query just part of the name and get the data Like I have data 12345678 can I somehow just search for 1234?
Sure it is. When it comes to Firestore, you can simply use .startAt() as seen in the following query:
db.collection("collName").orderBy("data").startAt(1234);
When it comes to the Realtime Database, you can use .startAt() too, but as seen below:
db.child("nodeName").orderByChild("data").startAt(1234);
But remember, both queries will return elements that are greater than 1234
. Since 12345678
is greater, it will be present in the result set.
How to search data from firebase by typing any string of given attributes.?
There is no built-in operator on Firebase to search across all properties of a node. If your app requires such functionality, you might want to consider using a different/additional database for that, such as using Algolia for the searches and Firebase for the realtime sync.
Alternatively, you can build your own search structure inside of Firebase, as I've shown in my answer here: How to query based on multiple conditions in Firebase?
Also see:
- Google Firestore: Query on substring of a property value (text search)
- How to query firebase for property with specific value inside all children
- How to perform sql "LIKE" operation on firebase?
- Angularfire: How to access an item by one of it's properties?
How can I perform OR query while searching in firebase?
As @Puf said, you can't achieve it at Firebase Realtime Database but you can do it at client side which mean at the Android part.
First, you cannot use FirebaseUI
which is you are currently using, instead you need to use https://firebase.google.com/docs/database/android/read-and-write#read_data
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// You have to make for each loop
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
DoctorHelperClass doc = snapshot.getValue(DoctorHelperClass.class);
//List them in an array
docList.add(doc);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
mPostReference.addValueEventListener(postListener);
Once you have added all the list of doctors. You can compare them using the arrayList.
You can do something like this.
private void searchDoc(final String inputDoc){
boolean isFound = false;
for (DoctorHelperClass doc in docList){
if (doc.getFullName() == inputDoc && doc.getHospitalName() == inputDoc){
isFound = true;
//Do something if found
}
}
}
I hope you get the concept of it.
Firebase Realtime Database Search by word in between the query?
when I type some word that is in between the query it doesn't work
This is happening because Firebase does not support native indexing or search for text fields in database properties. Additionally, downloading an entire node to search for fields client-side isn't practical at all. To enable full text search of your Firebase realtime database data, I recommend you to use a third-party search service like Algolia or Elasticsearch.
This is an example on how it works with Cloud Firestore but in the same way you can use it with Firebase realtime database.
Flutter: Firebase basic Query or Basic Search code
THIS IS ANOTHER SEARCH CODE THIS WILL SEARCH INSIDE FIREBASE DATABASE
import 'package:flutter/material.dart';
import 'package:firebase_database/firebase_database.dart';
import 'package:firebase_database/ui/firebase_animated_list.dart';
class Db extends StatefulWidget {
@override
HomeState createState() => HomeState();
}
class HomeState extends State<Db> {
List<Item> Remedios = List();
Item item;
DatabaseReference itemRef;
TextEditingController controller = new TextEditingController();
String filter;
final GlobalKey<FormState> formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
item = Item("", "");
final FirebaseDatabase database = FirebaseDatabase.instance; //Rather then just writing FirebaseDatabase(), get the instance.
itemRef = database.reference().child('Remedios');
itemRef.onChildAdded.listen(_onEntryAdded);
itemRef.onChildChanged.listen(_onEntryChanged);
controller.addListener(() {
setState(() {
filter = controller.text;
});
});
}
_onEntryAdded(Event event) {
setState(() {
Remedios.add(Item.fromSnapshot(event.snapshot));
});
}
_onEntryChanged(Event event) {
var old = Remedios.singleWhere((entry) {
return entry.key == event.snapshot.key;
});
setState(() {
Remedios\[Remedios.indexOf(old)\] = Item.fromSnapshot(event.snapshot);
});
}
void handleSubmit() {
final FormState form = formKey.currentState;
if (form.validate()) {
form.save();
form.reset();
itemRef.push().set(item.toJson());
}
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
centerTitle: true,
backgroundColor: new Color(0xFFE1564B),
),
resizeToAvoidBottomPadding: false,
body: Column(
children: <Widget>\[
new TextField(
decoration: new InputDecoration(
labelText: "Type something"
),
controller: controller,
),
Flexible(
child: FirebaseAnimatedList(
query: itemRef,
itemBuilder: (BuildContext context, DataSnapshot snapshot,
Animation<double> animation, int index) {
return Remedios\[index\].name.contains(filter) || Remedios\[index\].form.contains(filter) ? ListTile(
leading: Icon(Icons.message),
title: Text(Remedios\[index\].name),
subtitle: Text(Remedios\[index\].form),
) : new Container();
},
),
),
\],
),
);
}
}
class Item {
String key;
String form;
String name;
Item(this.form, this.name);
Item.fromSnapshot(DataSnapshot snapshot)
: key = snapshot.key,
form = snapshot.value\["form"\],
name = snapshot.value\["name"\];
toJson() {
return {
"form": form,
"name": name,
};
}
}
Related Topics
Why My App Is Not Showing Up on Tablets in Google Play
Show an Alert Dialog in Broadcast Receiver After a System Reboot
Android: How to Periodically Send Location to a Server
How to Maintain Multi Layers of Imageviews and Keep Their Aspect Ratio Based on the Largest One
Manifest Merger Fails for Appcomponentfactory
Android Background Image Size in Pixel
Filtering Listview with Custom (Object) Adapter
Actionbaractivity Is Deprecated
Transparent Actionbar: Custom Tabcolor
Getrunningtasks Doesn't Work in Android L
Sqliteopenhelper Problem with Fully Qualified Db Path Name
Remove Listview Items in Android
How to Fix: "Hax Is Not Working and Emulator Runs in Emulation Mode"
Adding Table Rows Dynamically in Android
Android Studio Rendering Problems
How to Set Textview Textstyle Such as Bold, Italic
How Permission Can Be Checked at Runtime Without Throwing Securityexception