Fielddata: Fix the ordinals impl for sparse fields.

Caused by #6908
This commit is contained in:
Adrien Grand 2014-07-23 17:38:13 +02:00
parent 10201d511c
commit 76511158b5
4 changed files with 16 additions and 5 deletions

View File

@ -75,6 +75,8 @@ public class MultiOrdinals extends Ordinals {
}
lastEndOffset = endOffset;
}
endOffsets.freeze();
ords.freeze();
assert endOffsets.size() == builder.maxDoc();
assert ords.size() == builder.getTotalNumOrds() : ords.size() + " != " + builder.getTotalNumOrds();
}
@ -109,8 +111,9 @@ public class MultiOrdinals extends Ordinals {
@Override
public int getOrd(int docId) {
final long offset = docId != 0 ? endOffsets.get(docId - 1) : 0;
return (int) ords.get(offset);
final long startOffset = docId != 0 ? endOffsets.get(docId - 1) : 0;
final long endOffset = endOffsets.get(docId);
return startOffset == endOffset ? -1 : (int) ords.get(startOffset);
}
@Override

View File

@ -40,6 +40,11 @@ import java.util.Comparator;
*/
public final class OrdinalsBuilder implements Closeable {
/**
* Whether to for the use of {@link MultiOrdinals} to store the ordinals for testing purposes.
*/
public static final String FORCE_MULTI_ORDINALS = "force_multi_ordinals";
/**
* Default acceptable overhead ratio. {@link OrdinalsBuilder} memory usage is mostly transient so it is likely a better trade-off to
* trade memory for speed in order to resize less often.
@ -395,7 +400,8 @@ public final class OrdinalsBuilder implements Closeable {
*/
public Ordinals build(Settings settings) {
final float acceptableOverheadRatio = settings.getAsFloat("acceptable_overhead_ratio", PackedInts.FASTEST);
if (numMultiValuedDocs > 0 || MultiOrdinals.significantlySmallerThanSinglePackedOrdinals(maxDoc, numDocsWithValue, getValueCount(), acceptableOverheadRatio)) {
final boolean forceMultiOrdinals = settings.getAsBoolean(FORCE_MULTI_ORDINALS, false);
if (forceMultiOrdinals || numMultiValuedDocs > 0 || MultiOrdinals.significantlySmallerThanSinglePackedOrdinals(maxDoc, numDocsWithValue, getValueCount(), acceptableOverheadRatio)) {
// MultiOrdinals can be smaller than SinglePackedOrdinals for sparse fields
return new MultiOrdinals(this, acceptableOverheadRatio);
} else {

View File

@ -20,6 +20,7 @@
package org.elasticsearch.index.fielddata;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder;
/**
*/
@ -27,6 +28,6 @@ public class FSTPackedBytesStringFieldDataTests extends AbstractStringFieldDataT
@Override
protected FieldDataType getFieldDataType() {
return new FieldDataType("string", ImmutableSettings.builder().put("format", "fst"));
return new FieldDataType("string", ImmutableSettings.builder().put("format", "fst").put(OrdinalsBuilder.FORCE_MULTI_ORDINALS, randomBoolean()));
}
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.index.fielddata;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.fielddata.ordinals.OrdinalsBuilder;
/**
*/
@ -27,6 +28,6 @@ public class PagedBytesStringFieldDataTests extends AbstractStringFieldDataTests
@Override
protected FieldDataType getFieldDataType() {
return new FieldDataType("string", ImmutableSettings.builder().put("format", "paged_bytes"));
return new FieldDataType("string", ImmutableSettings.builder().put("format", "paged_bytes").put(OrdinalsBuilder.FORCE_MULTI_ORDINALS, randomBoolean()));
}
}