Add tests and fixes for primitive numeric ctors on TermsQueryBuilder

This commit is contained in:
Simon Willnauer 2015-09-18 22:56:50 +02:00
parent 2305ccef43
commit 37cff7b01c
2 changed files with 38 additions and 3 deletions

View File

@ -41,6 +41,7 @@ import org.elasticsearch.search.internal.SearchContext;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
/**
* A filter for a field based on several terms matching on any of them.
@ -89,7 +90,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
* @param values The terms
*/
public TermsQueryBuilder(String fieldName, int... values) {
this(fieldName, values != null ? Arrays.asList(values) : (Iterable<?>) null);
this(fieldName, values != null ? Arrays.stream(values).mapToObj(s -> s).collect(Collectors.toList()) : (Iterable<?>) null);
}
/**
@ -116,7 +117,14 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
* @param values The terms
*/
public TermsQueryBuilder(String fieldName, float... values) {
this(fieldName, values != null ? Arrays.asList(values) : (Iterable<?>) null);
if (values == null) {
throw new IllegalArgumentException("No value specified for terms query");
}
this.fieldName = fieldName;
this.values = new ArrayList<>(values.length);
for (float floatValue : values) {
this.values.add(floatValue);
}
}
/**
@ -126,7 +134,7 @@ public class TermsQueryBuilder extends AbstractQueryBuilder<TermsQueryBuilder> {
* @param values The terms
*/
public TermsQueryBuilder(String fieldName, double... values) {
this(fieldName, values != null ? Arrays.asList(values) : (Iterable<?>) null);
this(fieldName, values != null ? Arrays.stream(values).mapToObj(s -> s).collect(Collectors.toList()) : (Iterable<?>) null);
}
/**

View File

@ -275,5 +275,32 @@ public class TermsQueryBuilderTests extends AbstractQueryTestCase<TermsQueryBuil
}
return new GetResponse(new GetResult(getRequest.index(), getRequest.type(), getRequest.id(), 0, true, new BytesArray(json), null));
}
public void testNumeric() throws IOException {
{
TermsQueryBuilder builder = new TermsQueryBuilder("foo", new int[]{1, 3, 4});
TermsQueryBuilder copy = assertSerialization(builder);
List<Object> values = copy.values();
assertEquals(Arrays.asList(1, 3, 4), values);
}
{
TermsQueryBuilder builder = new TermsQueryBuilder("foo", new double[]{1, 3, 4});
TermsQueryBuilder copy = assertSerialization(builder);
List<Object> values = copy.values();
assertEquals(Arrays.asList(1d, 3d, 4d), values);
}
{
TermsQueryBuilder builder = new TermsQueryBuilder("foo", new float[]{1, 3, 4});
TermsQueryBuilder copy = assertSerialization(builder);
List<Object> values = copy.values();
assertEquals(Arrays.asList(1f, 3f, 4f), values);
}
{
TermsQueryBuilder builder = new TermsQueryBuilder("foo", new long[]{1, 3, 4});
TermsQueryBuilder copy = assertSerialization(builder);
List<Object> values = copy.values();
assertEquals(Arrays.asList(1l, 3l, 4l), values);
}
}
}