Search in sources :

Example 76 with IndexReader

use of org.apache.lucene.index.IndexReader in project lucene-solr by apache.

the class TestSpanMultiTermQueryWrapper method testWrappedQueryIsNotModified.

@Test
public void testWrappedQueryIsNotModified() {
    final PrefixQuery pq = new PrefixQuery(new Term("field", "test"));
    int pqHash = pq.hashCode();
    SpanMultiTermQueryWrapper<PrefixQuery> wrapper = new SpanMultiTermQueryWrapper<>(pq);
    assertEquals(pqHash, pq.hashCode());
    wrapper.setRewriteMethod(new SpanMultiTermQueryWrapper.SpanRewriteMethod() {

        @Override
        public SpanQuery rewrite(IndexReader reader, MultiTermQuery query) throws IOException {
            return null;
        }
    });
    assertEquals(pqHash, pq.hashCode());
}
Also used : MultiTermQuery(org.apache.lucene.search.MultiTermQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) IOException(java.io.IOException) Test(org.junit.Test)

Example 77 with IndexReader

use of org.apache.lucene.index.IndexReader in project lucene-solr by apache.

the class TestSpanNearQuery method testNoPositions.

public void testNoPositions() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new StringField("foo", "bar", Field.Store.NO));
    iw.addDocument(doc);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher is = new IndexSearcher(ir);
    SpanTermQuery query = new SpanTermQuery(new Term("foo", "bar"));
    SpanTermQuery query2 = new SpanTermQuery(new Term("foo", "baz"));
    IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
        is.search(new SpanNearQuery(new SpanQuery[] { query, query2 }, 10, true), 5);
    });
    assertTrue(expected.getMessage().contains("was indexed without position data"));
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 78 with IndexReader

use of org.apache.lucene.index.IndexReader in project lucene-solr by apache.

the class TestSpanNotQuery method testNoPositions.

public void testNoPositions() throws IOException {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    doc.add(new StringField("foo", "bar", Field.Store.NO));
    iw.addDocument(doc);
    IndexReader ir = iw.getReader();
    iw.close();
    IndexSearcher is = new IndexSearcher(ir);
    SpanTermQuery query = new SpanTermQuery(new Term("foo", "bar"));
    SpanTermQuery query2 = new SpanTermQuery(new Term("foo", "baz"));
    IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
        is.search(new SpanNotQuery(query, query2), 5);
    });
    assertTrue(expected.getMessage().contains("was indexed without position data"));
    ir.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 79 with IndexReader

use of org.apache.lucene.index.IndexReader in project lucene-solr by apache.

the class RandomSamplingFacetsCollector method amortizeFacetCounts.

/**
   * Note: if you use a counting {@link Facets} implementation, you can amortize the
   * sampled counts by calling this method. Uses the {@link FacetsConfig} and
   * the {@link IndexSearcher} to determine the upper bound for each facet value.
   */
public FacetResult amortizeFacetCounts(FacetResult res, FacetsConfig config, IndexSearcher searcher) throws IOException {
    if (res == null || totalHits <= sampleSize) {
        return res;
    }
    LabelAndValue[] fixedLabelValues = new LabelAndValue[res.labelValues.length];
    IndexReader reader = searcher.getIndexReader();
    DimConfig dimConfig = config.getDimConfig(res.dim);
    // +2 to prepend dimension, append child label
    String[] childPath = new String[res.path.length + 2];
    childPath[0] = res.dim;
    // reuse
    System.arraycopy(res.path, 0, childPath, 1, res.path.length);
    for (int i = 0; i < res.labelValues.length; i++) {
        childPath[res.path.length + 1] = res.labelValues[i].label;
        String fullPath = FacetsConfig.pathToString(childPath, childPath.length);
        int max = reader.docFreq(new Term(dimConfig.indexFieldName, fullPath));
        int correctedCount = (int) (res.labelValues[i].value.doubleValue() / samplingRate);
        correctedCount = Math.min(max, correctedCount);
        fixedLabelValues[i] = new LabelAndValue(res.labelValues[i].label, correctedCount);
    }
    // cap the total count on the total number of non-deleted documents in the reader
    int correctedTotalCount = res.value.intValue();
    if (correctedTotalCount > 0) {
        correctedTotalCount = Math.min(reader.numDocs(), (int) (res.value.doubleValue() / samplingRate));
    }
    return new FacetResult(res.dim, res.path, correctedTotalCount, fixedLabelValues, res.childCount);
}
Also used : IndexReader(org.apache.lucene.index.IndexReader) Term(org.apache.lucene.index.Term) DimConfig(org.apache.lucene.facet.FacetsConfig.DimConfig)

Example 80 with IndexReader

use of org.apache.lucene.index.IndexReader in project lucene-solr by apache.

the class ConcurrentSortedSetDocValuesFacetCounts method count.

/** Does all the "real work" of tallying up the counts. */
private final void count(List<MatchingDocs> matchingDocs) throws IOException, InterruptedException {
    MultiDocValues.OrdinalMap ordinalMap;
    // matchingDocs...
    if (dv instanceof MultiDocValues.MultiSortedSetDocValues && matchingDocs.size() > 1) {
        ordinalMap = ((MultiSortedSetDocValues) dv).mapping;
    } else {
        ordinalMap = null;
    }
    IndexReader reader = state.getReader();
    List<Future<Void>> results = new ArrayList<>();
    for (MatchingDocs hits : matchingDocs) {
        // AIOOBE can happen:
        if (ReaderUtil.getTopLevelContext(hits.context).reader() != reader) {
            throw new IllegalStateException("the SortedSetDocValuesReaderState provided to this class does not match the reader being searched; you must create a new SortedSetDocValuesReaderState every time you open a new IndexReader");
        }
        results.add(exec.submit(new CountOneSegment(hits.context.reader(), hits, ordinalMap, hits.context.ord)));
    }
    for (Future<Void> result : results) {
        try {
            result.get();
        } catch (ExecutionException ee) {
            // Theoretically cause can be null; guard against that.
            Throwable cause = ee.getCause();
            throw IOUtils.rethrowAlways(cause != null ? cause : ee);
        }
    }
}
Also used : MatchingDocs(org.apache.lucene.facet.FacetsCollector.MatchingDocs) ArrayList(java.util.ArrayList) MultiDocValues(org.apache.lucene.index.MultiDocValues) IndexReader(org.apache.lucene.index.IndexReader) Future(java.util.concurrent.Future) MultiSortedSetDocValues(org.apache.lucene.index.MultiDocValues.MultiSortedSetDocValues) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

IndexReader (org.apache.lucene.index.IndexReader)962 Document (org.apache.lucene.document.Document)610 Directory (org.apache.lucene.store.Directory)603 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)549 IndexSearcher (org.apache.lucene.search.IndexSearcher)410 Term (org.apache.lucene.index.Term)332 TopDocs (org.apache.lucene.search.TopDocs)204 TermQuery (org.apache.lucene.search.TermQuery)160 Query (org.apache.lucene.search.Query)158 IndexWriter (org.apache.lucene.index.IndexWriter)150 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)144 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)143 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)142 Field (org.apache.lucene.document.Field)135 BytesRef (org.apache.lucene.util.BytesRef)134 IOException (java.io.IOException)133 BooleanQuery (org.apache.lucene.search.BooleanQuery)122 ArrayList (java.util.ArrayList)108 TextField (org.apache.lucene.document.TextField)81 Test (org.junit.Test)81