@Override
 public void writeTo(StreamOutput out) throws IOException {
   boolean hasAggregations = aggregations != null;
   out.writeBoolean(hasAggregations);
   if (hasAggregations) {
     out.writeVInt(aggregations.size());
     for (BytesReference aggregation : aggregations) {
       out.writeBytesReference(aggregation);
     }
   }
   out.writeOptionalBoolean(explain);
   FetchSourceContext.optionalWriteToStream(fetchSourceContext, out);
   boolean hasFieldDataFields = fieldDataFields != null;
   out.writeBoolean(hasFieldDataFields);
   if (hasFieldDataFields) {
     out.writeVInt(fieldDataFields.size());
     for (String field : fieldDataFields) {
       out.writeString(field);
     }
   }
   boolean hasFieldNames = fieldNames != null;
   out.writeBoolean(hasFieldNames);
   if (hasFieldNames) {
     out.writeVInt(fieldNames.size());
     for (String field : fieldNames) {
       out.writeString(field);
     }
   }
   out.writeVInt(from);
   boolean hasHighlightBuilder = highlightBuilder != null;
   out.writeBoolean(hasHighlightBuilder);
   if (hasHighlightBuilder) {
     out.writeBytesReference(highlightBuilder);
   }
   boolean hasIndexBoost = indexBoost != null;
   out.writeBoolean(hasIndexBoost);
   if (hasIndexBoost) {
     out.writeVInt(indexBoost.size());
     for (ObjectCursor<String> key : indexBoost.keys()) {
       out.writeString(key.value);
       out.writeFloat(indexBoost.get(key.value));
     }
   }
   boolean hasInnerHitsBuilder = innerHitsBuilder != null;
   out.writeBoolean(hasInnerHitsBuilder);
   if (hasInnerHitsBuilder) {
     out.writeBytesReference(innerHitsBuilder);
   }
   boolean hasMinScore = minScore != null;
   out.writeBoolean(hasMinScore);
   if (hasMinScore) {
     out.writeFloat(minScore);
   }
   boolean hasPostQuery = postQueryBuilder != null;
   out.writeBoolean(hasPostQuery);
   if (hasPostQuery) {
     out.writeQuery(postQueryBuilder);
   }
   boolean hasQuery = queryBuilder != null;
   out.writeBoolean(hasQuery);
   if (hasQuery) {
     out.writeQuery(queryBuilder);
   }
   boolean hasRescoreBuilders = rescoreBuilders != null;
   out.writeBoolean(hasRescoreBuilders);
   if (hasRescoreBuilders) {
     out.writeVInt(rescoreBuilders.size());
     for (BytesReference rescoreBuilder : rescoreBuilders) {
       out.writeBytesReference(rescoreBuilder);
     }
   }
   boolean hasScriptFields = scriptFields != null;
   out.writeBoolean(hasScriptFields);
   if (hasScriptFields) {
     out.writeVInt(scriptFields.size());
     for (ScriptField scriptField : scriptFields) {
       scriptField.writeTo(out);
     }
   }
   out.writeVInt(size);
   boolean hasSorts = sorts != null;
   out.writeBoolean(hasSorts);
   if (hasSorts) {
     out.writeVInt(sorts.size());
     for (BytesReference sort : sorts) {
       out.writeBytesReference(sort);
     }
   }
   boolean hasStats = stats != null;
   out.writeBoolean(hasStats);
   if (hasStats) {
     out.writeVInt(stats.size());
     for (String stat : stats) {
       out.writeString(stat);
     }
   }
   boolean hasSuggestBuilder = suggestBuilder != null;
   out.writeBoolean(hasSuggestBuilder);
   if (hasSuggestBuilder) {
     out.writeBytesReference(suggestBuilder);
   }
   out.writeVInt(terminateAfter);
   out.writeLong(timeoutInMillis);
   out.writeBoolean(trackScores);
   out.writeOptionalBoolean(version);
   boolean hasExt = ext != null;
   out.writeBoolean(hasExt);
   if (hasExt) {
     out.writeBytesReference(ext);
   }
 }
  public void innerToXContent(XContentBuilder builder, Params params) throws IOException {
    if (from != -1) {
      builder.field(FROM_FIELD.getPreferredName(), from);
    }
    if (size != -1) {
      builder.field(SIZE_FIELD.getPreferredName(), size);
    }

    if (timeoutInMillis != -1) {
      builder.field(TIMEOUT_FIELD.getPreferredName(), timeoutInMillis);
    }

    if (terminateAfter != SearchContext.DEFAULT_TERMINATE_AFTER) {
      builder.field(TERMINATE_AFTER_FIELD.getPreferredName(), terminateAfter);
    }

    if (queryBuilder != null) {
      builder.field(QUERY_FIELD.getPreferredName(), queryBuilder);
    }

    if (postQueryBuilder != null) {
      builder.field(POST_FILTER_FIELD.getPreferredName(), postQueryBuilder);
    }

    if (minScore != null) {
      builder.field(MIN_SCORE_FIELD.getPreferredName(), minScore);
    }

    if (version != null) {
      builder.field(VERSION_FIELD.getPreferredName(), version);
    }

    if (explain != null) {
      builder.field(EXPLAIN_FIELD.getPreferredName(), explain);
    }

    if (fetchSourceContext != null) {
      builder.field(_SOURCE_FIELD.getPreferredName(), fetchSourceContext);
    }

    if (fieldNames != null) {
      if (fieldNames.size() == 1) {
        builder.field(FIELDS_FIELD.getPreferredName(), fieldNames.get(0));
      } else {
        builder.startArray(FIELDS_FIELD.getPreferredName());
        for (String fieldName : fieldNames) {
          builder.value(fieldName);
        }
        builder.endArray();
      }
    }

    if (fieldDataFields != null) {
      builder.startArray(FIELDDATA_FIELDS_FIELD.getPreferredName());
      for (String fieldDataField : fieldDataFields) {
        builder.value(fieldDataField);
      }
      builder.endArray();
    }

    if (scriptFields != null) {
      builder.startObject(SCRIPT_FIELDS_FIELD.getPreferredName());
      for (ScriptField scriptField : scriptFields) {
        scriptField.toXContent(builder, params);
      }
      builder.endObject();
    }

    if (sorts != null) {
      builder.startArray(SORT_FIELD.getPreferredName());
      for (BytesReference sort : sorts) {
        XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(sort);
        parser.nextToken();
        builder.copyCurrentStructure(parser);
      }
      builder.endArray();
    }

    if (trackScores) {
      builder.field(TRACK_SCORES_FIELD.getPreferredName(), true);
    }

    if (indexBoost != null) {
      builder.startObject(INDICES_BOOST_FIELD.getPreferredName());
      assert !indexBoost.containsKey(null);
      final Object[] keys = indexBoost.keys;
      final float[] values = indexBoost.values;
      for (int i = 0; i < keys.length; i++) {
        if (keys[i] != null) {
          builder.field((String) keys[i], values[i]);
        }
      }
      builder.endObject();
    }

    if (aggregations != null) {
      builder.field(AGGREGATIONS_FIELD.getPreferredName());
      builder.startObject();
      for (BytesReference aggregation : aggregations) {
        XContentParser parser =
            XContentFactory.xContent(XContentType.JSON).createParser(aggregation);
        parser.nextToken();
        parser.nextToken();
        builder.copyCurrentStructure(parser);
      }
      builder.endObject();
    }

    if (highlightBuilder != null) {
      builder.field(HIGHLIGHT_FIELD.getPreferredName());
      XContentParser parser =
          XContentFactory.xContent(XContentType.JSON).createParser(highlightBuilder);
      parser.nextToken();
      builder.copyCurrentStructure(parser);
    }

    if (innerHitsBuilder != null) {
      builder.field(INNER_HITS_FIELD.getPreferredName());
      XContentParser parser =
          XContentFactory.xContent(XContentType.JSON).createParser(innerHitsBuilder);
      parser.nextToken();
      builder.copyCurrentStructure(parser);
    }

    if (suggestBuilder != null) {
      builder.field(SUGGEST_FIELD.getPreferredName());
      XContentParser parser =
          XContentFactory.xContent(XContentType.JSON).createParser(suggestBuilder);
      parser.nextToken();
      builder.copyCurrentStructure(parser);
    }

    if (rescoreBuilders != null) {
      builder.startArray(RESCORE_FIELD.getPreferredName());
      for (BytesReference rescoreBuilder : rescoreBuilders) {
        XContentParser parser =
            XContentFactory.xContent(XContentType.JSON).createParser(rescoreBuilder);
        parser.nextToken();
        builder.copyCurrentStructure(parser);
      }
      builder.endArray();
    }

    if (stats != null) {
      builder.field(STATS_FIELD.getPreferredName(), stats);
    }

    if (ext != null) {
      builder.field(EXT_FIELD.getPreferredName());
      XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(ext);
      parser.nextToken();
      builder.copyCurrentStructure(parser);
    }
  }
