@Override
  public void toXContent(XContentBuilder builder, Params params) throws IOException {
    if (keyScript == null) {
      throw new SearchSourceBuilderException(
          "key_script must be set on range script facet for facet [" + name + "]");
    }
    if (valueScript == null) {
      throw new SearchSourceBuilderException(
          "value_script must be set on range script facet for facet [" + name + "]");
    }

    if (entries.isEmpty()) {
      throw new SearchSourceBuilderException(
          "at least one range must be defined for range facet [" + name + "]");
    }

    builder.startObject(name);

    builder.startObject(RangeFacetCollectorParser.NAME);
    builder.field("key_script", keyScript);
    builder.field("value_script", valueScript);

    builder.startArray("ranges");
    for (Entry entry : entries) {
      builder.startObject();
      if (!Double.isInfinite(entry.from)) {
        builder.field("from", entry.from);
      }
      if (!Double.isInfinite(entry.to)) {
        builder.field("to", entry.to);
      }
      builder.endObject();
    }
    builder.endArray();

    if (this.params != null) {
      builder.field("params");
      builder.map(this.params);
    }
    builder.endObject();

    addFilterFacetAndGlobal(builder, params);

    builder.endObject();
  }
Ejemplo n.º 2
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();
  }