Skip to content

Commit

Permalink
Making possible to create and list a large amount of site search indi…
Browse files Browse the repository at this point in the history
…ces (#26178)

* #24816 adding fix on the creation of site search indice

* #24816 refactor and optimization

* #24816 change on the batchSize and fix on test

---------

Co-authored-by: erickgonzalez <erick.gonzalez@dotcms.com>
  • Loading branch information
AndreyDotcms and erickgonzalez committed Sep 22, 2023
1 parent af48b17 commit 45044ed
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.dotcms.rest.api.v1.menu.MenuResource;
import com.dotcms.util.IntegrationTestInitService;
import com.dotmarketing.business.APILocator;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.exception.DotDataException;
import com.dotmarketing.sitesearch.business.SiteSearchAPI;
import java.io.IOException;
import java.util.Date;
import java.util.Set;

import com.liferay.portal.model.User;
import org.junit.BeforeClass;
import org.junit.Test;

Expand All @@ -36,6 +40,31 @@ public static void prepare() throws Exception {
contentletIndexAPI = APILocator.getContentletIndexAPI();
}

/**
* Method to test: {@link SiteSearchAPI#createSiteSearchIndex(String, String, int)}
* Given Scenario: Create many (100+) site search indexes, Attempt to load the list.
* ExpectedResult: List should load without errors.
*
*/
@Test
public void test_createSiteSearchIndex_shouldBePossibleToAddMoreThan100() throws IOException, DotDataException {
String timeStamp, indexName, aliasName;
String lastCreatedIndex = "";

final int indicesAmount = 115;
for (int i = 0; i < indicesAmount; i++) {
timeStamp = String.valueOf(new Date().getTime());
indexName = ES_SITE_SEARCH_NAME + "_" + timeStamp;
aliasName = "indexAlias" + "_" + timeStamp;

siteSearchAPI.createSiteSearchIndex(indexName, aliasName, 1);

lastCreatedIndex = indexName;
}

assertTrue(indexAPI.listIndices().contains(lastCreatedIndex));
}

@Test
public void testCreateSiteSearchIndexAndMakeItDefault() throws IOException, DotDataException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -857,31 +857,57 @@ public Map<String,String> getIndexAlias(List<String> indexNames) {
return getIndexAlias(indexNames.toArray(new String[indexNames.size()]));
}

public Map<String,String> getIndexAlias(String[] indexNames) {
public Map<String,String> getIndexAlias(String[] indexNames) {

String[] indexNamesWithPrefix = new String[indexNames.length];
for (int i = 0; i < indexNames.length; i++){
indexNamesWithPrefix[i] = getNameWithClusterIDPrefix(indexNames[i]);
}
final String[] indexNamesWithPrefix = new String[indexNames.length];
for (int i = 0; i < indexNames.length; i++){
indexNamesWithPrefix[i] = getNameWithClusterIDPrefix(indexNames[i]);
}
final int batchSize = 40;
int limit = 0;

//stores the offset to iterate
int currentOffset = 0;
//stores arrays of 50 elements or less
final ArrayList<String[]> partitionLists = new ArrayList<String[]>();

//fill the partition list
while (currentOffset < indexNamesWithPrefix.length) {
//update the limit to the next iteration
limit = Math.min(batchSize, indexNamesWithPrefix.length - currentOffset);
final String[] partition = new String[limit];
//loop to fill the partition
for (int i = 0; i < limit; i++) {
partition[i] = indexNamesWithPrefix[currentOffset];
currentOffset++;
}
partitionLists.add(partition);
}

GetAliasesRequest request = new GetAliasesRequest();
request.indices(indexNamesWithPrefix);
final Map<String,String> alias=new HashMap<>();

GetAliasesResponse response = Sneaky.sneak(()->
RestHighLevelClientProvider.getInstance().getClient()
.indices().getAlias(request, RequestOptions.DEFAULT));
//iterates the partition list
for (final String[] portionElement : partitionLists ) {

Map<String,String> alias=new HashMap<>();
final GetAliasesRequest request = new GetAliasesRequest();

response.getAliases().forEach((indexName, value) -> {
if(UtilMethods.isSet(value)) {
final String aliasName = value.iterator().next().alias();
alias.put(removeClusterIdFromName(indexName), removeClusterIdFromName(aliasName));
}
});
//set the limited values
request.indices(portionElement);

final GetAliasesResponse response = Sneaky.sneak(()->
RestHighLevelClientProvider.getInstance().getClient()
.indices().getAlias(request, RequestOptions.DEFAULT));

response.getAliases().forEach((indexName, value) -> {
if(UtilMethods.isSet(value)) {
final String aliasName = value.iterator().next().alias();
alias.put(removeClusterIdFromName(indexName), removeClusterIdFromName(aliasName));
}
});
}

return alias;
}
}

public String getIndexAlias(String indexName) {
return getIndexAlias(new String[]{indexName}).get(indexName);
Expand Down

0 comments on commit 45044ed

Please sign in to comment.