Search in sources :

Example 11 with Searcher

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

the class InternalEngineTests method testRetryWithAutogeneratedIdsAndWrongOrderWorksAndNoDuplicateDocs.

public void testRetryWithAutogeneratedIdsAndWrongOrderWorksAndNoDuplicateDocs() throws IOException {
    final ParsedDocument doc = testParsedDocument("1", "test", null, testDocumentWithTextField(), new BytesArray("{}".getBytes(Charset.defaultCharset())), null);
    boolean isRetry = true;
    long autoGeneratedIdTimestamp = 0;
    Engine.Index firstIndexRequest = new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_ANY, VersionType.INTERNAL, PRIMARY, System.nanoTime(), autoGeneratedIdTimestamp, isRetry);
    Engine.IndexResult result = engine.index(firstIndexRequest);
    assertThat(result.getVersion(), equalTo(1L));
    Engine.Index firstIndexRequestReplica = new Engine.Index(newUid(doc), doc, result.getSeqNo(), firstIndexRequest.primaryTerm(), result.getVersion(), firstIndexRequest.versionType().versionTypeForReplicationAndRecovery(), REPLICA, System.nanoTime(), autoGeneratedIdTimestamp, isRetry);
    Engine.IndexResult indexReplicaResult = replicaEngine.index(firstIndexRequestReplica);
    assertThat(indexReplicaResult.getVersion(), equalTo(1L));
    isRetry = false;
    Engine.Index secondIndexRequest = new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, Versions.MATCH_ANY, VersionType.INTERNAL, PRIMARY, System.nanoTime(), autoGeneratedIdTimestamp, isRetry);
    Engine.IndexResult indexResult = engine.index(secondIndexRequest);
    assertTrue(indexResult.isCreated());
    engine.refresh("test");
    try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), 10);
        assertEquals(1, topDocs.totalHits);
    }
    Engine.Index secondIndexRequestReplica = new Engine.Index(newUid(doc), doc, result.getSeqNo(), secondIndexRequest.primaryTerm(), result.getVersion(), firstIndexRequest.versionType().versionTypeForReplicationAndRecovery(), REPLICA, System.nanoTime(), autoGeneratedIdTimestamp, isRetry);
    replicaEngine.index(secondIndexRequestReplica);
    replicaEngine.refresh("test");
    try (Engine.Searcher searcher = replicaEngine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), 10);
        assertEquals(1, topDocs.totalHits);
    }
}
Also used : Searcher(org.elasticsearch.index.engine.Engine.Searcher) TopDocs(org.apache.lucene.search.TopDocs) BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Index(org.elasticsearch.index.Index) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 12 with Searcher

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

the class InternalEngineTests method testTranslogMultipleOperationsSameDocument.

public void testTranslogMultipleOperationsSameDocument() throws IOException {
    final int ops = randomIntBetween(1, 32);
    Engine initialEngine;
    final List<Engine.Operation> operations = new ArrayList<>();
    try {
        initialEngine = engine;
        for (int i = 0; i < ops; i++) {
            final ParsedDocument doc = testParsedDocument("1", "test", null, testDocumentWithTextField(), SOURCE, null);
            if (randomBoolean()) {
                final Engine.Index operation = new Engine.Index(newUid(doc), doc, SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, i, VersionType.EXTERNAL, Engine.Operation.Origin.PRIMARY, System.nanoTime(), -1, false);
                operations.add(operation);
                initialEngine.index(operation);
            } else {
                final Engine.Delete operation = new Engine.Delete("test", "1", newUid(doc), SequenceNumbersService.UNASSIGNED_SEQ_NO, 0, i, VersionType.EXTERNAL, Engine.Operation.Origin.PRIMARY, System.nanoTime());
                operations.add(operation);
                initialEngine.delete(operation);
            }
        }
    } finally {
        IOUtils.close(engine);
    }
    Engine recoveringEngine = null;
    try {
        recoveringEngine = new InternalEngine(copy(engine.config(), EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG));
        recoveringEngine.recoverFromTranslog();
        try (Engine.Searcher searcher = recoveringEngine.acquireSearcher("test")) {
            final TotalHitCountCollector collector = new TotalHitCountCollector();
            searcher.searcher().search(new MatchAllDocsQuery(), collector);
            assertThat(collector.getTotalHits(), equalTo(operations.get(operations.size() - 1) instanceof Engine.Delete ? 0 : 1));
        }
    } finally {
        IOUtils.close(recoveringEngine);
    }
}
Also used : Searcher(org.elasticsearch.index.engine.Engine.Searcher) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LongPoint(org.apache.lucene.document.LongPoint) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector)

Example 13 with Searcher

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

the class InternalEngineTests method testSkipTranslogReplay.

