Java 类org.elasticsearch.common.xcontent.XContentHelper 实例源码

项目:elasticsearch_my    文件:JsonProcessor.java   
@Override
public void execute(IngestDocument document) throws Exception {
    String stringValue = document.getFieldValue(field, String.class);
    try {
        Map<String, Object> mapValue = XContentHelper.convertToMap(JsonXContent.jsonXContent, stringValue, false);
        if (addToRoot) {
            for (Map.Entry<String, Object> entry : mapValue.entrySet()) {
                document.setFieldValue(entry.getKey(), entry.getValue());
            }
        } else {
            document.setFieldValue(targetField, mapValue);
        }
    } catch (ElasticsearchParseException e) {
        throw new IllegalArgumentException(e);
    }
}
项目:elasticsearch_my    文件:MustacheTests.java   
public void testEmbeddedToJSON() throws Exception {
    XContentBuilder builder = jsonBuilder().startObject()
                    .startArray("bulks")
                        .startObject()
                            .field("index", "index-1")
                            .field("type", "type-1")
                            .field("id", 1)
                        .endObject()
                        .startObject()
                            .field("index", "index-2")
                            .field("type", "type-2")
                            .field("id", 2)
                        .endObject()
                    .endArray()
                .endObject();

    Map<String, Object> ctx =
        Collections.singletonMap("ctx", XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2());

    assertScript("{{#ctx.bulks}}{{#toJson}}.{{/toJson}}{{/ctx.bulks}}", ctx,
            equalTo("{\"index\":\"index-1\",\"id\":1,\"type\":\"type-1\"}{\"index\":\"index-2\",\"id\":2,\"type\":\"type-2\"}"));

    assertScript("{{#ctx.bulks}}<{{#toJson}}id{{/toJson}}>{{/ctx.bulks}}", ctx,
            equalTo("<1><2>"));
}
项目:elasticsearch_my    文件:PipelineStore.java   
void validatePipeline(Map<DiscoveryNode, IngestInfo> ingestInfos, PutPipelineRequest request) throws Exception {
    if (ingestInfos.isEmpty()) {
        throw new IllegalStateException("Ingest info is empty");
    }

    Map<String, Object> pipelineConfig = XContentHelper.convertToMap(request.getSource(), false, request.getXContentType()).v2();
    Pipeline pipeline = factory.create(request.getId(), pipelineConfig, processorFactories);
    List<Exception> exceptions = new ArrayList<>();
    for (Processor processor : pipeline.flattenAllProcessors()) {
        for (Map.Entry<DiscoveryNode, IngestInfo> entry : ingestInfos.entrySet()) {
            if (entry.getValue().containsProcessor(processor.getType()) == false) {
                String message = "Processor type [" + processor.getType() + "] is not installed on node [" + entry.getKey() + "]";
                exceptions.add(ConfigurationUtils.newConfigurationException(processor.getType(), processor.getTag(), null, message));
            }
        }
    }
    ExceptionsHelper.rethrowAndSuppress(exceptions);
}
项目:elasticsearch_my    文件:DocumentParser.java   
ParsedDocument parseDocument(SourceToParse source) throws MapperParsingException {
    validateType(source);

    final Mapping mapping = docMapper.mapping();
    final ParseContext.InternalParseContext context;
    try (XContentParser parser = XContentHelper.createParser(docMapperParser.getXContentRegistry(), source.source())) {
        context = new ParseContext.InternalParseContext(indexSettings.getSettings(),
                docMapperParser, docMapper, source, parser);
        validateStart(parser);
        internalParseDocument(mapping, context, parser);
        validateEnd(parser);
    } catch (Exception e) {
        throw wrapInMapperParsingException(source, e);
    }
    String remainingPath = context.path().pathAsText("");
    if (remainingPath.isEmpty() == false) {
        throw new IllegalStateException("found leftover path elements: " + remainingPath);
    }

    reverseOrder(context);

    ParsedDocument doc = parsedDocument(source, context, createDynamicUpdate(mapping, docMapper, context.getDynamicMappers()));
    return doc;
}
项目:elasticsearch_my    文件:AliasMetaData.java   
public static void toXContent(AliasMetaData aliasMetaData, XContentBuilder builder, ToXContent.Params params) throws IOException {
    builder.startObject(aliasMetaData.alias());

    boolean binary = params.paramAsBoolean("binary", false);

    if (aliasMetaData.filter() != null) {
        if (binary) {
            builder.field("filter", aliasMetaData.filter.compressed());
        } else {
            builder.field("filter", XContentHelper.convertToMap(new BytesArray(aliasMetaData.filter().uncompressed()), true).v2());
        }
    }
    if (aliasMetaData.indexRouting() != null) {
        builder.field("index_routing", aliasMetaData.indexRouting());
    }
    if (aliasMetaData.searchRouting() != null) {
        builder.field("search_routing", aliasMetaData.searchRouting());
    }

    builder.endObject();
}
项目:elasticsearch_my    文件:SimulatePipelineTransportAction.java   
@Override
protected void doExecute(SimulatePipelineRequest request, ActionListener<SimulatePipelineResponse> listener) {
    final Map<String, Object> source = XContentHelper.convertToMap(request.getSource(), false, request.getXContentType()).v2();

    final SimulatePipelineRequest.Parsed simulateRequest;
    try {
        if (request.getId() != null) {
            simulateRequest = SimulatePipelineRequest.parseWithPipelineId(request.getId(), source, request.isVerbose(), pipelineStore);
        } else {
            simulateRequest = SimulatePipelineRequest.parse(source, request.isVerbose(), pipelineStore);
        }
    } catch (Exception e) {
        listener.onFailure(e);
        return;
    }

    executionService.execute(simulateRequest, listener);
}
项目:elasticsearch_my    文件:ElasticsearchExceptionTests.java   
public void testThrowableToAndFromXContent() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();

    final Tuple<Throwable, ElasticsearchException> exceptions = randomExceptions();
    final Throwable throwable = exceptions.v1();

    BytesReference throwableBytes = XContentHelper.toXContent((builder, params) -> {
        ElasticsearchException.generateThrowableXContent(builder, params, throwable);
        return builder;
    }, xContent.type(), randomBoolean());

    ElasticsearchException parsedException;
    try (XContentParser parser = createParser(xContent, throwableBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        parsedException = ElasticsearchException.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertDeepEquals(exceptions.v2(), parsedException);
}
项目:elasticsearch_my    文件:ElasticsearchExceptionTests.java   
public void testUnknownFailureToAndFromXContent() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();

    BytesReference failureBytes = XContentHelper.toXContent((builder, params) -> {
        // Prints a null failure using generateFailureXContent()
        ElasticsearchException.generateFailureXContent(builder, params, null, randomBoolean());
        return builder;
    }, xContent.type(), randomBoolean());

    ElasticsearchException parsedFailure;
    try (XContentParser parser = createParser(xContent, failureBytes)) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedFailure = ElasticsearchException.failureFromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }

    // Failure was null, expecting a "unknown" reason
    assertEquals("Elasticsearch exception [type=exception, reason=unknown]", parsedFailure.getMessage());
    assertEquals(0, parsedFailure.getHeaders().size());
    assertEquals(0, parsedFailure.getMetadata().size());
}
项目:elasticsearch_my    文件:SearchCancellationIT.java   
public void testCancellationDuringQueryPhase() throws Exception {

        List<ScriptedBlockPlugin> plugins = initBlockFactory();
        indexTestData();

        logger.info("Executing search");
        ListenableActionFuture<SearchResponse> searchResponse = client().prepareSearch("test").setQuery(
            scriptQuery(new Script(
                ScriptType.INLINE, "native", NativeTestScriptedBlockFactory.TEST_NATIVE_BLOCK_SCRIPT, Collections.emptyMap())))
            .execute();

        awaitForBlock(plugins);
        cancelSearch(SearchAction.NAME);
        disableBlocks(plugins);
        logger.info("Segments {}", XContentHelper.toString(client().admin().indices().prepareSegments("test").get(), FORMAT_PARAMS));
        ensureSearchWasCancelled(searchResponse);
    }
