org.elasticsearch.common.xcontent.XContentBuilder.prettyPrint()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(10.1k)|赞(0)|评价(0)|浏览(103)

本文整理了Java中org.elasticsearch.common.xcontent.XContentBuilder.prettyPrint()方法的一些代码示例,展示了XContentBuilder.prettyPrint()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XContentBuilder.prettyPrint()方法的具体详情如下:
包路径:org.elasticsearch.common.xcontent.XContentBuilder
类名称:XContentBuilder
方法名:prettyPrint

XContentBuilder.prettyPrint介绍

暂无

代码示例

代码示例来源:origin: NLPchina/elasticsearch-sql

@Override
public String explain() {
  try {
    XContentBuilder firstBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
    this.firstSearchRequest.request().source().toXContent(firstBuilder, ToXContent.EMPTY_PARAMS);
    XContentBuilder secondBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
    this.secondSearchRequest.request().source().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS);
    String explained = String.format("performing %s on :\n left query:\n%s\n right query:\n%s", this.relation.name, BytesReference.bytes(firstBuilder).utf8ToString(), BytesReference.bytes(secondBuilder).utf8ToString());
    return explained;
  } catch (IOException e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: NLPchina/elasticsearch-sql

@Override
public String explain() {
  try {
    XContentBuilder firstBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
    firstTable.getRequestBuilder().request().source().toXContent(firstBuilder, ToXContent.EMPTY_PARAMS);
    XContentBuilder secondBuilder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
    secondTable.getRequestBuilder().request().source().toXContent(secondBuilder, ToXContent.EMPTY_PARAMS);
    String explained = String.format(" first query:\n%s\n second query:\n%s", BytesReference.bytes(firstBuilder).utf8ToString(), BytesReference.bytes(secondBuilder).utf8ToString());
    return explained;
  } catch (IOException e) {
    e.printStackTrace();
  }
  return null;
}

代码示例来源:origin: NLPchina/elasticsearch-sql

public static String hitsAsStringResult(SearchHits results, MetaSearchResult metaResults) throws IOException {
    if(results == null) return null;
    Object[] searchHits;
    searchHits = new Object[(int) results.getTotalHits()];
    int i = 0;
    for(SearchHit hit : results) {
      HashMap<String,Object> value = new HashMap<>();
      value.put("_id",hit.getId());
      value.put("_type", hit.getType());
      value.put("_score", hit.getScore());
      value.put("_source", hit.getSourceAsMap());
      searchHits[i] = value;
      i++;
    }
    HashMap<String,Object> hits = new HashMap<>();
    hits.put("total",results.getTotalHits());
    hits.put("max_score",results.getMaxScore());
    hits.put("hits",searchHits);
    XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON).prettyPrint();
    builder.startObject();
    builder.field("took", metaResults.getTookImMilli());
    builder.field("timed_out",metaResults.isTimedOut());
    builder.field("_shards", ImmutableMap.of("total", metaResults.getTotalNumOfShards(),
        "successful", metaResults.getSuccessfulShards()
        , "failed", metaResults.getFailedShards()));
    builder.field("hits",hits) ;
    builder.endObject();
    return BytesReference.bytes(builder).utf8ToString();
  }
}

代码示例来源:origin: floragunncom/search-guard

private static String convertToYaml(String type, BytesReference bytes, boolean prettyPrint) throws IOException {
  
  try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, bytes.streamInput())) {
    
    parser.nextToken();
    parser.nextToken();
    
    if(!type.equals((parser.currentName()))) {
      return null;
    }
    
    parser.nextToken();
    
    XContentBuilder builder = XContentFactory.yamlBuilder();
    if (prettyPrint) {
      builder.prettyPrint();
    }
    builder.rawValue(new ByteArrayInputStream(parser.binaryValue()), XContentType.YAML);
    return Strings.toString(builder);
  }
}

代码示例来源:origin: richardwilly98/elasticsearch-river-mongodb

public static XContentBuilder restContentBuilder(RestRequest request, @Nullable BytesReference autoDetectSource) throws IOException {
  XContentType contentType = XContentType.fromRestContentType(request.param("format", request.header("Content-Type")));
  if (contentType == null) {
    // try and guess it from the auto detect source
    if (autoDetectSource != null) {
      contentType = XContentFactory.xContentType(autoDetectSource);
    }
  }
  if (contentType == null) {
    // default to JSON
    contentType = XContentType.JSON;
  }
  XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(contentType), new BytesStreamOutput());
  if (request.paramAsBoolean("pretty", false)) {
    builder.prettyPrint().lfAtEnd();
  }
  builder.humanReadable(request.paramAsBoolean("human", builder.humanReadable()));
  String casing = request.param("case");
  if (casing != null && "camelCase".equals(casing)) {
    builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.CAMELCASE);
  } else {
    // we expect all REST interfaces to write results in underscore casing, so
    // no need for double casing
    builder.fieldCaseConversion(XContentBuilder.FieldCaseConversion.NONE);
  }
  return builder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
