Search in sources :

Example 16 with Searcher

use of org.elasticsearch.index.engine.Engine.Searcher in project elasticsearch by elastic.

the class InternalEngineTests method testTranslogReplayWithFailure.

public void testTranslogReplayWithFailure() throws IOException {
    final int numDocs = randomIntBetween(1, 10);
    for (int i = 0; i < numDocs; i++) {
        ParsedDocument doc = testParsedDocument(Integer.toString(i), "test", null, testDocument(), new BytesArray("{}"), null);
        Engine.Index firstIndexRequest = new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_DELETED, VersionType.INTERNAL, PRIMARY, System.nanoTime(), -1, false);
        Engine.IndexResult indexResult = engine.index(firstIndexRequest);
        assertThat(indexResult.getVersion(), equalTo(1L));
    }
    engine.refresh("test");
    try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), randomIntBetween(numDocs, numDocs + 10));
        assertThat(topDocs.totalHits, equalTo(numDocs));
    }
    engine.close();
    final MockDirectoryWrapper directory = DirectoryUtils.getLeaf(store.directory(), MockDirectoryWrapper.class);
    if (directory != null) {
        // since we rollback the IW we are writing the same segment files again after starting IW but MDW prevents
        // this so we have to disable the check explicitly
        boolean started = false;
        final int numIters = randomIntBetween(10, 20);
        for (int i = 0; i < numIters; i++) {
            directory.setRandomIOExceptionRateOnOpen(randomDouble());
            directory.setRandomIOExceptionRate(randomDouble());
            directory.setFailOnOpenInput(randomBoolean());
            directory.setAllowRandomFileNotFoundException(randomBoolean());
            try {
                engine = createEngine(store, primaryTranslogDir);
                started = true;
                break;
            } catch (EngineException | IOException e) {
            }
        }
        directory.setRandomIOExceptionRateOnOpen(0.0);
        directory.setRandomIOExceptionRate(0.0);
        directory.setFailOnOpenInput(false);
        directory.setAllowRandomFileNotFoundException(false);
        if (started == false) {
            engine = createEngine(store, primaryTranslogDir);
        }
    } else {
        // no mock directory, no fun.
        engine = createEngine(store, primaryTranslogDir);
    }
    try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), randomIntBetween(numDocs, numDocs + 10));
        assertThat(topDocs.totalHits, equalTo(numDocs));
    }
}
Also used : Searcher(org.elasticsearch.index.engine.Engine.Searcher) MockDirectoryWrapper(org.apache.lucene.store.MockDirectoryWrapper) BytesArray(org.elasticsearch.common.bytes.BytesArray) Index(org.elasticsearch.index.Index) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LongPoint(org.apache.lucene.document.LongPoint) TopDocs(org.apache.lucene.search.TopDocs) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument)

Example 17 with Searcher

use of org.elasticsearch.index.engine.Engine.Searcher in project elasticsearch by elastic.

the class ValuesSourceConfigTests method testLong.

public void testLong() throws Exception {
    IndexService indexService = createIndex("index", Settings.EMPTY, "type", "long", "type=long");
    client().prepareIndex("index", "type", "1").setSource("long", 42).setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
        ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve(context, null, "long", null, null, null, null);
        ValuesSource.Numeric valuesSource = config.toValuesSource(context);
        LeafReaderContext ctx = searcher.reader().leaves().get(0);
        SortedNumericDocValues values = valuesSource.longValues(ctx);
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(42, values.valueAt(0));
    }
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) IndexService(org.elasticsearch.index.IndexService) Searcher(org.elasticsearch.index.engine.Engine.Searcher) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 18 with Searcher

use of org.elasticsearch.index.engine.Engine.Searcher in project elasticsearch by elastic.

the class ValuesSourceConfigTests method testEmptyBoolean.