public void testSkipTranslogReplay() 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();
    engine = new InternalEngine(engine.config());
    try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), randomIntBetween(numDocs, numDocs + 10));
        assertThat(topDocs.totalHits, equalTo(0));
    }
}
Also used : Searcher(org.elasticsearch.index.engine.Engine.Searcher) TopDocs(org.apache.lucene.search.TopDocs) BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) Index(org.elasticsearch.index.Index) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) LongPoint(org.apache.lucene.document.LongPoint)

Example 14 with Searcher

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

the class InternalEngineTests method testDoubleDelivery.

public void testDoubleDelivery() throws IOException {
    final ParsedDocument doc = testParsedDocument("1", "test", null, testDocumentWithTextField(), new BytesArray("{}".getBytes(Charset.defaultCharset())), null);
    Engine.Index operation = randomAppendOnly(doc, false, 1);
    Engine.Index retry = randomAppendOnly(doc, true, 1);
    if (randomBoolean()) {
        Engine.IndexResult indexResult = engine.index(operation);
        assertFalse(engine.indexWriterHasDeletions());
        assertEquals(0, engine.getNumVersionLookups());
        assertNotNull(indexResult.getTranslogLocation());
        Engine.IndexResult retryResult = engine.index(retry);
        assertTrue(engine.indexWriterHasDeletions());
        assertEquals(0, engine.getNumVersionLookups());
        assertNotNull(retryResult.getTranslogLocation());
        assertTrue(retryResult.getTranslogLocation().compareTo(indexResult.getTranslogLocation()) > 0);
    } else {
        Engine.IndexResult retryResult = engine.index(retry);
        assertTrue(engine.indexWriterHasDeletions());
        assertEquals(0, engine.getNumVersionLookups());
        assertNotNull(retryResult.getTranslogLocation());
        Engine.IndexResult indexResult = engine.index(operation);
        assertTrue(engine.indexWriterHasDeletions());
        assertEquals(0, engine.getNumVersionLookups());
        assertNotNull(retryResult.getTranslogLocation());
        assertTrue(retryResult.getTranslogLocation().compareTo(indexResult.getTranslogLocation()) < 0);
    }
    engine.refresh("test");
    try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), 10);
        assertEquals(1, topDocs.totalHits);
    }
    operation = randomAppendOnly(doc, false, 1);
    retry = randomAppendOnly(doc, true, 1);
    if (randomBoolean()) {
        Engine.IndexResult indexResult = engine.index(operation);
        assertNotNull(indexResult.getTranslogLocation());
        Engine.IndexResult retryResult = engine.index(retry);
        assertNotNull(retryResult.getTranslogLocation());
        assertTrue(retryResult.getTranslogLocation().compareTo(indexResult.getTranslogLocation()) > 0);
    } else {
        Engine.IndexResult retryResult = engine.index(retry);
        assertNotNull(retryResult.getTranslogLocation());
        Engine.IndexResult indexResult = engine.index(operation);
        assertNotNull(retryResult.getTranslogLocation());
        assertTrue(retryResult.getTranslogLocation().compareTo(indexResult.getTranslogLocation()) < 0);
    }
    engine.refresh("test");
    try (Engine.Searcher searcher = engine.acquireSearcher("test")) {
        TopDocs topDocs = searcher.searcher().search(new MatchAllDocsQuery(), 10);
        assertEquals(1, topDocs.totalHits);
    }
}
Also used : Searcher(org.elasticsearch.index.engine.Engine.Searcher) TopDocs(org.apache.lucene.search.TopDocs) BytesArray(org.elasticsearch.common.bytes.BytesArray) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 15 with Searcher

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

the class InternalEngineTests method testIndexSearcherWrapper.

public void testIndexSearcherWrapper() throws Exception {
    final AtomicInteger counter = new AtomicInteger();
    IndexSearcherWrapper wrapper = new IndexSearcherWrapper() {

        @Override
        public DirectoryReader wrap(DirectoryReader reader) {
            counter.incrementAndGet();
            return reader;
        }

        @Override
        public IndexSearcher wrap(IndexSearcher searcher) throws EngineException {
            counter.incrementAndGet();
            return searcher;
        }
    };
    Store store = createStore();
    Path translog = createTempDir("translog-test");
    InternalEngine engine = createEngine(store, translog);
    engine.close();
    engine = new InternalEngine(copy(engine.config(), EngineConfig.OpenMode.OPEN_INDEX_AND_TRANSLOG));
    assertTrue(engine.isRecovering());
    engine.recoverFromTranslog();
    Engine.Searcher searcher = wrapper.wrap(engine.acquireSearcher("test"));
    assertThat(counter.get(), equalTo(2));
    searcher.close();
    IOUtils.close(store, engine);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Path(java.nio.file.Path) ContentPath(org.elasticsearch.index.mapper.ContentPath) Searcher(org.elasticsearch.index.engine.Engine.Searcher) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DirectoryReader(org.apache.lucene.index.DirectoryReader) Store(org.elasticsearch.index.store.Store) IndexSearcherWrapper(org.elasticsearch.index.shard.IndexSearcherWrapper)

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