Skip to content

Commit cfa3932

Browse files
committed
Added some notes about using primary index strategies.
1 parent b0f2143 commit cfa3932

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,36 @@ class MySearchIndex {
9797

9898
}
9999
```
100+
101+
## Using Strategies
102+
103+
You can now customize the strategy of getting/setting the primary index. By default, the `ConfigurationStrategy` is employed,
104+
however we have also included an `AliasStrategy`. The main difference is when `setPrimaryIndex` is called, instead of creating an entry
105+
in the configuration index, it adds an alias (specified by `alias_name` option) on the specified index and deletes all other aliases
106+
for the old primary indices (specified by `index_pattern`).
107+
108+
#### Using the `AliasStrategy`
109+
110+
```php
111+
<?php
112+
113+
$client = new \Elasticsearch\Client();
114+
$indexRotator = new \Zumba\ElasticsearchRotator\IndexRotator($client, 'pizza_shops');
115+
$aliasStrategy = $indexRotator->strategyFactory(IndexRotator::STRATEGY_ALIAS, [
116+
'alias_name' => 'pizza_shops',
117+
'index_pattern' => 'pizza_shops_*'
118+
]);
119+
// Build your index here
120+
$newlyBuiltIndexName = 'pizza_shops_1234102874';
121+
$indexRotator->copyPrimaryIndexToSecondary();
122+
$indexRotator->setPrimaryIndex($newlyBuiltIndexName);
123+
124+
// Now that the alias is set, you can search on that alias instead of having to call `getPrimaryIndex`.
125+
$client->search([
126+
'index' => 'pizza_shops',
127+
'type' => 'shop',
128+
'body' => [] //...
129+
])
130+
```
131+
132+
Since the alias (`pizza_shops`) is mapped to the primary index (`pizza_shops_1234102874`), you can use the alias directly in your client application rather than having to call `getPrimaryIndex()` on the `IndexRotator`. That being said, calling `getPrimaryIndex` won't return the alias, but rather the index that it is aliasing. The secondary entries in the configuration index are still used and reference the actual index names, since the alias can be updated at any time and there wouldn't be a reference to remove the old one.

0 commit comments

Comments
 (0)