Firebase Database Database

I am currently creating an e-commerce application for iOS, and I am having problems deciding how to structure my database for a script for a user searching for an item by keywords. I was not sure if it would be better to store an array of keywords or just have a string of keywords for this element.

If you have any suggestions, please let me know!

Thanks.

Here I have a structure for an individual element right now:

"items": { "item1_id":{ "brand_id": brandName_id, "itemName":....., "price":....., "keywords": ? } } 
+1
source share
2 answers

In Firebase, you can query a single value or a range of values, so creating a single query to retrieve elements matching multiple keywords will not be possible. However, you can organize your database so that you can easily query for a single keyword. Then you need to combine several queries, etc.

You can organize your data as follows:

 { "items": { "item1_id": { "brand_id": ..., "itemName": ..., "price": ..., "keywords": { "black": true, "t-shirt": true } } } } 

However, as explained in this answer , you will need to define an index for each keyword. To provide effective keyword-based queries, you can create your own keyword mapping with elements:

 { "items": { "item1_id": { "brand_id": ..., "itemName": ..., "price": ..., "keywords": { "black": true, "t-shirt": true } } }, "keywords": { "black": { "item1_id": true }, "t-shirt": { "item1_id": true } } } 

The query for the keyword would be something like this:

 let keyword = "black"; database.ref(`keywords/${keyword}`).once("value", (snapshot) => { snapshot.forEach((idSnapshot) => { database.ref(`items/${idSnapshot.key}`).once("value", (itemSnapshot) => { console.log(JSON.stringify(itemSnapshot.val())); }); }); }); 

The need to maintain your own matching of keywords with elements is a bit painful, but queries will be fast. In addition, multi-user Firebase updates can facilitate support for keyword matching. To create an item:

 database.ref().update({ "items/item1_id": { "brand_id": ..., "itemName": ..., "price": ..., "keywords": { "black": true, "t-shirt": true } }, "keywords/black/item1_id": true, "keywords/t-shirt/item1_id": true }); 

To change the keywords of an element (remove black , add blue and leave the t-shirt untouched):

 database.ref().update({ "items/item1_id/keywords": { "black": null, "blue": true }, "keywords/black/item1_id": null, "keywords/blue/item1_id": true }); 

And to remove the item:

 database.ref().update({ "items/item1_id": null, "keywords/blue/item1_id": null, "keywords/t-shirt/item1_id": null }); 
+2
source

What about:

  items item0 color: black type: t-shirt option: design keywords: black_t-shirt_design item1 color: black type: hat option: design keywords: black_hat_design 

then a simple query using startAt and endAt.

So, for example, let's say the user uses a filter to narrow the results and wants to know about all the black hats and it does not matter what parameter parameter:

 itemsRef.queryOrdered(byChild: "keywords") .queryStarting(atValue: "black_hat") .queryEnding(atValue: "black_hat") 

Similarly, if the user wanted to know about all the clothes that are only black, you can request a β€œcolor” child for black.

But if they wanted all black hats with a design option

 itemsRef.queryOrdered(byChild: "keywords") .queryStarting(atValue: "black_hat_design") .queryEnding(atValue: "black_hat_design") 
+2
source

Source: https://habr.com/ru/post/1262038/


All Articles