Skip to content
This repository has been archived by the owner on Sep 19, 2023. It is now read-only.

DATASOLR-484 Add possibility to optimize collection #80

Open
wants to merge 1 commit into
base: 3.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -49,6 +51,7 @@
* @author Francisco Spaeth
* @author Shiradwade Sateesh Krishna
* @author David Webb
* @author Radek Mensik
*/
public interface SolrOperations {

Expand Down Expand Up @@ -511,4 +514,22 @@ <T> FacetAndHighlightPage<T> 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);

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -85,6 +85,7 @@
* @author Christoph Strobl
* @author Joachim Uhrlass
* @author Francisco Spaeth
* @author Radek Mensik
*/
@RunWith(MockitoJUnitRunner.Silent.class)
public class SolrTemplateTests {
Expand Down Expand Up @@ -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;
Expand Down