diff --git a/src/main/java/org/elasticsearch/common/lucene/Lucene.java b/src/main/java/org/elasticsearch/common/lucene/Lucene.java index e654276d08b..a4b71a4f913 100644 --- a/src/main/java/org/elasticsearch/common/lucene/Lucene.java +++ b/src/main/java/org/elasticsearch/common/lucene/Lucene.java @@ -440,9 +440,17 @@ public class Lucene { } public static Explanation readExplanation(StreamInput in) throws IOException { - float value = in.readFloat(); - String description = in.readString(); - Explanation explanation = new Explanation(value, description); + Explanation explanation; + if (in.getVersion().onOrAfter(org.elasticsearch.Version.V_1_4_0) && in.readBoolean()) { + Boolean match = in.readOptionalBoolean(); + explanation = new ComplexExplanation(); + ((ComplexExplanation) explanation).setMatch(match); + + } else { + explanation = new Explanation(); + } + explanation.setValue(in.readFloat()); + explanation.setDescription(in.readString()); if (in.readBoolean()) { int size = in.readVInt(); for (int i = 0; i < size; i++) { @@ -453,6 +461,15 @@ public class Lucene { } public static void writeExplanation(StreamOutput out, Explanation explanation) throws IOException { + + if (out.getVersion().onOrAfter(org.elasticsearch.Version.V_1_4_0)) { + if (explanation instanceof ComplexExplanation) { + out.writeBoolean(true); + out.writeOptionalBoolean(((ComplexExplanation) explanation).getMatch()); + } else { + out.writeBoolean(false); + } + } out.writeFloat(explanation.getValue()); out.writeString(explanation.getDescription()); Explanation[] subExplanations = explanation.getDetails(); diff --git a/src/test/java/org/elasticsearch/explain/ExplainActionTests.java b/src/test/java/org/elasticsearch/explain/ExplainActionTests.java index 5d225ce166e..ff607549d8f 100644 --- a/src/test/java/org/elasticsearch/explain/ExplainActionTests.java +++ b/src/test/java/org/elasticsearch/explain/ExplainActionTests.java @@ -19,8 +19,13 @@ package org.elasticsearch.explain; +import org.apache.lucene.search.ComplexExplanation; +import org.apache.lucene.search.Explanation; import org.elasticsearch.action.admin.indices.alias.Alias; import org.elasticsearch.action.explain.ExplainResponse; +import org.elasticsearch.common.io.stream.InputStreamStreamInput; +import org.elasticsearch.common.io.stream.OutputStreamStreamOutput; +import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.index.query.FilterBuilders; import org.elasticsearch.index.query.QueryBuilders; @@ -30,6 +35,8 @@ import org.joda.time.DateTimeZone; import org.joda.time.format.ISODateTimeFormat; import org.junit.Test; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.util.Map; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; @@ -256,8 +263,43 @@ public class ExplainActionTests extends ElasticsearchIntegrationTest { assertThat(explainResponse.isExists(), equalTo(true)); assertThat(explainResponse.isMatch(), equalTo(true)); } - + private static String indexOrAlias() { return randomBoolean() ? "test" : "alias"; } + + + @Test + public void streamExplainTest() throws Exception { + + Explanation exp = new Explanation((float) 2.0, "some explanation"); + + // write + ByteArrayOutputStream outBuffer = new ByteArrayOutputStream(); + OutputStreamStreamOutput out = new OutputStreamStreamOutput(outBuffer); + Lucene.writeExplanation(out, exp); + + // read + ByteArrayInputStream esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray()); + InputStreamStreamInput esBuffer = new InputStreamStreamInput(esInBuffer); + + Explanation result = Lucene.readExplanation(esBuffer); + assertThat(exp.toString(),equalTo(result.toString())); + + exp = new ComplexExplanation(true, 2.0f, "some explanation"); + exp.addDetail(new Explanation(2.0f,"another explanation")); + + // write complex + outBuffer = new ByteArrayOutputStream(); + out = new OutputStreamStreamOutput(outBuffer); + Lucene.writeExplanation(out, exp); + + // read complex + esInBuffer = new ByteArrayInputStream(outBuffer.toByteArray()); + esBuffer = new InputStreamStreamInput(esInBuffer); + + result = Lucene.readExplanation(esBuffer); + assertThat(exp.toString(),equalTo(result.toString())); + + } }