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());
}
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();
}
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();
}
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);
}
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);
}
}
}
Aggregations