1

I have a "products" collection:

products:{
  PD1232:{
    name:"Brick",
    type: LPK52 
  },
  PD553: {
    name:"Panel",
    type: DX343
  }
}

The products collection is referenced to a "lookup" collection:

productTypes:{
   LPK52:{
     name: "Concrete mixed with dried bamboo"
   },
   DX343: {
     name: "bla bla........"
   }
}

In terms of searching the products, I want to index the "products" collection as a whole. It means I want to also combine a collection and its referenced collection when indexing. So for example when I search with the term "Concrete mixed with dried bamboo", it also returns the respective product as a result (the one with uid=LPK52). Please give me the solution?

Tran Loc
  • 433
  • 2
  • 7

2 Answers2

1

You can first search for product types by name ("Concrete mixed with dried bamboo") and that will give you type IDs, then search for products by type ("LPK52"). I am not aware of any shortcuts.

Or, if you're willing to maintain it when you edit a type, you can add a typeName field in your products and search directly in them by typeName.

Stratubas
  • 2,939
  • 1
  • 13
  • 18
  • Thanks for answering my question but I am not really convinced with your approach as It more about querying than searching. You have to know the type before hand right? In case the product name might contain the search terms, then how do you identify which one is productType? – Tran Loc Aug 10 '20 at 03:32
  • What do you mean by "searching"? How is it different from "querying"? Searching for substrings in a string field is tricky. One way to do it is to make another "array" field that will contain all possibly searched substrings, and perform an `array-contains` query: https://stackoverflow.com/a/52715590/6002078 – Stratubas Aug 10 '20 at 03:38
  • I thought about this appoach before but for a sake of scaling or future change. Changing a productType name might be expensive as it is in the "products" collection for million records right? I am still thinking that firestore has the solution but I haven't found so far! – Tran Loc Aug 10 '20 at 03:41
  • Document writes cost something more than $0.18/100K, so whether it's disastrous or not depends on how frequently you're gonna rename types. – Stratubas Aug 10 '20 at 03:45
1

There is no way to search in one collection and get results from another collection.

One option is to do what Stratubas describes: first find the product types matching your condition, then find the products for that type.

If you don't want to do that, you can consider duplicating the product type name into each product document as a field. That would allow you to immediately query on products based on their product type's name.

Stratubas
  • 2,939
  • 1
  • 13
  • 18
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807