【发布时间】:2018-09-10 10:41:20
【问题描述】:
我有一个餐厅列表,我想在其中使用过滤器,用户可以在其中过滤餐厅列表。所以这是过滤器对象的数据:
dishType:(3) ["Pasta", "Barbecue", "Casual Dining"]
dishnationality:(4) ["Korean", "Indian", "Arabian", "German"]
organizeby:"Low Price"
在这里,我已经有一个查询,可以按以下方式组织它们:
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;
}
但是如何根据上面显示的过滤器对象进行多重查询呢?
或者我是否必须在 firebase 中考虑其他方式,例如将餐厅存储在数组中以迭代并推回匹配值?
我试图做的是在我的 firebase 数据库中创建一个复合键:
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;
}
但是我该如何处理多个数组值并将它们组合起来呢?
【问题讨论】:
-
Firebase 数据库查询只能对单个属性进行排序/过滤。可以将要过滤的值组合成单个(合成)属性。有关此方法和其他方法的示例,请参阅我在 stackoverflow.com/questions/26700924/… 中的回答。但这仍然无法过滤多个值,因此您最终会得到多个查询,无论哪种方式都必须合并客户端。一般来说,Firebase 数据库不是一个好的搜索引擎,因此您可能需要考虑使用例如ElasticSearch 用于搜索。
-
@FrankvanPuffelen 感谢您分享您的答案,将尝试使用复合键
-
@FrankvanPuffelen 但在我的情况下,dishType 和 disnationality 可以是一个多数组。如何使用 Composite_key 做到这一点?
标签: firebase firebase-realtime-database ionic3 observable angularfire2