java
46 lines · 8 steps
Rebuilding a search index at Spring startup
An ApplicationRunner streams every active product and writes it into a search view in fixed-size batches when the app boots.
Explained by
highlit
1@Component
2@Order(20)
3public class ProductSearchIndexRunner implements ApplicationRunner {
4
5 private static final Logger log = LoggerFactory.getLogger(ProductSearchIndexRunner.class);
6
7 private final ProductRepository products;
8 private final ProductSearchView searchView;
9
10 public ProductSearchIndexRunner(ProductRepository products, ProductSearchView searchView) {
11 this.products = products;
12 this.searchView = searchView;
13 }
14
15 @Override
16 public void run(ApplicationArguments args) {
17 if (args.containsOption("skip-index")) {
18 log.info("Skipping product search index rebuild (--skip-index present)");
19 return;
20 }
21
22 StopWatch watch = new StopWatch();
23 watch.start();
24 searchView.clear();
25
26 int indexed = 0;
27 try (Stream<Product> stream = products.streamAllActive()) {
28 List<ProductDocument> batch = new ArrayList<>(500);
29 for (Product product : (Iterable<Product>) stream::iterator) {
30 batch.add(ProductDocument.from(product));
31 if (batch.size() == 500) {
32 searchView.saveAll(batch);
33 indexed += batch.size();
34 batch.clear();
35 }
36 }
37 if (!batch.isEmpty()) {
38 searchView.saveAll(batch);
39 indexed += batch.size();
40 }
41 }
42
43 watch.stop();
44 log.info("Rebuilt product search index: {} documents in {} ms", indexed, watch.getTotalTimeMillis());
45 }
46}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1ApplicationRunner lets you run one-off work after the context is ready, ordered against other runners.
- 2Streaming rows and flushing in fixed-size batches keeps memory flat over large datasets.
- 3A command-line flag gives operators an escape hatch to skip expensive startup work.
Related explainers
java
@Service public class InventoryService { private final RestClient warehouseClient;
Bulkhead-protected HTTP calls in Spring
bulkhead
resilience
fallback
Intermediate
7 steps
java
@Configuration public class DataSourceLoggingConfig { @Bean
Wrapping a Spring DataSource for query tracing
proxy pattern
observability
data source
Intermediate
7 steps
java
@RestController @RequestMapping("/api/products") @Validated public class ProductSearchController {
Validating query params in a Spring controller
validation
pagination
rest-api
Intermediate
8 steps
java
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = PasswordsMatchValidator.class) public @interface PasswordsMatch {
Custom cross-field validation in Spring
bean-validation
custom-constraints
cross-field-validation
Intermediate
8 steps
go
package logparse import ( "bufio"
Splitting multi-line logs with a Scanner
parsing
streaming
bufio
Intermediate
9 steps
java
public final class LogRedactor { private static final Pattern SECRET = Pattern.compile( "(?i)(password|token|api[_-]?key|secret|authorization)\\s*[=:]\\s*\\S+");
Streaming log redaction in Java
regex
streaming-io
try-with-resources
Intermediate
9 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/rebuilding-a-search-index-at-spring-startup-explained-java-4cf4/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.