I am using AWS OpenSearch database which is a fork of Elasticsearch. My use case is as follows:
- A product contains an ID, a title, a brand, a productGroupId
- Query: Given a string, find all products that has title OR brand that matches the string. For each returned product, include a
variationsfield which is a list of IDs of all other products within the same productGroupId
My current thinking is I am going to perform the query in 2 steps:
- Query for all products by title and brand
- For each product, query by its productGroupId to populate the
variationsfield (some optimization can be done to minimize the number of productGroupId look ups but that’s beside the point)
Do you have any suggestions on how to design the search index or some queries I can use to make this more efficient?
I would suggest you to create two indexes. One would keep all the product related details, and the other would keep track of all the
productvariations for a givenproductGroupId. The documents of these indices will have the following structure.Product
ProductGroupVariations
Using this structure, when fetching a
product, you can fetchvariations, using one more call toproductGroupVariationsindex.Also, make sure to update the
variationsarray, whenever aproductis added, updated or deleted.