0

I have a restaurant list where I want to use filters where the user can filter the restaurantlist on. So this is the data how a filter object would look like:

dishType:(3) ["Pasta", "Barbecue", "Casual Dining"]
dishnationality:(4) ["Korean", "Indian", "Arabian", "German"]
organizeby:"Low Price"

Here I allready have a query for orgranizing them on most rated like this:

export class RestaurantService {
  restaurantsRef: AngularFireList<any>;
  restaurantsList: Observable<any[]>;

  constructor(
    public afDb: AngularFireDatabase,
    public storage: Storage
    // public homePage: HomePage
  ) {
    this.restaurantsRef = afDb.list('/restaurants');
    this.restaurantsList = this.restaurantsRef.valueChanges();
  }

getFilters(filters) {
  if (filters.organizeby === "Most rating") {
     this.restaurantsRef = this.afDb.list('/restaurants', ref => ref.orderByChild('rating'));        
     this.restaurantsList = this.restaurantsRef.valueChanges();
  }

   return this.restaurantsList;
}

But how can I do a multiple query based on the filter object I show above?

Or do I have to think an other way in firebase like storing the restaurants in an array to iterate over en push back the matching values?

What I have tried to do is creating a composite_key in my firebase db:

getFilters(filters) {

    let dishtype = filters.dishtype;
    let dishnationality = filters.dishnationality;
    let composite_key = `${dishnationality}_${dishtype}`;

    console.log(dishtype, dishnationality)
    if(composite_key){
        this.restaurantsRef = this.afDb.list('/restaurants', ref => ref.orderByChild('tag_dishnationality').equalTo(composite_key));
        this.restaurantsList = this.restaurantsRef.valueChanges();
    }


    if (filters.organizeby === "Most rating") {
        this.restaurantsRef = this.afDb.list('/restaurants', ref => ref.orderByChild('rating'));
        this.restaurantsList = this.restaurantsRef.valueChanges();
    }


    return this.restaurantsList;
}

But How do I deal with that multiple array values and combining them?

Sireini
  • 4,142
  • 12
  • 52
  • 91
  • 1
    Firebase Database queries can only order/filter on a single property. It may be possible to combine the values you want to filter on into a single (synthetic) property. For an example of this and other approaches, see my answer in http://stackoverflow.com/questions/26700924/query-based-on-multiple-where-clauses-in-firebase. But that still can't filter on multiple values, so you'll end up with multiple queries that you have to merge client-side either way. In general the Firebase Database is not a good search engine, so you might want to consider using e.g. ElasticSearch for searching. – Frank van Puffelen Apr 01 '18 at 00:05
  • @FrankvanPuffelen Thanks for sharing your answer going to try it with a composite key – Sireini Apr 01 '18 at 10:57
  • @FrankvanPuffelen But in my case dishType and dishnationality can be a multi array. How can I do this with a composite_key? – Sireini Apr 01 '18 at 11:28

1 Answers1

1

If I get you right, you are considering if you should do multiple query from the server or NOT

I'd say you should NOT the server with queries since it's an easier FireBase construct usually filter a single query

In that case, Create a model to get the value from the server by filtering using single parameter perhaps, get your response as an array, and do multiple queries on the array when your data get to client side

That's fast and easy than throwing multiple queries to server because that may result in fatal errors if NOT properly binded

  • The link provided by Frank will elaborate more – Esinniobiwa Quareeb Apr 01 '18 at 06:58
  • Thanks for you answer, I have red the link Frank shared. But now I created a composite key like this in my database: tag_dishnationality: "Oriental_Veggie" Could you please provide me an example of how I could filter this that would be awesome! – Sireini Apr 01 '18 at 12:03