项目:elasticsearch_my    文件:SearchCancellationIT.java   
public void testCancellationDuringFetchPhase() throws Exception {

        List<ScriptedBlockPlugin> plugins = initBlockFactory();
        indexTestData();

        logger.info("Executing search");
        ListenableActionFuture<SearchResponse> searchResponse = client().prepareSearch("test")
            .addScriptField("test_field",
                new Script(ScriptType.INLINE, "native", NativeTestScriptedBlockFactory.TEST_NATIVE_BLOCK_SCRIPT, Collections.emptyMap())
            ).execute();

        awaitForBlock(plugins);
        cancelSearch(SearchAction.NAME);
        disableBlocks(plugins);
        logger.info("Segments {}", XContentHelper.toString(client().admin().indices().prepareSegments("test").get(), FORMAT_PARAMS));
        ensureSearchWasCancelled(searchResponse);
    }
项目:elasticsearch_my    文件:PipelineConfigurationTests.java   
public void testParser() throws IOException {
    ContextParser<Void, PipelineConfiguration> parser = PipelineConfiguration.getParser();
    XContentType xContentType = randomFrom(XContentType.values());
    final BytesReference bytes;
    try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) {
        new PipelineConfiguration("1", new BytesArray("{}".getBytes(StandardCharsets.UTF_8)), XContentType.JSON)
            .toXContent(builder, ToXContent.EMPTY_PARAMS);
        bytes = builder.bytes();
    }

    XContentParser xContentParser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, bytes);
    PipelineConfiguration parsed = parser.parse(xContentParser, null);
    assertEquals(xContentType, parsed.getXContentType());
    assertEquals("{}", XContentHelper.convertToJson(parsed.getConfig(), false, parsed.getXContentType()));
    assertEquals("1", parsed.getId());
}
项目:elasticsearch_my    文件:IndexLevelReplicationTests.java   
public void testCheckpointsAdvance() throws Exception {
    try (ReplicationGroup shards = createGroup(randomInt(3))) {
        shards.startPrimary();
        int numDocs = 0;
        int startedShards;
        do {
            numDocs += shards.indexDocs(randomInt(20));
            startedShards = shards.startReplicas(randomIntBetween(1, 2));
        } while (startedShards > 0);

        if (numDocs == 0 || randomBoolean()) {
            // in the case we have no indexing, we simulate the background global checkpoint sync
            shards.getPrimary().updateGlobalCheckpointOnPrimary();
        }
        for (IndexShard shard : shards) {
            final SeqNoStats shardStats = shard.seqNoStats();
            final ShardRouting shardRouting = shard.routingEntry();
            logger.debug("seq_no stats for {}: {}", shardRouting, XContentHelper.toString(shardStats,
                new ToXContent.MapParams(Collections.singletonMap("pretty", "false"))));
            assertThat(shardRouting + " local checkpoint mismatch", shardStats.getLocalCheckpoint(), equalTo(numDocs - 1L));

            assertThat(shardRouting + " global checkpoint mismatch", shardStats.getGlobalCheckpoint(), equalTo(numDocs - 1L));
            assertThat(shardRouting + " max seq no mismatch", shardStats.getMaxSeqNo(), equalTo(numDocs - 1L));
        }
    }
}
项目:elasticsearch_my    文件:XContentMapValuesTests.java   
@SuppressWarnings({"unchecked"})
public void testIncludingObjectWithNestedIncludedObject() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
            .startObject("obj1")
            .startObject("obj2")
            .endObject()
            .endObject()
            .endObject();

    Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true, builder.contentType());
    Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[]{"*.obj2"}, Strings.EMPTY_ARRAY);

    assertThat(filteredSource.size(), equalTo(1));
    assertThat(filteredSource, hasKey("obj1"));
    assertThat(((Map) filteredSource.get("obj1")).size(), equalTo(1));
    assertThat(((Map<String, Object>) filteredSource.get("obj1")), hasKey("obj2"));
    assertThat(((Map) ((Map) filteredSource.get("obj1")).get("obj2")).size(), equalTo(0));
}
项目:elasticsearch_my    文件:PutIndexTemplateRequestTests.java   
public void testPutIndexTemplateRequestSerializationXContent() throws IOException {
    PutIndexTemplateRequest request = new PutIndexTemplateRequest("foo");
    String mapping = YamlXContent.contentBuilder().startObject().field("foo", "bar").endObject().string();
    request.patterns(Collections.singletonList("foo"));
    request.mapping("bar", mapping, XContentType.YAML);
    assertNotEquals(mapping, request.mappings().get("bar"));
    assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), request.mappings().get("bar"));

    BytesStreamOutput out = new BytesStreamOutput();
    request.writeTo(out);

    StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
    PutIndexTemplateRequest serialized = new PutIndexTemplateRequest();
    serialized.readFrom(in);
    assertEquals(XContentHelper.convertToJson(new BytesArray(mapping), false, XContentType.YAML), serialized.mappings().get("bar"));
}
项目:elasticsearch_my    文件:TaskTests.java   
public void testTaskInfoToString() {
    String nodeId = randomAsciiOfLength(10);
    long taskId = randomIntBetween(0, 100000);
    long startTime = randomNonNegativeLong();
    long runningTime = randomNonNegativeLong();
    boolean cancellable = randomBoolean();
    TaskInfo taskInfo = new TaskInfo(new TaskId(nodeId, taskId), "test_type",
        "test_action", "test_description", null, startTime, runningTime, cancellable, TaskId.EMPTY_TASK_ID);
    String taskInfoString = taskInfo.toString();
    Map<String, Object> map = XContentHelper.convertToMap(new BytesArray(taskInfoString.getBytes(StandardCharsets.UTF_8)), true).v2();
    assertEquals(((Number)map.get("id")).longValue(), taskId);
    assertEquals(map.get("type"), "test_type");
    assertEquals(map.get("action"), "test_action");
    assertEquals(map.get("description"), "test_description");
    assertEquals(((Number)map.get("start_time_in_millis")).longValue(), startTime);
    assertEquals(((Number)map.get("running_time_in_nanos")).longValue(), runningTime);
    assertEquals(map.get("cancellable"), cancellable);
}
项目:Elasticsearch    文件:WriterProjector.java   
@Override
@SuppressWarnings("unchecked")
public void write(Row row) {
    for (CollectExpression<Row, ?> collectExpression : collectExpressions) {
        collectExpression.setNextRow(row);
    }
    Map doc = (Map) row.get(0);
    XContentHelper.update(doc, overwrites, false);
    try {
        builder.map(doc);
        builder.flush();
        outputStream.write(NEW_LINE);
    } catch (IOException e) {
        throw new UnhandledServerException("Failed to write row to output", e);
    }
}
项目:Elasticsearch    文件:SQLXContentSourceParser.java   
public void parseSource(BytesReference source) throws SQLParseException {
    XContentParser parser = null;
    try {
        if (source != null && source.length() != 0) {
            parser = XContentFactory.xContent(source).createParser(source);
            parse(parser);
        }
        validate();
    } catch (Exception e) {
        String sSource = "_na_";
        try {
            sSource = XContentHelper.convertToJson(source, false);
        } catch (Throwable e1) {
            // ignore
        }
        throw new SQLParseException("Failed to parse source [" + sSource + "]", e);
    } finally {
        if (parser != null) {
            parser.close();
        }
    }
}
项目:Elasticsearch    文件:TransportShardUpsertAction.java   
@VisibleForTesting
Map<String, Object> buildMapFromSource(Reference[] insertColumns,
                                       Object[] insertValues,
                                       boolean isRawSourceInsert) {
    Map<String, Object> sourceAsMap;
    if (isRawSourceInsert) {
        BytesRef source = (BytesRef) insertValues[0];
        sourceAsMap = XContentHelper.convertToMap(new BytesArray(source), true).v2();
    } else {
        sourceAsMap = new LinkedHashMap<>(insertColumns.length);
        for (int i = 0; i < insertColumns.length; i++) {
            sourceAsMap.put(insertColumns[i].ident().columnIdent().fqn(), insertValues[i]);
        }
    }
    return sourceAsMap;
}
项目:Elasticsearch    文件:BlobStoreRepository.java   
/**
 * Reads snapshot index file
 * <p>
 * This file can be used by read-only repositories that are unable to list files in the repository
 *
 * @return list of snapshots in the repository
 * @throws IOException I/O errors
 */
