Search in sources :

Example 31 with TotalHitCountCollector

use of org.apache.lucene.search.TotalHitCountCollector in project crate by crate.

the class InternalEngineTests method testConcurrentExternalVersioningOnPrimary.

@Test
public void testConcurrentExternalVersioningOnPrimary() throws IOException, InterruptedException {
    final List<Engine.Operation> ops = generateSingleDocHistory(false, VersionType.EXTERNAL, false, 2, 100, 300, "1");
    final Engine.Operation lastOp = ops.get(ops.size() - 1);
    final String lastFieldValue;
    if (lastOp instanceof Engine.Index) {
        Engine.Index index = (Engine.Index) lastOp;
        lastFieldValue = index.docs().get(0).get("value");
    } else {
        // delete
        lastFieldValue = null;
    }
    shuffle(ops, random());
    concurrentlyApplyOps(ops, engine);
    assertVisibleCount(engine, lastFieldValue == null ? 0 : 1);
    if (lastFieldValue != null) {
        try (Searcher searcher = engine.acquireSearcher("test")) {
            final TotalHitCountCollector collector = new TotalHitCountCollector();
            searcher.search(new TermQuery(new Term("value", lastFieldValue)), collector);
            assertThat(collector.getTotalHits(), equalTo(1));
        }
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Searcher(org.elasticsearch.index.engine.Engine.Searcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) Test(org.junit.Test)

Example 32 with TotalHitCountCollector

use of org.apache.lucene.search.TotalHitCountCollector in project crate by crate.

the class InternalEngineTests method testNonInternalVersioningOnPrimary.

@Test
public void testNonInternalVersioningOnPrimary() throws IOException {
    final Set<VersionType> nonInternalVersioning = new HashSet<>(Arrays.asList(VersionType.values()));
    nonInternalVersioning.remove(VersionType.INTERNAL);
    final VersionType versionType = randomFrom(nonInternalVersioning);
    final List<Engine.Operation> ops = generateSingleDocHistory(false, versionType, false, 2, 2, 20, "1");
    final Engine.Operation lastOp = ops.get(ops.size() - 1);
    final String lastFieldValue;
    if (lastOp instanceof Engine.Index) {
        Engine.Index index = (Engine.Index) lastOp;
        lastFieldValue = index.docs().get(0).get("value");
    } else {
        // delete
        lastFieldValue = null;
    }
    // other version types don't support out of order processing.
    if (versionType == VersionType.EXTERNAL) {
        shuffle(ops, random());
    }
    long highestOpVersion = Versions.NOT_FOUND;
    long seqNo = -1;
    boolean docDeleted = true;
    for (Engine.Operation op : ops) {
        logger.info("performing [{}], v [{}], seq# [{}], term [{}]", op.operationType().name().charAt(0), op.version(), op.seqNo(), op.primaryTerm());
        if (op instanceof Engine.Index) {
            final Engine.Index index = (Engine.Index) op;
            Engine.IndexResult result = engine.index(index);
            if (op.versionType().isVersionConflictForWrites(highestOpVersion, op.version(), docDeleted) == false) {
                seqNo++;
                assertThat(result.getSeqNo(), equalTo(seqNo));
                assertThat(result.isCreated(), equalTo(docDeleted));
                assertThat(result.getVersion(), equalTo(op.version()));
                assertThat(result.getResultType(), equalTo(Engine.Result.Type.SUCCESS));
                assertThat(result.getFailure(), nullValue());
                docDeleted = false;
                highestOpVersion = op.version();
            } else {
                assertThat(result.isCreated(), equalTo(false));
                assertThat(result.getVersion(), equalTo(highestOpVersion));
                assertThat(result.getResultType(), equalTo(Engine.Result.Type.FAILURE));
                assertThat(result.getFailure(), instanceOf(VersionConflictEngineException.class));
            }
        } else {
            final Engine.Delete delete = (Engine.Delete) op;
            Engine.DeleteResult result = engine.delete(delete);
            if (op.versionType().isVersionConflictForWrites(highestOpVersion, op.version(), docDeleted) == false) {
                seqNo++;
                assertThat(result.getSeqNo(), equalTo(seqNo));
                assertThat(result.isFound(), equalTo(docDeleted == false));
                assertThat(result.getVersion(), equalTo(op.version()));
                assertThat(result.getResultType(), equalTo(Engine.Result.Type.SUCCESS));
                assertThat(result.getFailure(), nullValue());
                docDeleted = true;
                highestOpVersion = op.version();
            } else {
                assertThat(result.isFound(), equalTo(docDeleted == false));
                assertThat(result.getVersion(), equalTo(highestOpVersion));
                assertThat(result.getResultType(), equalTo(Engine.Result.Type.FAILURE));
                assertThat(result.getFailure(), instanceOf(VersionConflictEngineException.class));
            }
        }
        if (randomBoolean()) {
            engine.refresh("test");
        }
        if (randomBoolean()) {
            engine.flush();
            engine.refresh("test");
        }
    }
    assertVisibleCount(engine, docDeleted ? 0 : 1);
    if (docDeleted == false) {
        logger.info("searching for [{}]", lastFieldValue);
        try (Searcher searcher = engine.acquireSearcher("test")) {
            final TotalHitCountCollector collector = new TotalHitCountCollector();
            searcher.search(new TermQuery(new Term("value", lastFieldValue)), collector);
            assertThat(collector.getTotalHits(), equalTo(1));
        }
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Searcher(org.elasticsearch.index.engine.Engine.Searcher) IndexSearcher(org.apache.lucene.search.IndexSearcher) Matchers.containsString(org.hamcrest.Matchers.containsString) Term(org.apache.lucene.index.Term) VersionType(org.elasticsearch.index.VersionType) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TotalHitCountCollector (org.apache.lucene.search.TotalHitCountCollector)32 TermQuery (org.apache.lucene.search.TermQuery)17 Term (org.apache.lucene.index.Term)13 BooleanQuery (org.apache.lucene.search.BooleanQuery)12 IOException (java.io.IOException)10 IndexSearcher (org.apache.lucene.search.IndexSearcher)10 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)10 Query (org.apache.lucene.search.Query)10 ArrayList (java.util.ArrayList)7 Searcher (org.elasticsearch.index.engine.Engine.Searcher)7 LongPoint (org.apache.lucene.document.LongPoint)6 Test (org.junit.Test)6 TopDocs (org.apache.lucene.search.TopDocs)5 HashSet (java.util.HashSet)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 Document (org.apache.lucene.document.Document)4 IndexReader (org.apache.lucene.index.IndexReader)4 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)4 BooleanClause (org.apache.lucene.search.BooleanClause)4 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)4