summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java
diff options
context:
space:
mode:
authorjavanna <cavannaluca@gmail.com>2015-10-05 13:44:20 +0200
committerLuca Cavanna <cavannaluca@gmail.com>2015-10-05 15:10:30 +0200
commite8653f51569580f23ca28a91106d90b07e989167 (patch)
tree7a5bb2abbdb01d39a4667197233372374f2ded49 /core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java
parent3a0d1841d9dc99b752d13118ea303f1497c28fc0 (diff)
Java api: IdsQueryBuilder to accept only non null ids and types
Types are still optional, but if you do provide them, they can't be null. Split the existing constructor that accepted nnull into two, one that accepts no arguments, and another one that accepts the types argument, which must be not null. Also trimmed down different ways of setting ids, some were misleading as they would always add the ids to the existing ones and not set them, the add prefix makes that clear. Left `addIds` method that accepts a varargs argument. Added check for ids not be null.
Diffstat (limited to 'core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java')
-rw-r--r--core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java50
1 files changed, 17 insertions, 33 deletions
diff --git a/core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java
index b85db4b66b..1de8db2e80 100644
--- a/core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java
+++ b/core/src/main/java/org/elasticsearch/index/query/IdsQueryBuilder.java
@@ -22,7 +22,6 @@ package org.elasticsearch.index.query;
import org.apache.lucene.queries.TermsQuery;
import org.apache.lucene.search.Query;
import org.elasticsearch.cluster.metadata.MetaData;
-import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.lucene.search.Queries;
@@ -47,9 +46,19 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> {
static final IdsQueryBuilder PROTOTYPE = new IdsQueryBuilder();
/**
- * Creates a new IdsQueryBuilder by optionally providing the types of the documents to look for
+ * Creates a new IdsQueryBuilder without providing the types of the documents to look for
*/
- public IdsQueryBuilder(@Nullable String... types) {
+ public IdsQueryBuilder() {
+ this.types = new String[0];
+ }
+
+ /**
+ * Creates a new IdsQueryBuilder by providing the types of the documents to look for
+ */
+ public IdsQueryBuilder(String... types) {
+ if (types == null) {
+ throw new IllegalArgumentException("[ids] types cannot be null");
+ }
this.types = types;
}
@@ -64,33 +73,14 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> {
* Adds ids to the query.
*/
public IdsQueryBuilder addIds(String... ids) {
+ if (ids == null) {
+ throw new IllegalArgumentException("[ids] ids cannot be null");
+ }
Collections.addAll(this.ids, ids);
return this;
}
/**
- * Adds ids to the query.
- */
- public IdsQueryBuilder addIds(Collection<String> ids) {
- this.ids.addAll(ids);
- return this;
- }
-
- /**
- * Adds ids to the filter.
- */
- public IdsQueryBuilder ids(String... ids) {
- return addIds(ids);
- }
-
- /**
- * Adds ids to the filter.
- */
- public IdsQueryBuilder ids(Collection<String> ids) {
- return addIds(ids);
- }
-
- /**
* Returns the ids for the query.
*/
public Set<String> ids() {
@@ -100,13 +90,7 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> {
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
- if (types != null) {
- if (types.length == 1) {
- builder.field("type", types[0]);
- } else {
- builder.array("types", types);
- }
- }
+ builder.array("types", types);
builder.startArray("values");
for (String value : ids) {
builder.value(value);
@@ -128,7 +112,7 @@ public class IdsQueryBuilder extends AbstractQueryBuilder<IdsQueryBuilder> {
query = Queries.newMatchNoDocsQuery();
} else {
Collection<String> typesForQuery;
- if (types == null || types.length == 0) {
+ if (types.length == 0) {
typesForQuery = context.queryTypes();
} else if (types.length == 1 && MetaData.ALL.equals(types[0])) {
typesForQuery = context.mapperService().types();