I am struggling create an index

I have documents where there is “product” field. I want to create an index where “product” is equal to the value “3in1”.

An index is a lookup table for documents in one or more collections. It would be inefficient to create an index exclusively for a single product, especially if you have thousands of products.

Typically, you’d use an index to find all of the documents that match a certain condition. In your case, that would be the product field. To do that, you could create an index like this:

CreateIndex({
  name: "documents_by_product",
  source: Collection("collection_name"),
  terms: [
    { field: ["data", "product"] }
  ],
})

You didn’t mention the name of the collection containing your documents, so I named this index “documents_by_product”, and the source is the “collection_name” collection. You should update both of those to match what you have.

The important bit is the terms definition. That’s where we specify which document fields should be included in the index for searching.

Once the index is has been created and is active, then you can search for a product identifier:

Paginate(Match(Index("documents_by_product"), "3in1"))

Here, you should update the index name to match the change you made to the index itself.

By default, indexes return each matching document’s reference. If you want to retrieve the full document, you can do that with:

Map(
  Paginate(Match(Index("documents_by_products"), "3in1")),
  Lambda("X", Get(Var("X")))
)

If you only need to fetch a field fields from the document, you can create an index that also contains those fields via the values definition:

CreateIndex({
  name: "documents_by_product",
  source: Collection("products"),
  terms: [
    { field: ["data", "product"] }
  ],
  values: [
    { field: ["data", "price"] },
    { field: ["ref"] },
  ]
})

Here, we’re using the same basic index definition as before, but with the addition of the values field. In values, we’ve configured two fields to return for matching index entries: the user-defined price field, and the document’s reference.

With this index, the Paginate query (as opposed to the Map query) would return the price field and the reference, instead of just the reference. If the price field is all you need, then you don’t need to fetch the full document. You incur fewer read operations using this approach, but the indexes are larger so the storage goes up.

Thank you @ewan. I will follow this approach.:+1::+1::+1: