Index Storefront Search Documents
This document provides procedures developers can use to manually index Storefront search documents.
These procedures are for manually indexing search documents. However, most search indexing is automatic, as explained in Search, Indexing. See also Storefront Search Features, Indexing for an overview of Storefront search document creation and indexing.
Index All Storefront Search Documents
search.rake Rakefile provides a task for (re)indexing all Storefront search documents.
Index all Storefront search documents from a shell session within the environment:
$ bin/rails workarea:search_index:storefront
Index all Storefront search documents within Ruby:
The procedures above index documents inline (i.e. in the foreground).
Alternatively, set the environment variable
false before executing the task to perform the indexing with Sidekiq workers (i.e. in the background):
$ INLINE=false bin/rails workarea:search_index:all
Index Specific Storefront Search Documents
In some cases, you may need to index only specific Storefront search documents. The procedure for this varies for product and non-product search documents.
For non-product search documents (e.g. categories, pages, searches), initialize a Storefront search model and call
#save to index the resulting search document.
catalog_category = Workarea::Catalog::Category.first Search::Storefront::Category.new(catalog_category).save
See Storefront Search Features, Search Models for an explanation of Storefront search models.
For product search documents, don't use a search model directly.
Use any of the following API calls, which create the search model instances for you.
(See Workers for more details regarding the
For example, index one product:
catalog_product = Workarea::Catalog::Product.first Workarea::IndexProduct.perform(catalog_product)
Or, enqueue a background job to index 5 products in bulk (using only a single request to Elasticsearch):
catalog_product_ids = Workarea::Catalog::Product.all.pluck('id').first(5) # => ["71C098FFD9", "9A7160EBF0", "2702316B04", "06C6FC0827", "0FA514641B"] Workarea::BulkIndexProducts.new.perform_async(catalog_product_ids)
Each of the API calls above provides two additional functions, which is why these calls are used instead of creating search models directly.
First, calling any of the above methods sets
:last_indexed_at on each of the affected models in MongoDB.
Among other things, this timestamp is used by workers responsible for keeping indexes fresh.
Second, for each product passed in, these methods create a collection of product entries to represent the document within the Storefront search indexes.
The product entries collection (an instance of
Search::ProductEntries is a collection of search models, each of which produce a search document to be indexed.
This allows more or fewer search documents to be indexed for each product. For example, with Workarea Browse Option installed, a single MongoDB document may be represented by multiple search documents in each search index (e.g. one for each color of the product). In contrast, with Workarea Package Products installed, the child products of a package are not indexed at all (they produce an empty product entries collection).
The product entries abstraction also allows a different search model to be used conditionally. For example, Workarea Browse Option extends product entries to use a different search model for those products that "browse by option". This search model uses only variants of the given option (e.g. blue) to construct the search model. Similarly, Workarea Package Products uses a different search model for "package" products. This search model includes details from the package's "child" products, which themselves are not indexed.
How'd We Do?
Was this article missing something?
Was it not quite what you were looking for?
Do you have suggestions on how we could improve this article or your experience in general?
If so, please take a moment to let us know!
Your invaluable feedback will help everyone better understand the platform!