Hello there, i'm really new to ES, so please forgive me if this is a very basic question. I think it must be a trivial case, but i wasn't able to find a solution.
My problem is this: i have a list of products, and users from my website can add those products to their favorites list. The trouble i'm encountering is that when a user wants to see his favorites list, i need to sort the list based on "date added" to the list, but also be able to sort it based on properties from the products themselves (like Popular, New Products, Price, etc.)
So i have a 'Product' type, and a 'FavoriteItem' type. The 'FavoriteItem' has a property 'productId' which references the product, and a 'userId' property which references the user. It also has a 'dateAdded' property which would hold the date when the user added the product to the list.
None of the solutions i've considered are fully satisfactory to the problem i want to solve:
-
Having the 'FavoriteItem' type as an object type inside an array property of the 'Product' type: i think this is not recommended as there might be a lot of 'FavoriteItem' documents inside the same product, so ES would flatten them and i would lost their relationships. Also i'm not sure if i am able to filter the products based on the userId of the FavoriteItem documents, nor sort the products based on the 'dateAdded' property of them. Also, i want to keep the products list as clean as possible, avoiding to get it 'dirty' from foreign documents.
-
Having the 'FavoriteItem' type as a nested type of the 'Product' type: not considered as the favorite items can be added or removed any time.
-
Having the 'FavoriteItem' type as a child type of the parent 'Product' type: i think there is no way to sort the parent based on a child property yet, so i wouldn't be able to sort by the 'dateAdded' property of the 'FavoriteItem' child document.
-
Not having relationships between 'FavoriteItem' and 'Product' type at all: this is the solution i found more favorable until now. In order to retrieve the user's favorite list, I just simply execute a filter query on all the 'FavoriteItem' documents matching the userId, and sort them by 'dateAdded'. As the favorite list is paginated, i just simply get by n 'FavoriteItem' documents and then execute a multi-get query on the 'Product' type for those n items. However, doing things this way wouldn't allow me to sort on the products properties too, like price, or newest products. I wouldn't want to duplicate values on both documents, but i would be willing to do it if it was the best approach.
So, is there a better approach to my problem?
Thanks a lot