Example #1
0
 public static void writeExplanation(StreamOutput out, Explanation explanation)
     throws IOException {
   out.writeFloat(explanation.getValue());
   out.writeString(explanation.getDescription());
   Explanation[] subExplanations = explanation.getDetails();
   if (subExplanations == null) {
     out.writeBoolean(false);
   } else {
     out.writeBoolean(true);
     out.writeVInt(subExplanations.length);
     for (Explanation subExp : subExplanations) {
       writeExplanation(out, subExp);
     }
   }
 }
  public void writeTo(StreamOutput out, InternalSearchHits.StreamContext context)
      throws IOException {
    out.writeFloat(score);
    out.writeText(id);
    out.writeText(type);
    out.writeOptionalStreamable(nestedIdentity);
    out.writeLong(version);
    out.writeBytesReference(source);
    if (explanation == null) {
      out.writeBoolean(false);
    } else {
      out.writeBoolean(true);
      writeExplanation(out, explanation);
    }
    if (fields == null) {
      out.writeVInt(0);
    } else {
      out.writeVInt(fields.size());
      for (SearchHitField hitField : fields().values()) {
        hitField.writeTo(out);
      }
    }
    if (highlightFields == null) {
      out.writeVInt(0);
    } else {
      out.writeVInt(highlightFields.size());
      for (HighlightField highlightField : highlightFields.values()) {
        highlightField.writeTo(out);
      }
    }

    if (sortValues.length == 0) {
      out.writeVInt(0);
    } else {
      out.writeVInt(sortValues.length);
      for (Object sortValue : sortValues) {
        if (sortValue == null) {
          out.writeByte((byte) 0);
        } else {
          Class type = sortValue.getClass();
          if (type == String.class) {
            out.writeByte((byte) 1);
            out.writeString((String) sortValue);
          } else if (type == Integer.class) {
            out.writeByte((byte) 2);
            out.writeInt((Integer) sortValue);
          } else if (type == Long.class) {
            out.writeByte((byte) 3);
            out.writeLong((Long) sortValue);
          } else if (type == Float.class) {
            out.writeByte((byte) 4);
            out.writeFloat((Float) sortValue);
          } else if (type == Double.class) {
            out.writeByte((byte) 5);
            out.writeDouble((Double) sortValue);
          } else if (type == Byte.class) {
            out.writeByte((byte) 6);
            out.writeByte((Byte) sortValue);
          } else if (type == Short.class) {
            out.writeByte((byte) 7);
            out.writeShort((Short) sortValue);
          } else if (type == Boolean.class) {
            out.writeByte((byte) 8);
            out.writeBoolean((Boolean) sortValue);
          } else if (sortValue instanceof Text) {
            out.writeByte((byte) 9);
            out.writeText((Text) sortValue);
          } else {
            throw new IOException("Can't handle sort field value of type [" + type + "]");
          }
        }
      }
    }

    if (matchedQueries.length == 0) {
      out.writeVInt(0);
    } else {
      out.writeVInt(matchedQueries.length);
      for (String matchedFilter : matchedQueries) {
        out.writeString(matchedFilter);
      }
    }

    if (context.streamShardTarget() == InternalSearchHits.StreamContext.ShardTargetType.STREAM) {
      if (shard == null) {
        out.writeBoolean(false);
      } else {
        out.writeBoolean(true);
        shard.writeTo(out);
      }
    } else if (context.streamShardTarget()
        == InternalSearchHits.StreamContext.ShardTargetType.LOOKUP) {
      if (shard == null) {
        out.writeVInt(0);
      } else {
        out.writeVInt(context.shardHandleLookup().get(shard));
      }
    }

    if (innerHits == null) {
      out.writeVInt(0);
    } else {
      out.writeVInt(innerHits.size());
      for (Map.Entry<String, InternalSearchHits> entry : innerHits.entrySet()) {
        out.writeString(entry.getKey());
        entry
            .getValue()
            .writeTo(
                out,
                InternalSearchHits.streamContext()
                    .streamShardTarget(InternalSearchHits.StreamContext.ShardTargetType.NO_STREAM));
      }
    }
  }