/* * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Contributors: * Antoine Taillefer <ataillefer@nuxeo.com> */ package org.nuxeo.elasticsearch.test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import javax.inject.Inject; import org.apache.commons.collections.CollectionUtils; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.nuxeo.ecm.core.api.CoreSession; import org.nuxeo.ecm.core.api.DocumentModel; import org.nuxeo.ecm.core.api.DocumentModelList; import org.nuxeo.ecm.core.work.api.WorkManager; import org.nuxeo.elasticsearch.api.ElasticSearchAdmin; import org.nuxeo.elasticsearch.api.ElasticSearchService; import org.nuxeo.elasticsearch.api.EsScrollResult; import org.nuxeo.elasticsearch.query.NxQueryBuilder; import org.nuxeo.runtime.test.runner.Features; import org.nuxeo.runtime.test.runner.FeaturesRunner; import org.nuxeo.runtime.test.runner.LocalDeploy; import org.nuxeo.runtime.transaction.TransactionHelper; /** * Tests the scroll search API exposed by {@link ElasticSearchService}. * * @since 8.3 */ @RunWith(FeaturesRunner.class) @Features({ RepositoryElasticSearchFeature.class }) @LocalDeploy("org.nuxeo.elasticsearch.core:elasticsearch-test-contrib.xml") public class TestScrollSearch { @Inject protected CoreSession session; @Inject protected ElasticSearchService ess; @Inject protected ElasticSearchAdmin esa; @Inject protected WorkManager workManager; @Before public void setupIndex() throws Exception { esa.initIndexes(true); } @Test public void testScroll() throws Exception { buildAndIndexTree(100); // Initial search request, includes the first batch of results String query = "select * from Document order by ecm:path"; EsScrollResult res = ess.scroll(new NxQueryBuilder(session).nxql(query).limit(20), 10000); assertNotNull(res); assertNotNull(res.getQueryBuilder()); assertEquals(10000, res.getKeepAlive()); assertNotNull(res.getScrollId()); // Next result batches int totalDocCount = 0; List<String> docPaths = new ArrayList<>(); DocumentModelList docs = res.getDocuments(); while (!docs.isEmpty()) { int hitCount = docs.size(); assertEquals(20, hitCount); totalDocCount += hitCount; docPaths.addAll(docs.stream().map(doc -> doc.getPathAsString()).collect(Collectors.toList())); res = ess.scroll(res); docs = res.getDocuments(); } assertEquals(100, totalDocCount); // Check order assertEquals(session.query(query).stream().map(doc -> doc.getPathAsString()).collect(Collectors.toList()), docPaths); } protected void buildAndIndexTree(int docCount) throws Exception { startTransaction(); buildTree(docCount); TransactionHelper.commitOrRollbackTransaction(); waitForCompletion(); startTransaction(); } protected void buildTree(int docCount) { String root = "/"; for (int i = 0; i < docCount; i++) { String name = "folder" + i; DocumentModel doc = session.createDocumentModel(root, name, "Folder"); doc.setPropertyValue("dc:title", "Folder" + i); session.createDocument(doc); } } protected void startTransaction() { if (!TransactionHelper.isTransactionActive()) { TransactionHelper.startTransaction(); } Assert.assertEquals(0, esa.getPendingWorkerCount()); } /** * Wait for async worker completion then wait for indexing completion */ public void waitForCompletion() throws Exception { workManager.awaitCompletion(20, TimeUnit.SECONDS); esa.prepareWaitForIndexing().get(20, TimeUnit.SECONDS); esa.refresh(); } }