Commit 9ecd12ed839a10b5031fd52efbbef550802ad70e
1 parent
24615363
Exists in
master
and in
19 other branches
Moved partial reindexing to performance section [skip ci]
Showing
1 changed file
with
27 additions
and
15 deletions
Show diff stats
README.md
... | ... | @@ -1158,6 +1158,33 @@ Reindex and search with: |
1158 | 1158 | Business.search "ice cream", routing: params[:city_id] |
1159 | 1159 | ``` |
1160 | 1160 | |
1161 | +### Partial Reindexing | |
1162 | + | |
1163 | +Reindex a subset of attributes to reduce time spent generating search data and cut down on network traffic. | |
1164 | + | |
1165 | +```ruby | |
1166 | +class Product < ActiveRecord::Base | |
1167 | + def search_data | |
1168 | + { | |
1169 | + name: name | |
1170 | + }.merge(search_prices) | |
1171 | + end | |
1172 | + | |
1173 | + def search_prices | |
1174 | + { | |
1175 | + price: price, | |
1176 | + sale_price: sale_price | |
1177 | + } | |
1178 | + end | |
1179 | +end | |
1180 | +``` | |
1181 | + | |
1182 | +And use: | |
1183 | + | |
1184 | +```ruby | |
1185 | +Product.reindex(:search_prices) | |
1186 | +``` | |
1187 | + | |
1161 | 1188 | ### Performant Conversions |
1162 | 1189 | |
1163 | 1190 | Split out conversions into a separate method so you can use partial reindexing, and cache conversions to prevent N+1 queries. Be sure to use a centralized cache store like Memcached or Redis. |
... | ... | @@ -1396,21 +1423,6 @@ Reindex associations |
1396 | 1423 | store.products.reindex |
1397 | 1424 | ``` |
1398 | 1425 | |
1399 | -Reindex a subset of attributes (partial reindex) | |
1400 | - | |
1401 | -```ruby | |
1402 | -class Product < ActiveRecord::Base | |
1403 | - def search_prices | |
1404 | - { | |
1405 | - price: price, | |
1406 | - sale_price: sale_price | |
1407 | - } | |
1408 | - end | |
1409 | -end | |
1410 | - | |
1411 | -Product.reindex(:search_prices) | |
1412 | -``` | |
1413 | - | |
1414 | 1426 | Remove old indices |
1415 | 1427 | |
1416 | 1428 | ```ruby | ... | ... |