Replace Bits with new abstract class to respresent documents that have a value (#24088) (#28334)

This commit is contained in:
kel 2018-01-31 22:42:11 +08:00 committed by Adrien Grand
parent 3f5716b9b8
commit 5819e57baa
4 changed files with 77 additions and 92 deletions

View File

@ -0,0 +1,32 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.fielddata;
import java.io.IOException;
public abstract class DocValueBits {
/**
* Advance this instance to the given document id
*
* @return true if there is a value for this document
*/
public abstract boolean advanceExact(int doc) throws IOException;
}

View File

@ -24,7 +24,6 @@ import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.NumericDocValues; import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SortedNumericDocValues; import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoPoint;
@ -92,109 +91,64 @@ public enum FieldData {
} }
/** /**
* Returns a {@link Bits} representing all documents from <code>dv</code> that have a value. * Returns a {@link DocValueBits} representing all documents from <code>values</code> that have a value.
*/ */
public static Bits docsWithValue(final SortedBinaryDocValues dv, final int maxDoc) { public static DocValueBits docsWithValue(final SortedBinaryDocValues values) {
return new Bits() { return new DocValueBits() {
@Override @Override
public boolean get(int index) { public boolean advanceExact(int doc) throws IOException {
try { return values.advanceExact(doc);
return dv.advanceExact(index);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public int length() {
return maxDoc;
} }
}; };
} }
/** /**
* Returns a {@link Bits} representing all documents from <code>dv</code> * Returns a {@link DocValueBits} representing all documents from <code>docValues</code>
* that have a value. * that have a value.
*/ */
public static Bits docsWithValue(final SortedSetDocValues dv, final int maxDoc) { public static DocValueBits docsWithValue(final SortedSetDocValues docValues) {
return new Bits() { return new DocValueBits() {
@Override @Override
public boolean get(int index) { public boolean advanceExact(int doc) throws IOException {
try { return docValues.advanceExact(doc);
return dv.advanceExact(index);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public int length() {
return maxDoc;
} }
}; };
} }
/** /**
* Returns a Bits representing all documents from <code>dv</code> that have * Returns a {@link DocValueBits} representing all documents from <code>pointValues</code> that have
* a value. * a value.
*/ */
public static Bits docsWithValue(final MultiGeoPointValues dv, final int maxDoc) { public static DocValueBits docsWithValue(final MultiGeoPointValues pointValues) {
return new Bits() { return new DocValueBits() {
@Override @Override
public boolean get(int index) { public boolean advanceExact(int doc) throws IOException {
try { return pointValues.advanceExact(doc);
return dv.advanceExact(index);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public int length() {
return maxDoc;
} }
}; };
} }
/** /**
* Returns a Bits representing all documents from <code>dv</code> that have a value. * Returns a {@link DocValueBits} representing all documents from <code>doubleValues</code> that have a value.
*/ */
public static Bits docsWithValue(final SortedNumericDoubleValues dv, final int maxDoc) { public static DocValueBits docsWithValue(final SortedNumericDoubleValues doubleValues) {
return new Bits() { return new DocValueBits() {
@Override @Override
public boolean get(int index) { public boolean advanceExact(int doc) throws IOException {
try { return doubleValues.advanceExact(doc);
return dv.advanceExact(index);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public int length() {
return maxDoc;
} }
}; };
} }
/** /**
* Returns a Bits representing all documents from <code>dv</code> that have * Returns a {@link DocValueBits} representing all documents from <code>docValues</code> that have
* a value. * a value.
*/ */
public static Bits docsWithValue(final SortedNumericDocValues dv, final int maxDoc) { public static DocValueBits docsWithValue(final SortedNumericDocValues docValues) {
return new Bits() { return new DocValueBits() {
@Override @Override
public boolean get(int index) { public boolean advanceExact(int doc) throws IOException {
try { return docValues.advanceExact(doc);
return dv.advanceExact(index);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public int length() {
return maxDoc;
} }
}; };
} }

View File

@ -19,7 +19,7 @@
package org.elasticsearch.search.aggregations.bucket.missing; package org.elasticsearch.search.aggregations.bucket.missing;
import org.apache.lucene.index.LeafReaderContext; import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.util.Bits; import org.elasticsearch.index.fielddata.DocValueBits;
import org.elasticsearch.search.aggregations.Aggregator; import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories; import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregation;
@ -49,17 +49,21 @@ public class MissingAggregator extends BucketsAggregator implements SingleBucket
@Override @Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, public LeafBucketCollector getLeafCollector(LeafReaderContext ctx,
final LeafBucketCollector sub) throws IOException { final LeafBucketCollector sub) throws IOException {
final DocValueBits docsWithValue;
final Bits docsWithValue;
if (valuesSource != null) { if (valuesSource != null) {
docsWithValue = valuesSource.docsWithValue(ctx); docsWithValue = valuesSource.docsWithValue(ctx);
} else { } else {
docsWithValue = new Bits.MatchNoBits(ctx.reader().maxDoc()); docsWithValue = new DocValueBits() {
@Override
public boolean advanceExact(int doc) throws IOException {
return false;
}
};
} }
return new LeafBucketCollectorBase(sub, docsWithValue) { return new LeafBucketCollectorBase(sub, docsWithValue) {
@Override @Override
public void collect(int doc, long bucket) throws IOException { public void collect(int doc, long bucket) throws IOException {
if (docsWithValue != null && !docsWithValue.get(doc)) { if (docsWithValue.advanceExact(doc) == false) {
collectBucket(sub, doc, bucket); collectBucket(sub, doc, bucket);
} }
} }

View File

@ -27,12 +27,12 @@ import org.apache.lucene.index.SortedNumericDocValues;
import org.apache.lucene.index.SortedSetDocValues; import org.apache.lucene.index.SortedSetDocValues;
import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Scorer; import org.apache.lucene.search.Scorer;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.BytesRef;
import org.elasticsearch.common.lucene.ScorerAware; import org.elasticsearch.common.lucene.ScorerAware;
import org.elasticsearch.common.util.CollectionUtils; import org.elasticsearch.common.util.CollectionUtils;
import org.elasticsearch.index.fielddata.AbstractSortingNumericDocValues; import org.elasticsearch.index.fielddata.AbstractSortingNumericDocValues;
import org.elasticsearch.index.fielddata.AtomicOrdinalsFieldData; import org.elasticsearch.index.fielddata.AtomicOrdinalsFieldData;
import org.elasticsearch.index.fielddata.DocValueBits;
import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.IndexFieldData;
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData; import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
import org.elasticsearch.index.fielddata.IndexNumericFieldData; import org.elasticsearch.index.fielddata.IndexNumericFieldData;
@ -58,7 +58,7 @@ public abstract class ValuesSource {
*/ */
public abstract SortedBinaryDocValues bytesValues(LeafReaderContext context) throws IOException; public abstract SortedBinaryDocValues bytesValues(LeafReaderContext context) throws IOException;
public abstract Bits docsWithValue(LeafReaderContext context) throws IOException; public abstract DocValueBits docsWithValue(LeafReaderContext context) throws IOException;
/** Whether this values source needs scores. */ /** Whether this values source needs scores. */
public boolean needsScores() { public boolean needsScores() {
@ -68,10 +68,9 @@ public abstract class ValuesSource {
public abstract static class Bytes extends ValuesSource { public abstract static class Bytes extends ValuesSource {
@Override @Override
public Bits docsWithValue(LeafReaderContext context) throws IOException { public DocValueBits docsWithValue(LeafReaderContext context) throws IOException {
final SortedBinaryDocValues bytes = bytesValues(context); final SortedBinaryDocValues bytes = bytesValues(context);
return org.elasticsearch.index.fielddata.FieldData.docsWithValue(bytes, return org.elasticsearch.index.fielddata.FieldData.docsWithValue(bytes);
context.reader().maxDoc());
} }
public abstract static class WithOrdinals extends Bytes { public abstract static class WithOrdinals extends Bytes {
@ -101,10 +100,9 @@ public abstract class ValuesSource {
}; };
@Override @Override
public Bits docsWithValue(LeafReaderContext context) throws IOException { public DocValueBits docsWithValue(LeafReaderContext context) throws IOException {
final SortedSetDocValues ordinals = ordinalsValues(context); final SortedSetDocValues ordinals = ordinalsValues(context);
return org.elasticsearch.index.fielddata.FieldData.docsWithValue(ordinals, return org.elasticsearch.index.fielddata.FieldData.docsWithValue(ordinals);
context.reader().maxDoc());
} }
public abstract SortedSetDocValues ordinalsValues(LeafReaderContext context) public abstract SortedSetDocValues ordinalsValues(LeafReaderContext context)
@ -241,15 +239,13 @@ public abstract class ValuesSource {
public abstract SortedNumericDoubleValues doubleValues(LeafReaderContext context) throws IOException; public abstract SortedNumericDoubleValues doubleValues(LeafReaderContext context) throws IOException;
@Override @Override
public Bits docsWithValue(LeafReaderContext context) throws IOException { public DocValueBits docsWithValue(LeafReaderContext context) throws IOException {
if (isFloatingPoint()) { if (isFloatingPoint()) {
final SortedNumericDoubleValues values = doubleValues(context); final SortedNumericDoubleValues values = doubleValues(context);
return org.elasticsearch.index.fielddata.FieldData.docsWithValue(values, return org.elasticsearch.index.fielddata.FieldData.docsWithValue(values);
context.reader().maxDoc());
} else { } else {
final SortedNumericDocValues values = longValues(context); final SortedNumericDocValues values = longValues(context);
return org.elasticsearch.index.fielddata.FieldData.docsWithValue(values, return org.elasticsearch.index.fielddata.FieldData.docsWithValue(values);
context.reader().maxDoc());
} }
} }
@ -493,10 +489,9 @@ public abstract class ValuesSource {
}; };
@Override @Override
public Bits docsWithValue(LeafReaderContext context) { public DocValueBits docsWithValue(LeafReaderContext context) throws IOException {
final MultiGeoPointValues geoPoints = geoPointValues(context); final MultiGeoPointValues geoPoints = geoPointValues(context);
return org.elasticsearch.index.fielddata.FieldData.docsWithValue(geoPoints, return org.elasticsearch.index.fielddata.FieldData.docsWithValue(geoPoints);
context.reader().maxDoc());
} }
public abstract MultiGeoPointValues geoPointValues(LeafReaderContext context); public abstract MultiGeoPointValues geoPointValues(LeafReaderContext context);