diff --git a/src/main/java/org/elasticsearch/action/termvector/MultiTermVectorsRequest.java b/src/main/java/org/elasticsearch/action/termvector/MultiTermVectorsRequest.java index 218c8afb7a4..5bfb94263e3 100644 --- a/src/main/java/org/elasticsearch/action/termvector/MultiTermVectorsRequest.java +++ b/src/main/java/org/elasticsearch/action/termvector/MultiTermVectorsRequest.java @@ -25,7 +25,6 @@ import org.elasticsearch.action.ActionRequest; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ValidateActions; import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; @@ -51,22 +50,6 @@ public class MultiTermVectorsRequest extends ActionRequest_local to prefer local shards, - * _primary to execute only on primary shards, or a custom value, - * which guarantees that the same order will be used across different - * requests. - */ - public MultiTermVectorsRequest preference(String preference) { - this.preference = preference; - return this; - } - - public String preference() { - return this.preference; - } - @Override public ActionRequestValidationException validate() { ActionRequestValidationException validationException = null; @@ -85,49 +68,16 @@ public class MultiTermVectorsRequest extends ActionRequest ids = null; while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { if (token == XContentParser.Token.FIELD_NAME) { currentFieldName = parser.currentName(); - } else if (token == XContentParser.Token.VALUE_BOOLEAN) { - if (currentFieldName.equals("offsets")) { - offsets = parser.booleanValue(); - offsetsFound = true; - } else if (currentFieldName.equals("positions")) { - positions = parser.booleanValue(); - positionsFound = true; - } else if (currentFieldName.equals("payloads")) { - payloads = parser.booleanValue(); - payloadsFound = true; - } else if (currentFieldName.equals("term_statistics") || currentFieldName.equals("termStatistics")) { - termStatistics = parser.booleanValue(); - termStatisticsFound = true; - } else if (currentFieldName.equals("field_statistics") || currentFieldName.equals("fieldStatistics")) { - fieldStatistics = parser.booleanValue(); - fieldStatisticsFound = true; - } else { - throw new ElasticSearchParseException("_mtermvectors: Parameter " + currentFieldName + "not supported"); - } } else if (token == XContentParser.Token.START_ARRAY) { if ("docs".equals(currentFieldName)) { @@ -135,52 +85,39 @@ public class MultiTermVectorsRequest extends ActionRequest(); while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { if (!token.isValue()) { throw new ElasticSearchIllegalArgumentException("ids array element should only contain ids"); } - TermVectorRequest tvr = new TermVectorRequest(defaultIndex, defaultType, parser.text()); - if (defaultFields != null) { - tvr.selectedFields(defaultFields.clone()); - } - add(tvr); + ids.add(parser.text()); } } else { - throw new ElasticSearchParseException("_mtermvectors: Parameter " + currentFieldName + "not supported"); + 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 (currentFieldName != null) { throw new ElasticSearchParseException("_mtermvectors: Parameter " + currentFieldName + "not supported"); } } - for (int i = 0; i < requests.size(); i++) { - TermVectorRequest curRequest = requests.get(i); - if (offsetsFound) { - curRequest.offsets(offsets); + if (ids != null) { + for (String id : ids) { + TermVectorRequest curRequest = new TermVectorRequest(template); + curRequest.id(id); + requests.add(curRequest); } - if (payloadsFound) { - curRequest.payloads(payloads); - } - if (fieldStatisticsFound) { - curRequest.fieldStatistics(fieldStatistics); - } - if (positionsFound) { - curRequest.positions(positions); - } - if (termStatisticsFound) { - curRequest.termStatistics(termStatistics); - } - requests.set(i, curRequest); } } finally { parser.close(); diff --git a/src/main/java/org/elasticsearch/action/termvector/MultiTermVectorsRequestBuilder.java b/src/main/java/org/elasticsearch/action/termvector/MultiTermVectorsRequestBuilder.java index eaeb77a60fc..f12d1579e52 100644 --- a/src/main/java/org/elasticsearch/action/termvector/MultiTermVectorsRequestBuilder.java +++ b/src/main/java/org/elasticsearch/action/termvector/MultiTermVectorsRequestBuilder.java @@ -49,16 +49,6 @@ public class MultiTermVectorsRequestBuilder extends ActionRequestBuilder_local to prefer local shards, _primary to execute only on primary shards, or - * a custom value, which guarantees that the same order will be used across different requests. - */ - public MultiTermVectorsRequestBuilder setPreference(String preference) { - request.preference(preference); - return this; - } - @Override protected void doExecute(ActionListener listener) { ((Client) client).multiTermVectors(request, listener); diff --git a/src/main/java/org/elasticsearch/action/termvector/TermVectorFields.java b/src/main/java/org/elasticsearch/action/termvector/TermVectorFields.java index 67be4002f63..a7d245933e8 100644 --- a/src/main/java/org/elasticsearch/action/termvector/TermVectorFields.java +++ b/src/main/java/org/elasticsearch/action/termvector/TermVectorFields.java @@ -350,7 +350,7 @@ public final class TermVectorFields extends Fields { public int getDocCount() throws IOException { return docCount; } - + @Override public boolean hasFreqs() { return true; diff --git a/src/main/java/org/elasticsearch/action/termvector/TermVectorRequest.java b/src/main/java/org/elasticsearch/action/termvector/TermVectorRequest.java index cc7e0153bd8..3eded8b5b98 100644 --- a/src/main/java/org/elasticsearch/action/termvector/TermVectorRequest.java +++ b/src/main/java/org/elasticsearch/action/termvector/TermVectorRequest.java @@ -54,7 +54,7 @@ public class TermVectorRequest extends SingleShardOperationRequest flagsEnum = EnumSet.of(Flag.Positions, Flag.Offsets, Flag.Payloads, Flag.FieldStatistics); - TermVectorRequest() { + public TermVectorRequest() { } /** @@ -67,6 +67,23 @@ public class TermVectorRequest extends SingleShardOperationRequest(other.selectedFields); + } + } public EnumSet getFlags() { return flagsEnum; @@ -85,6 +102,13 @@ public class TermVectorRequest extends SingleShardOperationRequest_local to prefer local shards, _primary to execute only on primary shards, or + * a custom value, which guarantees that the same order will be used across different requests. + */ + + public TermVectorRequestBuilder setPreference(String preference) { + request.preference(preference); + return this; + } + public TermVectorRequestBuilder setOffsets(boolean offsets) { request.offsets(offsets); return this; diff --git a/src/main/java/org/elasticsearch/rest/action/termvector/RestMultiTermVectorsAction.java b/src/main/java/org/elasticsearch/rest/action/termvector/RestMultiTermVectorsAction.java index 5455cf162fd..b49f7457a86 100644 --- a/src/main/java/org/elasticsearch/rest/action/termvector/RestMultiTermVectorsAction.java +++ b/src/main/java/org/elasticsearch/rest/action/termvector/RestMultiTermVectorsAction.java @@ -22,8 +22,8 @@ package org.elasticsearch.rest.action.termvector; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.termvector.MultiTermVectorsRequest; import org.elasticsearch.action.termvector.MultiTermVectorsResponse; +import org.elasticsearch.action.termvector.TermVectorRequest; import org.elasticsearch.client.Client; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.xcontent.XContentBuilder; @@ -49,18 +49,14 @@ public class RestMultiTermVectorsAction extends BaseRestHandler { @Override public void handleRequest(final RestRequest request, final RestChannel channel) { + MultiTermVectorsRequest multiTermVectorsRequest = new MultiTermVectorsRequest(); multiTermVectorsRequest.listenerThreaded(false); - multiTermVectorsRequest.preference(request.param("preference")); - - String[] sFields = null; - String sField = request.param("fields"); - if (sField != null) { - sFields = Strings.splitStringByCommaToArray(sField); - } - + TermVectorRequest template = new TermVectorRequest(); + RestTermVectorAction.readURIParameters(template, request); + try { - multiTermVectorsRequest.add(request.param("index"), request.param("type"), sFields, request.content()); + multiTermVectorsRequest.add(template, request.content()); } catch (Throwable t) { try { channel.sendResponse(new XContentThrowableRestResponse(request, t)); diff --git a/src/main/java/org/elasticsearch/rest/action/termvector/RestTermVectorAction.java b/src/main/java/org/elasticsearch/rest/action/termvector/RestTermVectorAction.java index bea60c85f94..2ca7ee67e06 100644 --- a/src/main/java/org/elasticsearch/rest/action/termvector/RestTermVectorAction.java +++ b/src/main/java/org/elasticsearch/rest/action/termvector/RestTermVectorAction.java @@ -59,9 +59,6 @@ public class RestTermVectorAction extends BaseRestHandler { public void handleRequest(final RestRequest request, final RestChannel channel) { TermVectorRequest termVectorRequest = new TermVectorRequest(request.param("index"), request.param("type"), request.param("id")); - termVectorRequest.routing(request.param("routing")); - termVectorRequest.parent(request.param("parent")); - termVectorRequest.preference(request.param("preference")); XContentParser parser = null; if (request.hasContent()) { try { @@ -114,6 +111,9 @@ public class RestTermVectorAction extends BaseRestHandler { termVectorRequest.offsets(request.paramAsBoolean("offsets", termVectorRequest.offsets())); termVectorRequest.positions(request.paramAsBoolean("positions", termVectorRequest.positions())); termVectorRequest.payloads(request.paramAsBoolean("payloads", termVectorRequest.payloads())); + termVectorRequest.routing(request.param("routing")); + termVectorRequest.parent(request.param("parent")); + termVectorRequest.preference(request.param("preference")); termVectorRequest.termStatistics(request.paramAsBoolean("termStatistics", termVectorRequest.termStatistics())); termVectorRequest.termStatistics(request.paramAsBoolean("term_statistics", termVectorRequest.termStatistics())); termVectorRequest.fieldStatistics(request.paramAsBoolean("fieldStatistics", termVectorRequest.fieldStatistics())); diff --git a/src/test/java/org/elasticsearch/termvectors/AbstractTermVectorTests.java b/src/test/java/org/elasticsearch/action/termvector/AbstractTermVectorTests.java similarity index 99% rename from src/test/java/org/elasticsearch/termvectors/AbstractTermVectorTests.java rename to src/test/java/org/elasticsearch/action/termvector/AbstractTermVectorTests.java index 2a1a73abc16..5759f91c009 100644 --- a/src/test/java/org/elasticsearch/termvectors/AbstractTermVectorTests.java +++ b/src/test/java/org/elasticsearch/action/termvector/AbstractTermVectorTests.java @@ -1,4 +1,4 @@ -package org.elasticsearch.termvectors; +package org.elasticsearch.action.termvector; /* * Licensed to ElasticSearch under one diff --git a/src/test/java/org/elasticsearch/termvectors/GetTermVectorCheckDocFreqTests.java b/src/test/java/org/elasticsearch/action/termvector/GetTermVectorCheckDocFreqTests.java similarity index 99% rename from src/test/java/org/elasticsearch/termvectors/GetTermVectorCheckDocFreqTests.java rename to src/test/java/org/elasticsearch/action/termvector/GetTermVectorCheckDocFreqTests.java index f5235422bd0..d881d23236c 100644 --- a/src/test/java/org/elasticsearch/termvectors/GetTermVectorCheckDocFreqTests.java +++ b/src/test/java/org/elasticsearch/action/termvector/GetTermVectorCheckDocFreqTests.java @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.termvectors; +package org.elasticsearch.action.termvector; import org.apache.lucene.index.DocsAndPositionsEnum; import org.apache.lucene.index.Fields; diff --git a/src/test/java/org/elasticsearch/termvectors/GetTermVectorTests.java b/src/test/java/org/elasticsearch/action/termvector/GetTermVectorTests.java similarity index 99% rename from src/test/java/org/elasticsearch/termvectors/GetTermVectorTests.java rename to src/test/java/org/elasticsearch/action/termvector/GetTermVectorTests.java index 2f2164255d5..ff30f672b89 100644 --- a/src/test/java/org/elasticsearch/termvectors/GetTermVectorTests.java +++ b/src/test/java/org/elasticsearch/action/termvector/GetTermVectorTests.java @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.termvectors; +package org.elasticsearch.action.termvector; import com.carrotsearch.hppc.ObjectIntOpenHashMap; import org.apache.lucene.analysis.payloads.PayloadHelper; diff --git a/src/test/java/org/elasticsearch/termvectors/MultiTermVectorsTests.java b/src/test/java/org/elasticsearch/action/termvector/MultiTermVectorsTests.java similarity index 98% rename from src/test/java/org/elasticsearch/termvectors/MultiTermVectorsTests.java rename to src/test/java/org/elasticsearch/action/termvector/MultiTermVectorsTests.java index eaa8e902534..d8648c50c5a 100644 --- a/src/test/java/org/elasticsearch/termvectors/MultiTermVectorsTests.java +++ b/src/test/java/org/elasticsearch/action/termvector/MultiTermVectorsTests.java @@ -1,4 +1,4 @@ -package org.elasticsearch.termvectors; +package org.elasticsearch.action.termvector; /* * Licensed to ElasticSearch under one * or more contributor license agreements. See the NOTICE file diff --git a/src/test/java/org/elasticsearch/termvectors/TermVectorUnitTests.java b/src/test/java/org/elasticsearch/action/termvector/TermVectorUnitTests.java similarity index 87% rename from src/test/java/org/elasticsearch/termvectors/TermVectorUnitTests.java rename to src/test/java/org/elasticsearch/action/termvector/TermVectorUnitTests.java index b87c8b3f6c3..290db34f3e2 100644 --- a/src/test/java/org/elasticsearch/termvectors/TermVectorUnitTests.java +++ b/src/test/java/org/elasticsearch/action/termvector/TermVectorUnitTests.java @@ -17,7 +17,7 @@ * under the License. */ -package org.elasticsearch.termvectors; +package org.elasticsearch.action.termvector; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.*; @@ -28,11 +28,10 @@ import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; -import org.elasticsearch.action.termvector.TermVectorRequest; import org.elasticsearch.action.termvector.TermVectorRequest.Flag; -import org.elasticsearch.action.termvector.TermVectorResponse; import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; +import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.io.stream.InputStreamStreamInput; import org.elasticsearch.common.io.stream.OutputStreamStreamOutput; import org.elasticsearch.common.xcontent.XContentFactory; @@ -51,6 +50,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.EnumSet; +import java.util.HashSet; import java.util.Set; import static org.hamcrest.Matchers.equalTo; @@ -288,5 +288,41 @@ public class TermVectorUnitTests extends ElasticsearchLuceneTestCase { String ftOpts = AbstractFieldMapper.termVectorOptionsToString(ft); assertThat(ftOpts, equalTo("with_offsets")); } + + @Test + public void testMultiParser() throws Exception { + byte[] data = Streams.copyToBytesFromClasspath("/org/elasticsearch/action/termvector/multiRequest1.json"); + BytesReference bytes = new BytesArray(data); + MultiTermVectorsRequest request = new MultiTermVectorsRequest(); + request.add(new TermVectorRequest(), bytes); + checkParsedParameters(request); + + data = Streams.copyToBytesFromClasspath("/org/elasticsearch/action/termvector/multiRequest2.json"); + bytes = new BytesArray(data); + request = new MultiTermVectorsRequest(); + request.add(new TermVectorRequest(), bytes); + checkParsedParameters(request); + + } + void checkParsedParameters(MultiTermVectorsRequest request) { + Set ids = new HashSet(); + ids.add("1"); + ids.add("2"); + Set fields = new HashSet(); + fields.add("a"); + fields.add("b"); + fields.add("c"); + for (TermVectorRequest singleRequest : request.requests) { + assertThat(singleRequest.index(), equalTo("testidx")); + assertThat(singleRequest.type(), equalTo("test")); + assertThat(singleRequest.payloads(), equalTo(false)); + assertThat(singleRequest.positions(), equalTo(false)); + assertThat(singleRequest.offsets(), equalTo(false)); + assertThat(singleRequest.termStatistics(), equalTo(true)); + assertThat(singleRequest.fieldStatistics(), equalTo(false)); + assertThat(singleRequest.id(),Matchers.anyOf(Matchers.equalTo("1"), Matchers.equalTo("2"))); + assertThat(singleRequest.selectedFields(), equalTo(fields)); + } + } } diff --git a/src/test/java/org/elasticsearch/action/termvector/multiRequest1.json b/src/test/java/org/elasticsearch/action/termvector/multiRequest1.json new file mode 100644 index 00000000000..fcb5e3a927a --- /dev/null +++ b/src/test/java/org/elasticsearch/action/termvector/multiRequest1.json @@ -0,0 +1,13 @@ +{ + "ids": ["1","2"], + "parameters": { + "field_statistics": false, + "term_statistics": true, + "payloads":false, + "offsets":false, + "positions":false, + "fields":["a","b","c"], + "_index": "testidx", + "_type":"test" + } +} \ No newline at end of file diff --git a/src/test/java/org/elasticsearch/action/termvector/multiRequest2.json b/src/test/java/org/elasticsearch/action/termvector/multiRequest2.json new file mode 100644 index 00000000000..a0709effe70 --- /dev/null +++ b/src/test/java/org/elasticsearch/action/termvector/multiRequest2.json @@ -0,0 +1,26 @@ +{ + "docs": [ + { + "_id": "1", + "field_statistics": false, + "term_statistics": true, + "payloads": false, + "offsets": false, + "positions": false, + "fields":["a","b","c"], + "_index": "testidx", + "_type": "test" + }, + { + "_id": "2", + "field_statistics": false, + "term_statistics": true, + "payloads": false, + "offsets": false, + "positions": false, + "fields":["a","b","c"], + "_index": "testidx", + "_type": "test" + } + ] +} \ No newline at end of file