firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.82k stars 885 forks source link

Using external orderby without using used query inequality in firebase firestore #1046

Closed generalomosco closed 5 years ago

generalomosco commented 6 years ago

Is there anyway i can escape GeoPoint as first order in this postion? If i remove GeoPoint from orderby it triggered the below error and if i put the GeoPoint as first orderby, as instructed as below, it mislead the second orderby priceSort..

Uncaught Error: Invalid query. You have a where filter with an inequality (<, <=, >, or >=) on field 'GeoPoint' and so you must also use 'GeoPoint' as your first Query.orderBy(), but your first Query.orderBy() is on field 'priceSort' instead.

 const locations=NearBy({
      center:{
        latitude:4*.*****,
        longitude:7*.*****},
      radius:30000})

    var db = firebase.firestore();
    var AdsQry = db.collection("ads");

      AdsQry = AdsQry
        .where('GeoPoint', '>', locations.lesserGeopoint)
        .where('GeoPoint', '<', locations.greaterThan)
        .orderBy('GeoPoint', 'asc');

        AdsQry = AdsQry.where('complete', '==', 'yes')
        //.where('countrycode', '==', 'IT')
        .orderBy('priceSort', 'desc')

    AdsQry.get().then(function(snapshot) {
      snapshot.forEach((doc) => {
        console.log(doc.id+": priceSort="+doc.data().priceSort+" dateSort="+doc.data().dateSort);
      })
    })

Please one can try it here as same as this, i want the index to order accordingly by numeric jsbin.com/dekafuvevo/1/edit?js,console

google-oss-bot commented 6 years ago

Hmmm this issue does not seem to follow the issue template. Make sure you provide all the required information.

google-oss-bot commented 6 years ago

Hey there! I couldn't figure out what this issue is about, so I've labeled it for a human to triage. Hang tight.

wilhuff commented 5 years ago

What the error message is telling you is that if you're using a where clause with an inequality, your first order by must be on the same field.

This is a limitation of the way indexes work in Firestore: in order to evaluate the inequality, we must perform a scan on an index where that field is in the first position. In other words: the results are going to be ordered by that field, all you get to choose is if there are any other orderings you want to apply after that.

mikelehen commented 5 years ago

I'll also point out that using inequalities on geopoints like you are doing is not going to work as expected. It'll essentially only filter by the latitude, not the longitude (see https://github.com/firebase/firebase-js-sdk/issues/826 for another discussion about this). Unfortunately we do not yet support proper geo queries.