From 960a3a201d1dc388c6bc800982cbd703ece03479 Mon Sep 17 00:00:00 2001 From: Radek Mensik Date: Thu, 27 Sep 2018 17:36:21 +0200 Subject: [PATCH] DATASOLR-484 Add possibility to optimize collection --- .../data/solr/core/SolrOperations.java | 23 ++++++++++++++++++- .../data/solr/core/SolrTemplate.java | 8 ++++++- .../data/solr/core/SolrTemplateTests.java | 13 ++++++++++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/solr/core/SolrOperations.java b/src/main/java/org/springframework/data/solr/core/SolrOperations.java index 44d23cabe..2bcc3c2bb 100644 --- a/src/main/java/org/springframework/data/solr/core/SolrOperations.java +++ b/src/main/java/org/springframework/data/solr/core/SolrOperations.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 the original author or authors. + * Copyright 2012 - 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,11 +15,13 @@ */ package org.springframework.data.solr.core; +import java.io.IOException; import java.time.Duration; import java.util.Collection; import java.util.Optional; import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.SolrServerException; import org.apache.solr.client.solrj.response.SolrPingResponse; import org.apache.solr.client.solrj.response.UpdateResponse; import org.apache.solr.common.SolrInputDocument; @@ -49,6 +51,7 @@ * @author Francisco Spaeth * @author Shiradwade Sateesh Krishna * @author David Webb + * @author Radek Mensik */ public interface SolrOperations { @@ -511,4 +514,22 @@ FacetAndHighlightPage queryForFacetAndHighlightPage(String collection, Fa */ SchemaOperations getSchemaOperations(String collection); + + /** + * Calls SolrClien.optmize, which has following documentation: + * + * Performs an explicit optimize, causing a merge of all segments to one. + * + * waitFlush=true and waitSearcher=true to be inline with the defaults for plain HTTP access + * + * Note: In most cases it is not required to do explicit optimize + * + * @param collection the Solr collection to send the optimize to + * + * @return an {@link org.apache.solr.client.solrj.response.UpdateResponse} containing the response + * from the server + * + */ + UpdateResponse optimize(String collection); + } diff --git a/src/main/java/org/springframework/data/solr/core/SolrTemplate.java b/src/main/java/org/springframework/data/solr/core/SolrTemplate.java index c5f2fbeff..709bc59e1 100644 --- a/src/main/java/org/springframework/data/solr/core/SolrTemplate.java +++ b/src/main/java/org/springframework/data/solr/core/SolrTemplate.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 the original author or authors. + * Copyright 2012 - 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -91,6 +91,7 @@ * @author Petar Tahchiev * @author Mark Paluch * @author Juan Manuel de Blas + * @author Radek Mensik */ public class SolrTemplate implements SolrOperations, InitializingBean, ApplicationContextAware { @@ -726,6 +727,11 @@ public void afterPropertiesSet() { registerPersistenceExceptionTranslator(); } + @Override + public UpdateResponse optimize(String collection) { + return execute(solrClient -> solrClient.optimize(collection)); + } + private void registerPersistenceExceptionTranslator() { if (this.applicationContext != null && this.applicationContext.getBeansOfType(PersistenceExceptionTranslator.class).isEmpty()) { diff --git a/src/test/java/org/springframework/data/solr/core/SolrTemplateTests.java b/src/test/java/org/springframework/data/solr/core/SolrTemplateTests.java index 96f492d3b..d0238deb4 100644 --- a/src/test/java/org/springframework/data/solr/core/SolrTemplateTests.java +++ b/src/test/java/org/springframework/data/solr/core/SolrTemplateTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012 - 2017 the original author or authors. + * Copyright 2012 - 2018 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -85,6 +85,7 @@ * @author Christoph Strobl * @author Joachim Uhrlass * @author Francisco Spaeth + * @author Radek Mensik */ @RunWith(MockitoJUnitRunner.Silent.class) public class SolrTemplateTests { @@ -521,6 +522,16 @@ public void queryForHighlightPageShouldUseRequestMethodCorrectly() throws IOExce verify(solrClientMock, times(1)).query(any(), any(SolrParams.class), eq(SolrRequest.METHOD.PUT)); } + @Test + public void testOptimize() throws IOException, SolrServerException { + when(solrClientMock.optimize(eq(COLLECTION_NAME))) + .thenReturn(new UpdateResponse()); + UpdateResponse updateResponse = solrTemplate.optimize(COLLECTION_NAME); + assertNotNull(updateResponse); + verify(solrClientMock, times(1)).optimize(eq(COLLECTION_NAME)); + } + + static class DocumentWithIndexAnnotations { @Id String id;