use of org.apache.lucene.search.DocIdSet in project lucene-solr by apache.
the class Filter method createWeight.
//
// Query compatibility
//
@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
return new Weight(this) {
@Override
public void extractTerms(Set<Term> terms) {
}
@Override
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
final Scorer scorer = scorer(context);
final boolean match = (scorer != null && scorer.iterator().advance(doc) == doc);
if (match) {
assert scorer.score() == 0f;
return Explanation.match(0f, "Match on id " + doc);
} else {
return Explanation.match(0f, "No match on id " + doc);
}
}
@Override
public Scorer scorer(LeafReaderContext context) throws IOException {
final DocIdSet set = getDocIdSet(context, null);
if (set == null) {
return null;
}
if (applyLazily && set.bits() != null) {
final Bits bits = set.bits();
final DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc());
final TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {
@Override
public boolean matches() throws IOException {
return bits.get(approximation.docID());
}
@Override
public float matchCost() {
// TODO use cost of bits.get()
return 10;
}
};
return new ConstantScoreScorer(this, 0f, twoPhase);
}
final DocIdSetIterator iterator = set.iterator();
if (iterator == null) {
return null;
}
return new ConstantScoreScorer(this, 0f, iterator);
}
};
}
use of org.apache.lucene.search.DocIdSet in project lucene-solr by apache.
the class QueryWrapperFilter method getDocIdSet.
@Override
public DocIdSet getDocIdSet(final LeafReaderContext context, final Bits acceptDocs) throws IOException {
// get a private context that is used to rewrite, createWeight and score eventually
final LeafReaderContext privateContext = context.reader().getContext();
final Weight weight = new IndexSearcher(privateContext).createNormalizedWeight(query, false);
DocIdSet set = new DocIdSet() {
@Override
public DocIdSetIterator iterator() throws IOException {
Scorer scorer = weight.scorer(privateContext);
return scorer == null ? null : scorer.iterator();
}
@Override
public long ramBytesUsed() {
return 0L;
}
};
return BitsFilteredDocIdSet.wrap(set, acceptDocs);
}
use of org.apache.lucene.search.DocIdSet in project lucene-solr by apache.
the class TestDocIdSetBuilder method testDense.
public void testDense() throws IOException {
final int maxDoc = 1000000 + random().nextInt(1000000);
DocIdSetBuilder builder = new DocIdSetBuilder(maxDoc);
final int numIterators = 1 + random().nextInt(10);
final FixedBitSet ref = new FixedBitSet(maxDoc);
for (int i = 0; i < numIterators; ++i) {
RoaringDocIdSet.Builder b = new RoaringDocIdSet.Builder(maxDoc);
for (int doc = random().nextInt(1000); doc < maxDoc; doc += 1 + random().nextInt(100)) {
b.add(doc);
ref.set(doc);
}
builder.add(b.build().iterator());
}
DocIdSet result = builder.build();
assertTrue(result instanceof BitDocIdSet);
assertEquals(new BitDocIdSet(ref), result);
}
use of org.apache.lucene.search.DocIdSet in project lucene-solr by apache.
the class TestDocIdSetBuilder method testLeverageStats.
public void testLeverageStats() throws IOException {
// single-valued points
PointValues values = new DummyPointValues(42, 42);
DocIdSetBuilder builder = new DocIdSetBuilder(100, values, "foo");
assertEquals(1d, builder.numValuesPerDoc, 0d);
assertFalse(builder.multivalued);
DocIdSetBuilder.BulkAdder adder = builder.grow(2);
adder.add(5);
adder.add(7);
DocIdSet set = builder.build();
assertTrue(set instanceof BitDocIdSet);
assertEquals(2, set.iterator().cost());
// multi-valued points
values = new DummyPointValues(42, 63);
builder = new DocIdSetBuilder(100, values, "foo");
assertEquals(1.5, builder.numValuesPerDoc, 0d);
assertTrue(builder.multivalued);
adder = builder.grow(2);
adder.add(5);
adder.add(7);
set = builder.build();
assertTrue(set instanceof BitDocIdSet);
// it thinks the same doc was added twice
assertEquals(1, set.iterator().cost());
// incomplete stats
values = new DummyPointValues(42, -1);
builder = new DocIdSetBuilder(100, values, "foo");
assertEquals(1d, builder.numValuesPerDoc, 0d);
assertTrue(builder.multivalued);
values = new DummyPointValues(-1, 84);
builder = new DocIdSetBuilder(100, values, "foo");
assertEquals(1d, builder.numValuesPerDoc, 0d);
assertTrue(builder.multivalued);
// single-valued terms
Terms terms = new DummyTerms(42, 42);
builder = new DocIdSetBuilder(100, terms);
assertEquals(1d, builder.numValuesPerDoc, 0d);
assertFalse(builder.multivalued);
adder = builder.grow(2);
adder.add(5);
adder.add(7);
set = builder.build();
assertTrue(set instanceof BitDocIdSet);
assertEquals(2, set.iterator().cost());
// multi-valued terms
terms = new DummyTerms(42, 63);
builder = new DocIdSetBuilder(100, terms);
assertEquals(1.5, builder.numValuesPerDoc, 0d);
assertTrue(builder.multivalued);
adder = builder.grow(2);
adder.add(5);
adder.add(7);
set = builder.build();
assertTrue(set instanceof BitDocIdSet);
// it thinks the same doc was added twice
assertEquals(1, set.iterator().cost());
// incomplete stats
terms = new DummyTerms(42, -1);
builder = new DocIdSetBuilder(100, terms);
assertEquals(1d, builder.numValuesPerDoc, 0d);
assertTrue(builder.multivalued);
terms = new DummyTerms(-1, 84);
builder = new DocIdSetBuilder(100, terms);
assertEquals(1d, builder.numValuesPerDoc, 0d);
assertTrue(builder.multivalued);
}
use of org.apache.lucene.search.DocIdSet in project greplin-lucene-utils by Cue.
the class BaseFilterTest method assertFilterBitsEqual.
protected static void assertFilterBitsEqual(IndexReader reader, Filter filter, boolean... expected) throws IOException {
DocIdSet actual = filter.getDocIdSet(reader);
FixedBitSet actualBitSet = new FixedBitSet(expected.length);
actualBitSet.or(actual.iterator());
for (int i = 0; i < expected.length; i++) {
Assert.assertEquals("Expected same value at position " + i, expected[i], actualBitSet.get(i));
}
}
Aggregations