public void testEmptyBoolean() throws Exception {
    IndexService indexService = createIndex("index", Settings.EMPTY, "type", "bool", "type=boolean");
    client().prepareIndex("index", "type", "1").setSource().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
        ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve(context, null, "bool", null, null, null, null);
        ValuesSource.Numeric valuesSource = config.toValuesSource(context);
        LeafReaderContext ctx = searcher.reader().leaves().get(0);
        SortedNumericDocValues values = valuesSource.longValues(ctx);
        values.setDocument(0);
        assertEquals(0, values.count());
        config = ValuesSourceConfig.resolve(context, null, "bool", null, true, null, null);
        valuesSource = config.toValuesSource(context);
        values = valuesSource.longValues(ctx);
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(1, values.valueAt(0));
    }
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) IndexService(org.elasticsearch.index.IndexService) Searcher(org.elasticsearch.index.engine.Engine.Searcher) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 19 with Searcher

use of org.elasticsearch.index.engine.Engine.Searcher in project elasticsearch by elastic.

the class ValuesSourceConfigTests method testUnmappedBoolean.

public void testUnmappedBoolean() throws Exception {
    IndexService indexService = createIndex("index", Settings.EMPTY, "type");
    client().prepareIndex("index", "type", "1").setSource().setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE).get();
    try (Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
        QueryShardContext context = indexService.newQueryShardContext(0, searcher.reader(), () -> 42L);
        ValuesSourceConfig<ValuesSource.Numeric> config = ValuesSourceConfig.resolve(context, ValueType.BOOLEAN, "bool", null, null, null, null);
        ValuesSource.Numeric valuesSource = config.toValuesSource(context);
        assertNull(valuesSource);
        config = ValuesSourceConfig.resolve(context, ValueType.BOOLEAN, "bool", null, true, null, null);
        valuesSource = config.toValuesSource(context);
        LeafReaderContext ctx = searcher.reader().leaves().get(0);
        SortedNumericDocValues values = valuesSource.longValues(ctx);
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(1, values.valueAt(0));
    }
}
Also used : SortedNumericDocValues(org.apache.lucene.index.SortedNumericDocValues) IndexService(org.elasticsearch.index.IndexService) Searcher(org.elasticsearch.index.engine.Engine.Searcher) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Example 20 with Searcher

use of org.elasticsearch.index.engine.Engine.Searcher in project elasticsearch by elastic.

the class InternalEngineTests method testUpgradeOldIndex.

