|
@@ -30,16 +30,21 @@ import org.elasticsearch.common.io.stream.StreamInput;
|
|
|
import org.elasticsearch.common.io.stream.StreamOutput;
|
|
|
import org.elasticsearch.common.xcontent.XContentFactory;
|
|
|
import org.elasticsearch.common.xcontent.XContentParser;
|
|
|
+import org.elasticsearch.rest.RestRequest;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
+import java.util.Set;
|
|
|
|
|
|
public class MultiTermVectorsRequest extends ActionRequest<MultiTermVectorsRequest> {
|
|
|
|
|
|
String preference;
|
|
|
List<TermVectorRequest> requests = new ArrayList<TermVectorRequest>();
|
|
|
|
|
|
+ final Set<String> ids = new HashSet<String>();
|
|
|
+
|
|
|
public MultiTermVectorsRequest add(TermVectorRequest termVectorRequest) {
|
|
|
requests.add(termVectorRequest);
|
|
|
return this;
|
|
@@ -70,57 +75,57 @@ public class MultiTermVectorsRequest extends ActionRequest<MultiTermVectorsReque
|
|
|
|
|
|
public void add(TermVectorRequest template, BytesReference data)
|
|
|
throws Exception {
|
|
|
- XContentParser parser = XContentFactory.xContent(data).createParser(data);
|
|
|
- try {
|
|
|
- XContentParser.Token token;
|
|
|
- String currentFieldName = null;
|
|
|
- List<String> ids = null;
|
|
|
- while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
|
|
- if (token == XContentParser.Token.FIELD_NAME) {
|
|
|
- currentFieldName = parser.currentName();
|
|
|
- } else if (token == XContentParser.Token.START_ARRAY) {
|
|
|
|
|
|
- if ("docs".equals(currentFieldName)) {
|
|
|
- while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
|
|
- if (token != XContentParser.Token.START_OBJECT) {
|
|
|
- throw new ElasticsearchIllegalArgumentException("docs array element should include an object");
|
|
|
+ XContentParser.Token token;
|
|
|
+ String currentFieldName = null;
|
|
|
+ if (data.length() > 0) {
|
|
|
+ XContentParser parser = XContentFactory.xContent(data).createParser(data);
|
|
|
+ try {
|
|
|
+ while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
|
|
+ if (token == XContentParser.Token.FIELD_NAME) {
|
|
|
+ currentFieldName = parser.currentName();
|
|
|
+ } else if (token == XContentParser.Token.START_ARRAY) {
|
|
|
+
|
|
|
+ if ("docs".equals(currentFieldName)) {
|
|
|
+ while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
|
|
+ if (token != XContentParser.Token.START_OBJECT) {
|
|
|
+ throw new ElasticsearchIllegalArgumentException("docs array element should include an object");
|
|
|
+ }
|
|
|
+ TermVectorRequest termVectorRequest = new TermVectorRequest(template);
|
|
|
+ TermVectorRequest.parseRequest(termVectorRequest, parser);
|
|
|
+ add(termVectorRequest);
|
|
|
}
|
|
|
- TermVectorRequest termVectorRequest = new TermVectorRequest(template);
|
|
|
- TermVectorRequest.parseRequest(termVectorRequest, parser);
|
|
|
- add(termVectorRequest);
|
|
|
- }
|
|
|
- } else if ("ids".equals(currentFieldName)) {
|
|
|
- ids = new ArrayList<String>();
|
|
|
- while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
|
|
- if (!token.isValue()) {
|
|
|
- throw new ElasticsearchIllegalArgumentException("ids array element should only contain ids");
|
|
|
+ } else if ("ids".equals(currentFieldName)) {
|
|
|
+ while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
|
|
+ if (!token.isValue()) {
|
|
|
+ throw new ElasticsearchIllegalArgumentException("ids array element should only contain ids");
|
|
|
+ }
|
|
|
+ ids.add(parser.text());
|
|
|
}
|
|
|
- ids.add(parser.text());
|
|
|
+ } else {
|
|
|
+ throw new ElasticsearchParseException(
|
|
|
+ "No parameter named " + currentFieldName + "and type ARRAY");
|
|
|
}
|
|
|
- } else {
|
|
|
- throw new ElasticsearchParseException(
|
|
|
- "No parameter named " + currentFieldName + "and type ARRAY");
|
|
|
- }
|
|
|
- } else if (token == XContentParser.Token.START_OBJECT && currentFieldName != null) {
|
|
|
- if ("parameters".equals(currentFieldName)) {
|
|
|
- TermVectorRequest.parseRequest(template, parser);
|
|
|
- } else {
|
|
|
- throw new ElasticsearchParseException(
|
|
|
- "No parameter named " + currentFieldName + "and type OBJECT");
|
|
|
+ } else if (token == XContentParser.Token.START_OBJECT && currentFieldName != null) {
|
|
|
+ if ("parameters".equals(currentFieldName)) {
|
|
|
+ TermVectorRequest.parseRequest(template, parser);
|
|
|
+ } else {
|
|
|
+ throw new ElasticsearchParseException(
|
|
|
+ "No parameter named " + currentFieldName + "and type OBJECT");
|
|
|
+ }
|
|
|
+ } else if (currentFieldName != null) {
|
|
|
+ throw new ElasticsearchParseException("_mtermvectors: Parameter " + currentFieldName + "not supported");
|
|
|
}
|
|
|
- } else if (currentFieldName != null) {
|
|
|
- throw new ElasticsearchParseException("_mtermvectors: Parameter " + currentFieldName + "not supported");
|
|
|
}
|
|
|
}
|
|
|
- if (ids != null) {
|
|
|
- for (String id : ids) {
|
|
|
- TermVectorRequest curRequest = new TermVectorRequest(template);
|
|
|
- curRequest.id(id);
|
|
|
- requests.add(curRequest);
|
|
|
- }
|
|
|
+ finally {
|
|
|
+ parser.close();
|
|
|
}
|
|
|
- } finally {
|
|
|
- parser.close();
|
|
|
+ }
|
|
|
+ for (String id : ids) {
|
|
|
+ TermVectorRequest curRequest = new TermVectorRequest(template);
|
|
|
+ curRequest.id(id);
|
|
|
+ requests.add(curRequest);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -144,4 +149,10 @@ public class MultiTermVectorsRequest extends ActionRequest<MultiTermVectorsReque
|
|
|
termVectorRequest.writeTo(out);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void ids(String[] ids) {
|
|
|
+ for (String id : ids) {
|
|
|
+ this.ids.add(id.replaceAll("\\s", ""));
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|