Ejemplo n.º 3
0
  @Override
  public void toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();

    if (from != -1) {
      builder.field("from", from);
    }
    if (size != -1) {
      builder.field("size", size);
    }
    if (queryParserName != null) {
      builder.field("query_parser_name", queryParserName);
    }

    if (queryBuilder != null) {
      builder.field("query");
      queryBuilder.toXContent(builder, params);
    }

    if (queryBinary != null) {
      if (XContentFactory.xContentType(queryBinary) == builder.contentType()) {
        builder.rawField("query", queryBinary);
      } else {
        builder.field("query_binary");
        builder.value(queryBinary);
      }
    }

    if (explain != null) {
      builder.field("explain", explain);
    }

    if (fieldNames != null) {
      if (fieldNames.size() == 1) {
        builder.field("fields", fieldNames.get(0));
      } else {
        builder.startArray("fields");
        for (String fieldName : fieldNames) {
          builder.value(fieldName);
        }
        builder.endArray();
      }
    }

    if (scriptFields != null) {
      builder.startObject("script_fields");
      for (ScriptField scriptField : scriptFields) {
        builder.startObject(scriptField.fieldName());
        builder.field("script", scriptField.script());
        if (scriptField.params() != null) {
          builder.field("params");
          builder.map(scriptField.params());
        }
        builder.endObject();
      }
      builder.endObject();
    }

    if (sorts != null) {
      builder.startArray("sort");
      for (SortBuilder sort : sorts) {
        builder.startObject();
        sort.toXContent(builder, params);
        builder.endObject();
      }
      builder.endArray();
    }

    if (indexBoost != null) {
      builder.startObject("indices_boost");
      for (TObjectFloatIterator<String> it = indexBoost.iterator(); it.hasNext(); ) {
        it.advance();
        builder.field(it.key(), it.value());
      }
      builder.endObject();
    }

    if (facets != null) {
      builder.field("facets");
      builder.startObject();
      for (AbstractFacetBuilder facet : facets) {
        facet.toXContent(builder, params);
      }
      builder.endObject();
    }

    if (highlightBuilder != null) {
      highlightBuilder.toXContent(builder, params);
    }

    builder.endObject();
  }