public void testUpgradeOldIndex() throws IOException {
    List<Path> indexes = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(getBwcIndicesPath(), "index-*.zip")) {
        for (Path path : stream) {
            indexes.add(path);
        }
    }
    Collections.shuffle(indexes, random());
    for (Path indexFile : indexes.subList(0, scaledRandomIntBetween(1, indexes.size() / 2))) {
        final String indexName = indexFile.getFileName().toString().replace(".zip", "").toLowerCase(Locale.ROOT);
        Path unzipDir = createTempDir();
        Path unzipDataDir = unzipDir.resolve("data");
        // decompress the index
        try (InputStream stream = Files.newInputStream(indexFile)) {
            TestUtil.unzip(stream, unzipDir);
        }
        // check it is unique
        assertTrue(Files.exists(unzipDataDir));
        Path[] list = filterExtraFSFiles(FileSystemUtils.files(unzipDataDir));
        if (list.length != 1) {
            throw new IllegalStateException("Backwards index must contain exactly one cluster but was " + list.length + " " + Arrays.toString(list));
        }
        // the bwc scripts packs the indices under this path
        Path src = OldIndexUtils.getIndexDir(logger, indexName, indexFile.toString(), list[0]);
        Path translog = src.resolve("0").resolve("translog");
        assertTrue("[" + indexFile + "] missing translog dir: " + translog.toString(), Files.exists(translog));
        Path[] tlogFiles = filterExtraFSFiles(FileSystemUtils.files(translog));
        // ckp & tlog
        assertEquals(Arrays.toString(tlogFiles), tlogFiles.length, 2);
        Path tlogFile = tlogFiles[0].getFileName().toString().endsWith("tlog") ? tlogFiles[0] : tlogFiles[1];
        final long size = Files.size(tlogFile);
        logger.debug("upgrading index {} file: {} size: {}", indexName, tlogFiles[0].getFileName(), size);
        Directory directory = newFSDirectory(src.resolve("0").resolve("index"));
        final IndexMetaData indexMetaData = IndexMetaData.FORMAT.loadLatestState(logger, xContentRegistry(), src);
        final IndexSettings indexSettings = IndexSettingsModule.newIndexSettings(indexMetaData);
        final Store store = createStore(indexSettings, directory);
        final int iters = randomIntBetween(0, 2);
        int numDocs = -1;
        for (int i = 0; i < iters; i++) {
            // make sure we can restart on an upgraded index
            try (InternalEngine engine = createEngine(indexSettings, store, translog, newMergePolicy())) {
                try (Searcher searcher = engine.acquireSearcher("test")) {
                    if (i > 0) {
                        assertEquals(numDocs, searcher.reader().numDocs());
                    }
                    TopDocs search = searcher.searcher().search(new MatchAllDocsQuery(), 1);
                    numDocs = searcher.reader().numDocs();
                    assertTrue(search.totalHits > 1);
                }
                CommitStats commitStats = engine.commitStats();
                Map<String, String> userData = commitStats.getUserData();
                assertTrue("user data doesn't contain uuid", userData.containsKey(Translog.TRANSLOG_UUID_KEY));
                assertTrue("user data doesn't contain generation key", userData.containsKey(Translog.TRANSLOG_GENERATION_KEY));
                assertFalse("user data contains legacy marker", userData.containsKey("translog_id"));
            }
        }
        try (InternalEngine engine = createEngine(indexSettings, store, translog, newMergePolicy())) {
            if (numDocs == -1) {
                try (Searcher searcher = engine.acquireSearcher("test")) {
                    numDocs = searcher.reader().numDocs();
                }
            }
            final int numExtraDocs = randomIntBetween(1, 10);
            for (int i = 0; i < numExtraDocs; i++) {
                ParsedDocument doc = testParsedDocument("extra" + Integer.toString(i), "test", null, testDocument(), new BytesArray("{}"), null);
                Engine.Index firstIndexRequest = new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_DELETED, VersionType.INTERNAL, PRIMARY, System.nanoTime(), -1, false);
                Engine.IndexResult indexResult = engine.index(firstIndexRequest);
                assertThat(indexResult.getVersion(), equalTo(1L));
            }
            engine.refresh("test");
            try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
                TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), randomIntBetween(numDocs, numDocs + numExtraDocs));
                assertThat(topDocs.totalHits, equalTo(numDocs + numExtraDocs));
            }
        }
        IOUtils.close(store, directory);
    }
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) ArrayList(java.util.ArrayList) Store(org.elasticsearch.index.store.Store) Index(org.elasticsearch.index.Index) Matchers.containsString(org.hamcrest.Matchers.containsString) TopDocs(org.apache.lucene.search.TopDocs) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Directory(org.apache.lucene.store.Directory) Path(java.nio.file.Path) ContentPath(org.elasticsearch.index.mapper.ContentPath) Searcher(org.elasticsearch.index.engine.Engine.Searcher) BytesArray(org.elasticsearch.common.bytes.BytesArray) InputStream(java.io.InputStream) Searcher(org.elasticsearch.index.engine.Engine.Searcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LongPoint(org.apache.lucene.document.LongPoint) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

Searcher (org.elasticsearch.index.engine.Engine.Searcher)22 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)11 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)11 TopDocs (org.apache.lucene.search.TopDocs)10 BytesArray (org.elasticsearch.common.bytes.BytesArray)10 Index (org.elasticsearch.index.Index)10 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)9 IndexService (org.elasticsearch.index.IndexService)9 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)9 LongPoint (org.apache.lucene.document.LongPoint)8 SortedNumericDocValues (org.apache.lucene.index.SortedNumericDocValues)6 IOException (java.io.IOException)4 UncheckedIOException (java.io.UncheckedIOException)4 ArrayList (java.util.ArrayList)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 BytesRef (org.apache.lucene.util.BytesRef)3 SortedBinaryDocValues (org.elasticsearch.index.fielddata.SortedBinaryDocValues)3 Path (java.nio.file.Path)2 CountDownLatch (java.util.concurrent.CountDownLatch)2