Search in sources :

Example 1 with TotalHitCountCollector

use of org.apache.lucene.search.TotalHitCountCollector 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 2 with TotalHitCountCollector

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

the class SimpleIndexReader method countIndexedNodes.

@Override
public long countIndexedNodes(long nodeId, Object... propertyValues) {
    Query nodeIdQuery = new TermQuery(LuceneDocumentStructure.newTermForChangeOrRemove(nodeId));
    Query valueQuery = LuceneDocumentStructure.newSeekQuery(propertyValues);
    BooleanQuery.Builder nodeIdAndValueQuery = new BooleanQuery.Builder().setDisableCoord(true);
    nodeIdAndValueQuery.add(nodeIdQuery, BooleanClause.Occur.MUST);
    nodeIdAndValueQuery.add(valueQuery, BooleanClause.Occur.MUST);
    try {
        TotalHitCountCollector collector = new TotalHitCountCollector();
        getIndexSearcher().search(nodeIdAndValueQuery.build(), collector);
        // A <label,propertyKeyId,nodeId> tuple should only match at most a single propertyValue
        return collector.getTotalHits();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) IndexQuery(org.neo4j.kernel.api.schema_new.IndexQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) IOException(java.io.IOException)

Example 3 with TotalHitCountCollector

use of org.apache.lucene.search.TotalHitCountCollector in project elasticsearch by elastic.

the class SearchCancellationTests method testCancellableCollector.

public void testCancellableCollector() throws IOException {
    TotalHitCountCollector collector = new TotalHitCountCollector();
    AtomicBoolean cancelled = new AtomicBoolean();
    CancellableCollector cancellableCollector = new CancellableCollector(cancelled::get, false, collector);
    final LeafCollector leafCollector = cancellableCollector.getLeafCollector(reader.leaves().get(0));
    leafCollector.collect(0);
    cancelled.set(true);
    leafCollector.collect(1);
    expectThrows(TaskCancelledException.class, () -> cancellableCollector.getLeafCollector(reader.leaves().get(1)));
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LeafCollector(org.apache.lucene.search.LeafCollector) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) CancellableCollector(org.elasticsearch.search.query.CancellableCollector)

Example 4 with TotalHitCountCollector

use of org.apache.lucene.search.TotalHitCountCollector in project elasticsearch by elastic.

the class QueryProfilerTests method testCollector.

public void testCollector() throws IOException {
    TotalHitCountCollector collector = new TotalHitCountCollector();
    ProfileCollector profileCollector = new ProfileCollector(collector);
    assertEquals(0, profileCollector.getTime());
    final LeafCollector leafCollector = profileCollector.getLeafCollector(reader.leaves().get(0));
    assertThat(profileCollector.getTime(), greaterThan(0L));
    long time = profileCollector.getTime();
    leafCollector.setScorer(null);
    assertThat(profileCollector.getTime(), greaterThan(time));
    time = profileCollector.getTime();
    leafCollector.collect(0);
    assertThat(profileCollector.getTime(), greaterThan(time));
}
Also used : LeafCollector(org.apache.lucene.search.LeafCollector) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector)

Example 5 with TotalHitCountCollector

use of org.apache.lucene.search.TotalHitCountCollector in project lucene-solr by apache.

the class SimpleNaiveBayesDocumentClassifier method getWordFreqForClass.

/**
   * Returns the number of documents of the input class ( from the whole index or from a subset)
   * that contains the word ( in a specific field or in all the fields if no one selected)
   *
   * @param word      the token produced by the analyzer
   * @param fieldName the field the word is coming from
   * @param term      the class term
   * @return number of documents of the input class
   * @throws java.io.IOException If there is a low-level I/O error
   */
private int getWordFreqForClass(String word, String fieldName, Term term) throws IOException {
    BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
    BooleanQuery.Builder subQuery = new BooleanQuery.Builder();
    subQuery.add(new BooleanClause(new TermQuery(new Term(fieldName, word)), BooleanClause.Occur.SHOULD));
    booleanQuery.add(new BooleanClause(subQuery.build(), BooleanClause.Occur.MUST));
    booleanQuery.add(new BooleanClause(new TermQuery(term), BooleanClause.Occur.MUST));
    if (query != null) {
        booleanQuery.add(query, BooleanClause.Occur.MUST);
    }
    TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
    indexSearcher.search(booleanQuery.build(), totalHitCountCollector);
    return totalHitCountCollector.getTotalHits();
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) Term(org.apache.lucene.index.Term)

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