protected List<SnapshotId> readSnapshotList() throws IOException {
    try (InputStream blob = snapshotsBlobContainer.readBlob(SNAPSHOTS_FILE)) {
        final byte[] data = ByteStreams.toByteArray(blob);
        ArrayList<SnapshotId> snapshots = new ArrayList<>();
        try (XContentParser parser = XContentHelper.createParser(new BytesArray(data))) {
            if (parser.nextToken() == XContentParser.Token.START_OBJECT) {
                if (parser.nextToken() == XContentParser.Token.FIELD_NAME) {
                    String currentFieldName = parser.currentName();
                    if ("snapshots".equals(currentFieldName)) {
                        if (parser.nextToken() == XContentParser.Token.START_ARRAY) {
                            while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                                snapshots.add(new SnapshotId(repositoryName, parser.text()));
                            }
                        }
                    }
                }
            }
        }
        return Collections.unmodifiableList(snapshots);
    }
}
项目:Elasticsearch    文件:NestedInnerQueryParseSupport.java   
public Query getInnerFilter() throws IOException {
    if (filterParsed) {
        return innerFilter;
    } else {
        if (path == null) {
            throw new QueryParsingException(parseContext, "[nested] requires 'path' field");
        }
        if (!filterFound) {
            throw new QueryParsingException(parseContext, "[nested] requires either 'query' or 'filter' field");
        }

        setPathLevel();
        XContentParser old = parseContext.parser();
        try {
            XContentParser innerParser = XContentHelper.createParser(source);
            parseContext.parser(innerParser);
            innerFilter = parseContext.parseInnerFilter();
            filterParsed = true;
            return innerFilter;
        } finally {
            resetPathLevel();
            parseContext.parser(old);
        }
    }
}
项目:Elasticsearch    文件:RestClearScrollAction.java   
public static void buildFromContent(BytesReference content, ClearScrollRequest clearScrollRequest) {
    try (XContentParser parser = XContentHelper.createParser(content)) {
        if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
            throw new IllegalArgumentException("Malformed content, must start with an object");
        } else {
            XContentParser.Token token;
            String currentFieldName = null;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.START_ARRAY) {
                    while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
                        if (token.isValue() == false) {
                            throw new IllegalArgumentException("scroll_id array element should only contain scroll_id");
                        }
                        clearScrollRequest.addScrollId(parser.text());
                    }
                } else {
                    throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] ");
                }
            }
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to parse request body", e);
    }
}
项目:Elasticsearch    文件:RestSearchScrollAction.java   
public static void buildFromContent(BytesReference content, SearchScrollRequest searchScrollRequest) {
    try (XContentParser parser = XContentHelper.createParser(content)) {
        if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
            throw new IllegalArgumentException("Malforrmed content, must start with an object");
        } else {
            XContentParser.Token token;
            String currentFieldName = null;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if ("scroll_id".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
                    searchScrollRequest.scrollId(parser.text());
                } else if ("scroll".equals(currentFieldName) && token == XContentParser.Token.VALUE_STRING) {
                    searchScrollRequest.scroll(new Scroll(TimeValue.parseTimeValue(parser.text(), null, "scroll")));
                } else {
                    throw new IllegalArgumentException("Unknown parameter [" + currentFieldName + "] in request body or parameter is of the wrong type[" + token + "] ");
                }
            }
        }
    } catch (IOException e) {
        throw new IllegalArgumentException("Failed to parse request body", e);
    }
}
项目:Elasticsearch    文件:MappingMetaData.java   
public MappingMetaData(CompressedXContent mapping) throws IOException {
    Map<String, Object> mappingMap;
    try (XContentParser parser = XContentHelper.createParser(mapping.compressedReference())) {
        mappingMap = parser.mapOrdered();
    }
    if (mappingMap.containsKey(MAPPING_VERSION)) {
        this.mappingVersion = (int)mappingMap.get(MAPPING_VERSION);
        mappingMap.remove(MAPPING_VERSION);
    } else {
        this.mappingVersion = 1;
    }
    XContentBuilder mappingBuilder = XContentFactory.jsonBuilder().map(mappingMap);
    this.source = new CompressedXContent(mappingBuilder.bytes());
    if (mappingMap.size() != 1) {
        throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string());
    }
    this.type = mappingMap.keySet().iterator().next();
    initMappers((Map<String, Object>) mappingMap.get(this.type));
}
项目:es-legacy-completion-plugin    文件:LegacyCompletionFieldMapper.java   
@Override // HACK ALARM: calls super's toXContent, but patches type name by de-serializing and re-serializing
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
  final XContentBuilder innerBuilder = XContentFactory.cborBuilder().startObject();
  super.toXContent(innerBuilder, params);
  final BytesReference bytes = innerBuilder.endObject().bytes();
  @SuppressWarnings("unchecked") final Map<String,Object> map = (Map<String,Object>) XContentHelper
      .convertToMap(bytes, true, XContentType.CBOR).v2().values().iterator().next();
  map.put(Fields.TYPE, CONTENT_TYPE); // patch the name :-)
  return builder.field(simpleName(), map);
}
项目:Elasticsearch    文件:CountRequest.java   
@Override
public String toString() {
    String sSource = "_na_";
    try {
        sSource = XContentHelper.convertToJson(source, false);
    } catch (Exception e) {
        // ignore
    }
    return "[" + Arrays.toString(indices) + "]" + Arrays.toString(types) + ", source[" + sSource + "]";
}
项目:elasticsearch_my    文件:RemoteRequestBuilders.java   
static HttpEntity initialSearchEntity(SearchRequest searchRequest, BytesReference query) {
    // EMPTY is safe here because we're not calling namedObject
    try (XContentBuilder entity = JsonXContent.contentBuilder();
            XContentParser queryParser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, query)) {
        entity.startObject();

        entity.field("query"); {
            /* We're intentionally a bit paranoid here - copying the query as xcontent rather than writing a raw field. We don't want
             * poorly written queries to escape. Ever. */
            entity.copyCurrentStructure(queryParser);
            XContentParser.Token shouldBeEof = queryParser.nextToken();
            if (shouldBeEof != null) {
                throw new ElasticsearchException(
                        "query was more than a single object. This first token after the object is [" + shouldBeEof + "]");
            }
        }

        if (searchRequest.source().fetchSource() != null) {
            entity.field("_source", searchRequest.source().fetchSource());
        } else {
            entity.field("_source", true);
        }

        entity.endObject();
        BytesRef bytes = entity.bytes().toBytesRef();
        return new ByteArrayEntity(bytes.bytes, bytes.offset, bytes.length, ContentType.APPLICATION_JSON);
    } catch (IOException e) {
        throw new ElasticsearchException("unexpected error building entity", e);
    }
}
项目:elasticsearch_my    文件:ESTestCase.java   
/**
 * Assert that two objects are equals, calling {@link ToXContent#toXContent(XContentBuilder, ToXContent.Params)} to print out their
 * differences if they aren't equal.
 */
