Fix IncludeExclude parsing

`include` / `exclude` in terms / sig-terms aggs seems completely broken
and massively untested. This commit makes the TermsTests pass again that
randomly use `include` / `exclude`. This class must be tested individually
and we need real integ tests that use xcontent that use this feature.
This commit is contained in:
Simon Willnauer 2016-12-11 09:55:53 +01:00
parent e6b10ca4db
commit 20ff703e07

View File

@ -103,6 +103,9 @@ public class IncludeExclude implements Writeable, ToXContent {
String currentFieldName = null; String currentFieldName = null;
Integer partition = null, numPartitions = null; Integer partition = null, numPartitions = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else
// This "include":{"pattern":"foo.*"} syntax is undocumented since 2.0 // This "include":{"pattern":"foo.*"} syntax is undocumented since 2.0
// Regexes should be "include":"foo.*" // Regexes should be "include":"foo.*"
if (parseFieldMatcher.match(currentFieldName, PATTERN_FIELD)) { if (parseFieldMatcher.match(currentFieldName, PATTERN_FIELD)) {
@ -150,8 +153,6 @@ public class IncludeExclude implements Writeable, ToXContent {
} }
public class PartitionedLongFilter extends LongFilter { public class PartitionedLongFilter extends LongFilter {
private final ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
@Override @Override
public boolean accept(long value) { public boolean accept(long value) {
// hash the value to keep even distributions // hash the value to keep even distributions
@ -682,22 +683,21 @@ public class IncludeExclude implements Writeable, ToXContent {
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
if (include != null) { if (include != null) {
builder.field(INCLUDE_FIELD.getPreferredName(), include.getOriginalString()); builder.field(INCLUDE_FIELD.getPreferredName(), include.getOriginalString());
} } else if (includeValues != null) {
if (includeValues != null) {
builder.startArray(INCLUDE_FIELD.getPreferredName()); builder.startArray(INCLUDE_FIELD.getPreferredName());
for (BytesRef value : includeValues) { for (BytesRef value : includeValues) {
builder.value(value.utf8ToString()); builder.value(value.utf8ToString());
} }
builder.endArray(); builder.endArray();
} } else if (isPartitionBased()) {
if (isPartitionBased()) { builder.startObject(INCLUDE_FIELD.getPreferredName());
builder.field(PARTITION_FIELD.getPreferredName(), incZeroBasedPartition); builder.field(PARTITION_FIELD.getPreferredName(), incZeroBasedPartition);
builder.field(NUM_PARTITIONS_FIELD.getPreferredName(), incNumPartitions); builder.field(NUM_PARTITIONS_FIELD.getPreferredName(), incNumPartitions);
builder.endObject();
} }
if (exclude != null) { if (exclude != null) {
builder.field(EXCLUDE_FIELD.getPreferredName(), exclude.getOriginalString()); builder.field(EXCLUDE_FIELD.getPreferredName(), exclude.getOriginalString());
} } else if (excludeValues != null) {
if (excludeValues != null) {
builder.startArray(EXCLUDE_FIELD.getPreferredName()); builder.startArray(EXCLUDE_FIELD.getPreferredName());
for (BytesRef value : excludeValues) { for (BytesRef value : excludeValues) {
builder.value(value.utf8ToString()); builder.value(value.utf8ToString());