0

I'm trying to query my firestore like,

db.collection("products").where("price",">=","200").orderBy("relevance")

But it's giving an error that, first orderBy must have the field that's been used in range comparison in my case 'price'. As per the official firebase documentation it's given that

If you include a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field.

In my scenario it's important to have this kind of query, is there any way to achieve this?

Praveen
  • 11
  • 3

1 Answers1

1

As you mentioned, the firestore has few restrictions for orderBy().

To resolve the issue you are facing, you have to order your result by price first, then with relevance.

db.collection("products").where("price",">=","200").orderBy("price").orderBy("relevance")

In above query, the ordering happens first on price then on relevance.

A good SO ref - Firestore one range query with order by on different field

Muthu Thavamani
  • 1,325
  • 9
  • 19
  • Thank you muthu for the response ..but by querying as you said the result will be sorted by price not by relevance , the second orderBy relevance will be on effect only when the price of multiple products are same and then those same price products will be sorted based on relevance...but what i really want to get is full and fully sorted by relevance ...which is not supported now in firestore. Is there any alternative way to make it happen – Praveen Feb 06 '21 at 07:58
  • Have you tried the Frank's answer available on my SO reference link? Rearranging the order and where queries – Muthu Thavamani Feb 06 '21 at 08:05
  • Yes I tried like db.collection('products') .where('gender' ,'==', gender) .orderBy('price') .where('price', '>=', from) .where('price', '<=', to) .orderBy('rating','desc'). But its still not working for me :( – Praveen Feb 06 '21 at 15:51
  • If you include a filter with a range comparison (<, <=, >, >=), your first ordering must be on the same field, therefore you have to order by price first. – marian.vladoi Feb 08 '21 at 09:54