public static <T extends ToXContent> void assertEqualsWithErrorMessageFromXContent(T expected, T actual) {
    if (Objects.equals(expected, actual)) {
        return;
    }
    if (expected == null) {
        throw new AssertionError("Expected null be actual was [" + actual.toString() + "]");
    }
    if (actual == null) {
        throw new AssertionError("Didn't expect null but actual was [null]");
    }
    try (XContentBuilder actualJson = JsonXContent.contentBuilder();
            XContentBuilder expectedJson = JsonXContent.contentBuilder()) {
        actualJson.startObject();
        actual.toXContent(actualJson, ToXContent.EMPTY_PARAMS);
        actualJson.endObject();
        expectedJson.startObject();
        expected.toXContent(expectedJson, ToXContent.EMPTY_PARAMS);
        expectedJson.endObject();
        NotEqualMessageBuilder message = new NotEqualMessageBuilder();
        message.compareMaps(
                XContentHelper.convertToMap(actualJson.bytes(), false).v2(),
                XContentHelper.convertToMap(expectedJson.bytes(), false).v2());
        throw new AssertionError("Didn't match expected value:\n" + message);
    } catch (IOException e) {
        throw new AssertionError("IOException while building failure message", e);
    }
}
项目:elasticsearch_my    文件:ESClientYamlSuiteTestCase.java   
@Override
protected void afterIfFailed(List<Throwable> errors) {
    // Dump the stash on failure. Instead of dumping it in true json we escape `\n`s so stack traces are easier to read
    logger.info("Stash dump on failure [{}]",
            XContentHelper.toString(restTestExecutionContext.stash()).replace("\\n", "\n").replace("\\r", "\r").replace("\\t", "\t"));
    super.afterIfFailed(errors);
}
项目:elasticsearch_my    文件:XContentTestUtils.java   
public static Map<String, Object> convertToMap(ToXContent part) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    part.toXContent(builder, EMPTY_PARAMS);
    builder.endObject();
    return XContentHelper.convertToMap(builder.bytes(), false, builder.contentType()).v2();
}
项目:elasticsearch_my    文件:TaskResult.java   
public XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
    builder.field("completed", completed);
    builder.startObject("task");
    task.toXContent(builder, params);
    builder.endObject();
    if (error != null) {
        XContentHelper.writeRawField("error", error, builder, params);
    }
    if (response != null) {
        XContentHelper.writeRawField("response", response, builder, params);
    }
    return builder;
}
项目:elasticsearch_my    文件:SearchHit.java   
/**
 * The source of the document as string (can be <tt>null</tt>).
 */