protected XContentBuilder newXContentBuilder(XContentType type, OutputStream stream) throws IOException {
  XContentBuilder xContentBuilder = super.newXContentBuilder(type, stream);
  xContentBuilder.prettyPrint();
  return xContentBuilder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
protected XContentBuilder newXContentBuilder(XContentType type, OutputStream stream) throws IOException {
  XContentBuilder xContentBuilder = super.newXContentBuilder(type, stream);
  xContentBuilder.prettyPrint();
  return xContentBuilder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public String toString() {
  try {
    final XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
    builder.startObject();
    toXContent(builder, EMPTY_PARAMS);
    builder.endObject();
    return Strings.toString(builder);
  } catch (final IOException e) {
    return "{ \"error\" : \"" + e.getMessage() + "\"}";
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public String toString() {
  try {
    XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
    builder.startObject();
    toXContent(builder, EMPTY_PARAMS);
    builder.endObject();
    return Strings.toString(builder);
  } catch (IOException e) {
    return "{ \"error\" : \"" + e.getMessage() + "\"}";
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public String toString() {
  try {
    XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
    builder.startObject();
    toXContent(builder, EMPTY_PARAMS);
    builder.endObject();
    return Strings.toString(builder);
  } catch (IOException e) {
    return "{ \"error\" : \"" + e.getMessage() + "\"}";
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public synchronized String toString() {
  try {
    XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
    builder.startObject();
    toXContent(builder, EMPTY_PARAMS);
    builder.endObject();
    return Strings.toString(builder);
  } catch (IOException e) {
    return "{ \"error\" : \"" + e.getMessage() + "\"}";
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
  public String toString() {
    try {
      XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
      builder.startObject();
      toXContent(builder, EMPTY_PARAMS);
      builder.endObject();
      return Strings.toString(builder);
    } catch (IOException e) {
      return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
  public String toString() {
    try {
      XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
      builder.startObject();
      toXContent(builder, EMPTY_PARAMS);
      builder.endObject();
      return Strings.toString(builder);
    } catch (IOException e) {
      return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
  public String toString() {
    try {
      XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
      builder.startObject();
      toXContent(builder, EMPTY_PARAMS);
      builder.endObject();
      return Strings.toString(builder);
    } catch (IOException e) {
      return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

private static XContentBuilder createBuilder(boolean pretty, boolean human) throws IOException {
  XContentBuilder builder = JsonXContent.contentBuilder();
  if (pretty) {
    builder.prettyPrint();
  }
  if (human) {
    builder.humanReadable(true);
  }
  return builder;
}

代码示例来源:origin: org.elasticsearch/elasticsearch

/**
   * Returns a JSON version of this exception for debugging.
   */
  public String toJsonString() {
    try {
      XContentBuilder json = XContentFactory.jsonBuilder().prettyPrint();
      json.startObject();
      toXContent(json, ToXContent.EMPTY_PARAMS);
      json.endObject();
      return Strings.toString(json);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public String toString() {
  try {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.prettyPrint();
    toXContent(builder, EMPTY_PARAMS);
    return Strings.toString(builder);
  } catch (Exception e) {
    return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
public String toString() {
  try {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.prettyPrint();
    toXContent(builder, EMPTY_PARAMS);
    return Strings.toString(builder);
  } catch (Exception e) {
    return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

@Override
  public String toString() {
    try {
      XContentBuilder builder = XContentFactory.jsonBuilder();
      builder.prettyPrint();
      toXContent(builder, EMPTY_PARAMS);
      return Strings.toString(builder);
    } catch (Exception e) {
      throw new ElasticsearchException("Failed to build xcontent.", e);
    }
  }
}

代码示例来源:origin: org.elasticsearch/elasticsearch

static BytesRestResponse convertMainResponse(MainResponse response, RestRequest request, XContentBuilder builder) throws IOException {
  RestStatus status = response.isAvailable() ? RestStatus.OK : RestStatus.SERVICE_UNAVAILABLE;
  // Default to pretty printing, but allow ?pretty=false to disable
  if (request.hasParam("pretty") == false) {
    builder.prettyPrint().lfAtEnd();
  }
  response.toXContent(builder, request);
  return new BytesRestResponse(status, builder);
}

相关文章

微信公众号

最新文章

更多