Skip to content

IncludeExclude does not need formatter when converting longs #38739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -632,12 +632,21 @@ public LongFilter convertToLongFilter(DocValueFormat format) {
SetBackedLongFilter result = new SetBackedLongFilter(numValids, numInvalids);
if (includeValues != null) {
for (BytesRef val : includeValues) {
result.addAccept(format.parseLong(val.utf8ToString(), false, null));
if (format.getWriteableName().equalsIgnoreCase(DocValueFormat.RAW.getWriteableName())) {
result.addAccept(Long.parseLong(val.utf8ToString()));
} else {
result.addAccept(format.parseLong(val.utf8ToString(), false, null));
}

}
}
if (excludeValues != null) {
for (BytesRef val : excludeValues) {
result.addReject(format.parseLong(val.utf8ToString(), false, null));
if (format.getWriteableName().equalsIgnoreCase(DocValueFormat.RAW.getWriteableName())) {
result.addReject(Long.parseLong(val.utf8ToString()));
} else {
result.addReject(format.parseLong(val.utf8ToString(), false, null));
}
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.LongBitSet;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.time.DateFormatter;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.fielddata.AbstractSortedSetDocValues;
import org.elasticsearch.index.mapper.DateFieldMapper;
import org.elasticsearch.search.DocValueFormat;
import org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude;
import org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude.OrdinalsFilter;
import org.elasticsearch.test.ESTestCase;

import java.io.IOException;
import java.time.ZoneOffset;
import java.util.Collections;
import java.util.TreeSet;

import static org.hamcrest.Matchers.equalTo;

public class IncludeExcludeTests extends ESTestCase {
public void testEmptyTermsWithOrds() throws IOException {
IncludeExclude inexcl = new IncludeExclude(
Expand Down Expand Up @@ -122,6 +127,59 @@ public long getValueCount() {
assertFalse(acceptedOrds.get(0));
}

public void testConvertToLongFilter() {
IncludeExclude includeExclude = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef("4094779560956318341"))), null);
IncludeExclude.LongFilter filter = includeExclude.convertToLongFilter(DocValueFormat.RAW);
assertTrue(filter.accept(4094779560956318341L));

// Longs
for (int i = 0; i < 100; i++) {
long num = randomLong();
includeExclude = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef(String.valueOf(num)))), null);
filter = includeExclude.convertToLongFilter(DocValueFormat.RAW);
assertTrue(filter.accept(num));
}

// Ints
for (int i = 0; i < 100; i++) {
int num = randomInt();
includeExclude = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef(String.valueOf(num)))), null);
filter = includeExclude.convertToLongFilter(DocValueFormat.RAW);
assertTrue(filter.accept(num));
}

// Shorts
for (int i = 0; i < 100; i++) {
short num = randomShort();
includeExclude = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef(String.valueOf(num)))), null);
filter = includeExclude.convertToLongFilter(DocValueFormat.RAW);
assertTrue(filter.accept(num));
}

// bytes
for (int i = 0; i < 100; i++) {
byte num = randomByte();
includeExclude = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef(String.valueOf(num)))), null);
filter = includeExclude.convertToLongFilter(DocValueFormat.RAW);
assertTrue(filter.accept(num));
}

double num = randomDouble();
final IncludeExclude badIncludeExclude
= new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef(String.valueOf(num)))), null);
NumberFormatException e = expectThrows(NumberFormatException.class,
() -> badIncludeExclude.convertToLongFilter(DocValueFormat.RAW));
assertThat(e.getMessage(), equalTo("For input string: \"" + num + "\""));
}

public void testConvertLongFilterDate() {
IncludeExclude includeExclude = new IncludeExclude(new TreeSet<>(Collections.singleton(new BytesRef("2016-05-03"))), null);
DateFormatter formatter = DateFormatter.forPattern("dateOptionalTime");
IncludeExclude.LongFilter filter = includeExclude.convertToLongFilter(new DocValueFormat.DateTime(formatter,
ZoneOffset.ofHours(1), DateFieldMapper.Resolution.MILLISECONDS));
assertTrue(filter.accept(1462230000000L));
}

public void testPartitionedEquals() throws IOException {
IncludeExclude serialized = serialize(new IncludeExclude(3, 20), IncludeExclude.INCLUDE_FIELD);
assertFalse(serialized.isRegexBased());
Expand Down