public String getSourceAsString() {
    if (source == null) {
        return null;
    }
    try {
        return XContentHelper.convertToJson(getSourceRef(), false);
    } catch (IOException e) {
        throw new ElasticsearchParseException("failed to convert source to a json string");
    }
}
项目:elasticsearch_my    文件:GeoShapeQueryBuilder.java   
/**
 * Fetches the Shape with the given ID in the given type and index.
 *
 * @param getRequest
 *            GetRequest containing index, type and id
 * @param path
 *            Name or path of the field in the Shape Document where the
 *            Shape itself is located
 * @return Shape with the given ID
 * @throws IOException
 *             Can be thrown while parsing the Shape Document and extracting
 *             the Shape
 */
private ShapeBuilder fetch(Client client, GetRequest getRequest, String path) throws IOException {
    if (ShapesAvailability.JTS_AVAILABLE == false) {
        throw new IllegalStateException("JTS not available");
    }
    getRequest.preference("_local");
    getRequest.operationThreaded(false);
    GetResponse response = client.get(getRequest).actionGet();
    if (!response.isExists()) {
        throw new IllegalArgumentException("Shape with ID [" + getRequest.id() + "] in type [" + getRequest.type() + "] not found");
    }
    if (response.isSourceEmpty()) {
        throw new IllegalArgumentException("Shape with ID [" + getRequest.id() + "] in type [" + getRequest.type() +
                "] source disabled");
    }

    String[] pathElements = path.split("\\.");
    int currentPathSlot = 0;

    // It is safe to use EMPTY here because this never uses namedObject
    try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, response.getSourceAsBytesRef())) {
        XContentParser.Token currentToken;
        while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (currentToken == XContentParser.Token.FIELD_NAME) {
                if (pathElements[currentPathSlot].equals(parser.currentName())) {
                    parser.nextToken();
                    if (++currentPathSlot == pathElements.length) {
                        return ShapeBuilder.parse(parser);
                    }
                } else {
                    parser.nextToken();
                    parser.skipChildren();
                }
            }
        }
        throw new IllegalStateException("Shape with name [" + getRequest.id() + "] found but missing " + path + " field");
    }
}
项目:elasticsearch_my    文件:DocumentMapperParser.java   
public DocumentMapper parse(@Nullable String type, CompressedXContent source, String defaultSource) throws MapperParsingException {
    Map<String, Object> mapping = null;
    if (source != null) {
        Map<String, Object> root = XContentHelper.convertToMap(source.compressedReference(), true, XContentType.JSON).v2();
        Tuple<String, Map<String, Object>> t = extractMapping(type, root);
        type = t.v1();
        mapping = t.v2();
    }
    if (mapping == null) {
        mapping = new HashMap<>();
    }
    return parse(type, mapping, defaultSource);
}
项目:elasticsearch_my    文件:AliasValidator.java   
/**
 * Allows to partially validate an alias, without knowing which index it'll get applied to.
 * Useful with index templates containing aliases. Checks also that it is possible to parse
 * the alias filter via {@link org.elasticsearch.common.xcontent.XContentParser},
 * without validating it as a filter though.
 * @throws IllegalArgumentException if the alias is not valid
 */
