Dynamic Indices
Implementing Dynamic Indices
To enable dynamic indexing, define the $index property in your model using a wildcard pattern. This approach allows the model to recognize and interact with multiple indices that share a common prefix.
In this example, the PageHit model is configured to work with indices that match the pattern page_hits_*
, enabling operations across all indices starting with page_hits_.
Querying Across Dynamic Indices
When querying a model with dynamic indices, the query will span all indices that match the defined pattern, allowing for aggregated data retrieval. This means that you can query your model without specifying the exact index, and the query will automatically search across all matching indices.
Retrieve page hits for page_id 1 across all ‘page_hits_*’ indices
Creating Records with Dynamic Indices
When creating new records, you must explicitly set the target index using the setIndex
method to specify the exact index where the record should be stored.
This pattern ensures that new records are stored in the appropriate index based on your application’s logic, such as using the current date to determine the index name.
Retrieving the Current Record’s Index
Each model instance associated with a dynamic index retains knowledge of the specific index it pertains to. In some cases you may need to know the exact index for a given record. To retrieve the current record’s index, use the getRecordIndex
method
Searching Within a Specific Dynamic Index
To constrain a search to a specific index within the range of dynamic indices, set the desired index using setIndex
before constructing your query.