0

I am trying to filter my Firebase data, my dataset is complex but here is a sample. I am able to retrieve data just fine but I am unable to filter that data any further using the Firebase queries. Here is a sample dataset:

enter image description here

Here is my query:

private double locStart;
private double locEnd;
Firebase mRef;
Firebase mRef1;

    mRef = new Firebase("https://test.firebaseio.com/");
mRef1 = mRef.child("test");

    final Intent intent = getIntent();
    final Inputs inputs = (Inputs) intent.getSerializableExtra("inputs");

    locStart = inputs.getLocLat() - 0.008983;
    locEnd = inputs.getLocLat() + 0.008983;

    Query filterData = mRef1.orderByChild("locLat").startAt(locStart).endAt(locEnd);

    filterData.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Match matchd = dataSnapshot.getValue(Match.class);
            Offer.setText(matchd.getfbName());
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

The reference mRef1 retrieves data perfectly, its only when I apply the query to it is when it retrieves null values, the data certainly matches the query, I have even hard coded the values but that does not seem to work either.

Am I missing something I should have setup on the Firebase console? I did setup an index and that solved a warning in my log which was suggesting me to set one up on the field meaning that Firebase is recognising the field and the index as well.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Akber Ali
  • 13
  • 6

1 Answers1

2

Firebase queries return a subset of the child nodes in a list.

So if you have a list of locations:

locations: {
  location1: { lat: 42.010185, lon: -33.010185 },
  location2: { lat: -11.19645, lon:  52.461219 },
  location3: { lat: 33.14518,  lon: -11.128746 }
}

You could query the locations with:

ref.child("locations").orderByChild("lat").startAt(-12.0).endAt(-10)

Your data is missing a level: you're querying test, but are missing the location1, location2, location3 level that I have under it. If you add such a level, your query will be able to match the child.

Your next problem is likely going to be that Firebase Database queries can only order by/filter on a single property. That means that for geo-queries, you can currently only filter on longitude or on latitude, which makes for only half a geo-query. I recommend that you look into GeoFire, which is our library for geo-querying. It combines latitude and longitude of each item into a single geohash value, which can then be queried.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks Frank. You were absolutely right, it was the way I entered data into Firebase and also the way in which I was retrieving the data with the assumption that there will only be one record, just used a for loop to loop through the dataSnapshot and I was just fine. Thanks for the help. Also, if I may ask you, is there any piece of code or Git repo out there to save the data in the format you suggested? – Akber Ali Jan 11 '17 at 03:17
  • I provided a link to Geofire, a library that does precisely that. – Frank van Puffelen Jan 11 '17 at 04:22
  • I did actually read through it but I was not sure how I can push the rest of the data along with the location. I know I need to change both the way the data is inputted and retrieved but is there a way to input the rest of my data along with the Geofire location that you would know of? – Akber Ali Jan 11 '17 at 04:48