public void validateAliasStandalone(Alias alias) {
    validateAliasStandalone(alias.name(), alias.indexRouting());
    if (Strings.hasLength(alias.filter())) {
        try {
            XContentHelper.convertToMap(XContentFactory.xContent(alias.filter()), alias.filter(), false);
        } catch (Exception e) {
            throw new IllegalArgumentException("failed to parse filter for alias [" + alias.name() + "]", e);
        }
    }
}
项目:elasticsearch_my    文件:MappingMetaData.java   
public MappingMetaData(CompressedXContent mapping) throws IOException {
    this.source = mapping;
    Map<String, Object> mappingMap = XContentHelper.convertToMap(mapping.compressedReference(), true).v2();
    if (mappingMap.size() != 1) {
        throw new IllegalStateException("Can't derive type from mapping, no root type: " + mapping.string());
    }
    this.type = mappingMap.keySet().iterator().next();
    initMappers((Map<String, Object>) mappingMap.get(this.type));
}
项目:elasticsearch_my    文件:MappingMetaData.java   
/**
 * Converts the serialized compressed form of the mappings into a parsed map.
 */
public Map<String, Object> sourceAsMap() throws IOException {
    Map<String, Object> mapping = XContentHelper.convertToMap(source.compressedReference(), true).v2();
    if (mapping.size() == 1 && mapping.containsKey(type())) {
        // the type name is the root value, reduce it
        mapping = (Map<String, Object>) mapping.get(type());
    }
    return mapping;
}
项目:elasticsearch_my    文件:JsonXContentGenerator.java   
@Override
public void copyCurrentStructure(XContentParser parser) throws IOException {
    // the start of the parser
    if (parser.currentToken() == null) {
        parser.nextToken();
    }
    if (parser instanceof JsonXContentParser) {
        generator.copyCurrentStructure(((JsonXContentParser) parser).parser);
    } else {
        XContentHelper.copyCurrentStructure(this, parser);
    }
}
项目:elasticsearch_my    文件:PutMappingRequest.java   
/**
 * The mapping source definition.
 */
public PutMappingRequest source(BytesReference mappingSource, XContentType xContentType) {
    Objects.requireNonNull(xContentType);
    try {
        this.source = XContentHelper.convertToJson(mappingSource, false, false, xContentType);
        return this;
    } catch (IOException e) {
        throw new UncheckedIOException("failed to convert source to json", e);
    }
}
项目:elasticsearch_my    文件:PutMappingRequest.java   
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    indices = in.readStringArray();
    indicesOptions = IndicesOptions.readIndicesOptions(in);
    type = in.readOptionalString();
    source = in.readString();
    if (in.getVersion().before(Version.V_6_0_0_alpha1_UNRELEASED)) { // TODO change to V_5_3 once backported
        // we do not know the format from earlier versions so convert if necessary
        source = XContentHelper.convertToJson(new BytesArray(source), false, false, XContentFactory.xContentType(source));
    }
    updateAllTypes = in.readBoolean();
    readTimeout(in);
    concreteIndex = in.readOptionalWriteable(Index::new);
}