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

项目:mapr-music    文件:MaprMusicElasticSearchService.java   
private void indexJSONTableDocuments(TransportClient client, String indexName, String typeName, String tablePath, String... fields) {

        loginTestUser(TEST_USER_NAME, TEST_USER_GROUP);

        // Create an OJAI connection to MapR cluster
        Connection connection = DriverManager.getConnection(CONNECTION_URL);

        // Get an instance of OJAI DocumentStore
        final DocumentStore store = connection.getStore(tablePath);

        DocumentStream documentStream = store.find(fields);
        for (Document document : documentStream) {

            IndexResponse response = client.prepareIndex(indexName, typeName, document.getId().getString())
                    .setSource(document.asJsonString(), XContentType.JSON)
                    .get();

            log.info("Elasticsearch Index Response: '{}'", response);
        }

        // Close this instance of OJAI DocumentStore
        store.close();

        // Close the OJAI connection and release any resources held by the connection
        connection.close();
    }
项目:elasticsearch_my    文件:ClientYamlTestResponse.java   
ClientYamlTestResponse(Response response) throws IOException {
    this.response = response;
    if (response.getEntity() != null) {
        String contentType = response.getHeader("Content-Type");
        this.bodyContentType = XContentType.fromMediaTypeOrFormat(contentType);
        try {
            byte[] bytes = EntityUtils.toByteArray(response.getEntity());
            //skip parsing if we got text back (e.g. if we called _cat apis)
            if (bodyContentType != null) {
                this.parsedResponse = ObjectPath.createFromXContent(bodyContentType.xContent(), new BytesArray(bytes));
            }
            this.body = bytes;
        } catch (IOException e) {
            EntityUtils.consumeQuietly(response.getEntity());
            throw e;
        }
    } else {
        this.body = null;
        this.bodyContentType = null;
    }
}
项目:elasticsearch_my    文件:BulkRequestTests.java   
public void testSimpleBulk4() throws Exception {
    String bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk4.json");
    BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.add(bulkAction.getBytes(StandardCharsets.UTF_8), 0, bulkAction.length(), null, null, XContentType.JSON);
    assertThat(bulkRequest.numberOfActions(), equalTo(4));
    assertThat(((UpdateRequest) bulkRequest.requests().get(0)).id(), equalTo("1"));
    assertThat(((UpdateRequest) bulkRequest.requests().get(0)).retryOnConflict(), equalTo(2));
    assertThat(((UpdateRequest) bulkRequest.requests().get(0)).doc().source().utf8ToString(), equalTo("{\"field\":\"value\"}"));
    assertThat(((UpdateRequest) bulkRequest.requests().get(1)).id(), equalTo("0"));
    assertThat(((UpdateRequest) bulkRequest.requests().get(1)).type(), equalTo("type1"));
    assertThat(((UpdateRequest) bulkRequest.requests().get(1)).index(), equalTo("index1"));
    Script script = ((UpdateRequest) bulkRequest.requests().get(1)).script();
    assertThat(script, notNullValue());
    assertThat(script.getIdOrCode(), equalTo("counter += param1"));
    assertThat(script.getLang(), equalTo("javascript"));
    Map<String, Object> scriptParams = script.getParams();
    assertThat(scriptParams, notNullValue());
    assertThat(scriptParams.size(), equalTo(1));
    assertThat(((Integer) scriptParams.get("param1")), equalTo(1));
    assertThat(((UpdateRequest) bulkRequest.requests().get(1)).upsertRequest().source().utf8ToString(), equalTo("{\"counter\":1}"));
}
项目:elasticsearch_my    文件:SearchAfterBuilderTests.java   
public void testFromXContent() throws Exception {
    for (int runs = 0; runs < 20; runs++) {
        SearchAfterBuilder searchAfterBuilder = randomJsonSearchFromBuilder();
        XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
        if (randomBoolean()) {
            builder.prettyPrint();
        }
        builder.startObject();
        searchAfterBuilder.innerToXContent(builder);
        builder.endObject();
        XContentParser parser = createParser(shuffleXContent(builder));
        parser.nextToken();
        parser.nextToken();
        parser.nextToken();
        SearchAfterBuilder secondSearchAfterBuilder = SearchAfterBuilder.fromXContent(parser);
        assertNotSame(searchAfterBuilder, secondSearchAfterBuilder);
        assertEquals(searchAfterBuilder, secondSearchAfterBuilder);
        assertEquals(searchAfterBuilder.hashCode(), secondSearchAfterBuilder.hashCode());
    }
}
项目:elasticsearch_my    文件:SearchSortValuesTests.java   
public void testFromXContent() throws IOException {
    SearchSortValues sortValues = createTestItem();
    XContentType xcontentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(sortValues, xcontentType, humanReadable);

    SearchSortValues parsed;
    try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
        parser.nextToken(); // skip to the elements start array token, fromXContent advances from there if called
        parser.nextToken();
        parser.nextToken();
        parsed = SearchSortValues.fromXContent(parser);
        parser.nextToken();
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xcontentType, humanReadable), xcontentType);
}
项目:elasticsearch_my    文件:SimpleQueryStringIT.java   
public void testEmptySimpleQueryStringWithAnalysis() throws Exception {
    // https://github.com/elastic/elasticsearch/issues/18202
    String mapping = XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type1")
            .startObject("properties")
            .startObject("body")
            .field("type", "text")
            .field("analyzer", "stop")
            .endObject()
            .endObject()
            .endObject()
            .endObject().string();

    CreateIndexRequestBuilder mappingRequest = client().admin().indices()
            .prepareCreate("test1")
            .addMapping("type1", mapping, XContentType.JSON);
    mappingRequest.execute().actionGet();
    indexRandom(true, client().prepareIndex("test1", "type1", "1").setSource("body", "Some Text"));
    refresh();

    SearchResponse searchResponse = client().prepareSearch()
            .setQuery(simpleQueryStringQuery("the*").field("body")).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 0L);
}
项目:elasticsearch_my    文件:GetFieldTests.java   
public void testToAndFromXContent() throws Exception {
    XContentType xContentType = randomFrom(XContentType.values());
    Tuple<GetField, GetField> tuple = randomGetField(xContentType);
    GetField getField = tuple.v1();
    GetField expectedGetField = tuple.v2();
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(getField, xContentType, humanReadable);
    //test that we can parse what we print out
    GetField parsedGetField;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        //we need to move to the next token, the start object one that we manually added is not expected
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
        parsedGetField = GetField.fromXContent(parser);
        assertEquals(XContentParser.Token.END_ARRAY, parser.currentToken());
        assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
        assertNull(parser.nextToken());
    }
    assertEquals(expectedGetField, parsedGetField);
    BytesReference finalBytes = toXContent(parsedGetField, xContentType, humanReadable);
    assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
}
项目:elasticsearch_my    文件:HighlightBuilderTests.java   
/**
 *  creates random highlighter, renders it to xContent and back to new instance that should be equal to original
 */
public void testFromXContent() throws IOException {
    for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
        HighlightBuilder highlightBuilder = randomHighlighterBuilder();
        XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
        if (randomBoolean()) {
            builder.prettyPrint();
        }
        highlightBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
        XContentBuilder shuffled = shuffleXContent(builder);

        XContentParser parser = createParser(shuffled);
        QueryParseContext context = new QueryParseContext(parser);
        parser.nextToken();
        HighlightBuilder secondHighlightBuilder;
        try {
            secondHighlightBuilder = HighlightBuilder.fromXContent(context);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error parsing " + highlightBuilder, e);
        }
        assertNotSame(highlightBuilder, secondHighlightBuilder);
        assertEquals(highlightBuilder, secondHighlightBuilder);
        assertEquals(highlightBuilder.hashCode(), secondHighlightBuilder.hashCode());
    }
}
项目:elasticsearch_my    文件:PipelineStoreTests.java   
public void testValidate() throws Exception {
    PutPipelineRequest putRequest = new PutPipelineRequest("_id", new BytesArray(
            "{\"processors\": [{\"set\" : {\"field\": \"_field\", \"value\": \"_value\", \"tag\": \"tag1\"}}," +
                "{\"remove\" : {\"field\": \"_field\", \"tag\": \"tag2\"}}]}"),
        XContentType.JSON);

    DiscoveryNode node1 = new DiscoveryNode("_node_id1", buildNewFakeTransportAddress(),
            emptyMap(), emptySet(), Version.CURRENT);
    DiscoveryNode node2 = new DiscoveryNode("_node_id2", buildNewFakeTransportAddress(),
            emptyMap(), emptySet(), Version.CURRENT);
    Map<DiscoveryNode, IngestInfo> ingestInfos = new HashMap<>();
    ingestInfos.put(node1, new IngestInfo(Arrays.asList(new ProcessorInfo("set"), new ProcessorInfo("remove"))));
    ingestInfos.put(node2, new IngestInfo(Arrays.asList(new ProcessorInfo("set"))));

    ElasticsearchParseException e =
        expectThrows(ElasticsearchParseException.class, () -> store.validatePipeline(ingestInfos, putRequest));
    assertEquals("Processor type [remove] is not installed on node [" + node2 + "]", e.getMessage());
    assertEquals("remove", e.getHeader("processor_type").get(0));
    assertEquals("tag2", e.getHeader("processor_tag").get(0));

    ingestInfos.put(node2, new IngestInfo(Arrays.asList(new ProcessorInfo("set"), new ProcessorInfo("remove"))));
    store.validatePipeline(ingestInfos, putRequest);
}
项目:elasticsearch_my    文件:MoreLikeThisQueryBuilderTests.java   
public void testItemSerializationBwc() throws IOException {
    final byte[] data = Base64.getDecoder().decode("AQVpbmRleAEEdHlwZQEODXsiZm9vIjoiYmFyIn0A/wD//////////QAAAAAAAAAA");
    final Version version = randomFrom(Version.V_5_0_0, Version.V_5_0_1, Version.V_5_0_2,
        Version.V_5_0_3_UNRELEASED, Version.V_5_1_1_UNRELEASED, Version.V_5_1_2_UNRELEASED, Version.V_5_2_0_UNRELEASED);
    try (StreamInput in = StreamInput.wrap(data)) {
        in.setVersion(version);
        Item item = new Item(in);
        assertEquals(XContentType.JSON, item.xContentType());
        assertEquals("{\"foo\":\"bar\"}", item.doc().utf8ToString());
        assertEquals("index", item.index());
        assertEquals("type", item.type());

        try (BytesStreamOutput out = new BytesStreamOutput()) {
            out.setVersion(version);
            item.writeTo(out);
            assertArrayEquals(data, out.bytes().toBytesRef().bytes);
        }
    }
}
项目:elasticsearch_my    文件:UpdateMappingIntegrationIT.java   
public void testUpdateMappingWithoutTypeMultiObjects() throws Exception {
    client().admin().indices().prepareCreate("test")
            .setSettings(
                    Settings.builder()
                            .put("index.number_of_shards", 1)
                            .put("index.number_of_replicas", 0)
            ).execute().actionGet();
    client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();

    PutMappingResponse putMappingResponse = client().admin().indices().preparePutMapping("test").setType("doc")
            .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}", XContentType.JSON)
            .execute().actionGet();

    assertThat(putMappingResponse.isAcknowledged(), equalTo(true));

    GetMappingsResponse getMappingsResponse = client().admin().indices().prepareGetMappings("test").execute().actionGet();
    assertThat(getMappingsResponse.mappings().get("test").get("doc").source().toString(),
            equalTo("{\"doc\":{\"properties\":{\"date\":{\"type\":\"integer\"}}}}"));
}
项目:elasticsearch_my    文件:IndexRequest.java   
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    type = in.readOptionalString();
    id = in.readOptionalString();
    routing = in.readOptionalString();
    parent = in.readOptionalString();
    if (in.getVersion().before(Version.V_6_0_0_alpha1_UNRELEASED)) {
        in.readOptionalString(); // timestamp
        in.readOptionalWriteable(TimeValue::new); // ttl
    }
    source = in.readBytesReference();
    opType = OpType.fromId(in.readByte());
    version = in.readLong();
    versionType = VersionType.fromValue(in.readByte());
    pipeline = in.readOptionalString();
    isRetry = in.readBoolean();
    autoGeneratedTimestamp = in.readLong();
    if (in.getVersion().onOrAfter(Version.V_5_3_0_UNRELEASED)) {
        contentType = in.readOptionalWriteable(XContentType::readFrom);
    } else {
        contentType = XContentFactory.xContentType(source);
    }
}
项目:elasticsearch_my    文件:RequestTests.java   
public void testEnforceSameContentType() {
    XContentType xContentType = randomFrom(XContentType.JSON, XContentType.SMILE);
    IndexRequest indexRequest = new IndexRequest().source(singletonMap("field", "value"), xContentType);
    assertEquals(xContentType, enforceSameContentType(indexRequest, null));
    assertEquals(xContentType, enforceSameContentType(indexRequest, xContentType));

    XContentType bulkContentType = randomBoolean() ? xContentType : null;

    IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () ->
            enforceSameContentType(new IndexRequest().source(singletonMap("field", "value"), XContentType.CBOR), bulkContentType));
    assertEquals("Unsupported content-type found for request with content-type [CBOR], only JSON and SMILE are supported",
            exception.getMessage());

    exception = expectThrows(IllegalArgumentException.class, () ->
            enforceSameContentType(new IndexRequest().source(singletonMap("field", "value"), XContentType.YAML), bulkContentType));
    assertEquals("Unsupported content-type found for request with content-type [YAML], only JSON and SMILE are supported",
            exception.getMessage());

    XContentType requestContentType = xContentType == XContentType.JSON ? XContentType.SMILE : XContentType.JSON;

    exception = expectThrows(IllegalArgumentException.class, () ->
            enforceSameContentType(new IndexRequest().source(singletonMap("field", "value"), requestContentType), xContentType));
    assertEquals("Mismatching content-type found for request with content-type [" + requestContentType + "], "
            + "previous requests have content-type [" + xContentType + "]", exception.getMessage());
}
项目:elasticsearch_my    文件:IndexShardTestCase.java   
protected Engine.Index indexDoc(IndexShard shard, String type, String id, String source, XContentType xContentType) throws IOException {
    final Engine.Index index;
    if (shard.routingEntry().primary()) {
        index = shard.prepareIndexOnPrimary(
            SourceToParse.source(SourceToParse.Origin.PRIMARY, shard.shardId().getIndexName(), type, id, new BytesArray(source),
                xContentType),
            Versions.MATCH_ANY,
            VersionType.INTERNAL,
            IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP,
            false);
    } else {
        index = shard.prepareIndexOnReplica(
            SourceToParse.source(SourceToParse.Origin.PRIMARY, shard.shardId().getIndexName(), type, id, new BytesArray(source),
                xContentType),
            randomInt(1 << 10), 1, VersionType.EXTERNAL, IndexRequest.UNSET_AUTO_GENERATED_TIMESTAMP, false);
    }
    shard.index(index);
    return index;
}
项目:elasticsearch_my    文件:AsyncBulkByScrollActionTests.java   
/**
 * Mimicks script failures or general wrongness by implementers.
 */
public void testBuildRequestThrowsException() throws Exception {
    DummyAsyncBulkByScrollAction action = new DummyAsyncBulkByScrollAction() {
        @Override
        protected AbstractAsyncBulkByScrollAction.RequestWrapper<?> buildRequest(Hit doc) {
            throw new RuntimeException("surprise");
        }
    };
    ScrollableHitSource.BasicHit hit = new ScrollableHitSource.BasicHit("index", "type", "id", 0);
    hit.setSource(new BytesArray("{}"), XContentType.JSON);
    ScrollableHitSource.Response response = new ScrollableHitSource.Response(false, emptyList(), 1, singletonList(hit), null);
    simulateScrollResponse(action, timeValueNanos(System.nanoTime()), 0, response);
    ExecutionException e = expectThrows(ExecutionException.class, () -> listener.get());
    assertThat(e.getCause(), instanceOf(RuntimeException.class));
    assertThat(e.getCause().getMessage(), equalTo("surprise"));
}
项目:elasticsearch_my    文件:ElasticsearchExceptionTests.java   
public void testFromXContent() throws IOException {
    final XContent xContent = randomFrom(XContentType.values()).xContent();
    XContentBuilder builder = XContentBuilder.builder(xContent)
                                                .startObject()
                                                    .field("type", "foo")
                                                    .field("reason", "something went wrong")
                                                    .field("stack_trace", "...")
                                                .endObject();

    ElasticsearchException parsed;
    try (XContentParser parser = createParser(xContent, builder.bytes())) {
        assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
        parsed = ElasticsearchException.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }

    assertNotNull(parsed);
    assertEquals(parsed.getMessage(), "Elasticsearch exception [type=foo, reason=something went wrong, stack_trace=...]");
}
项目:elasticsearch_my    文件:SimpleQueryStringIT.java   
public void testKeywordWithWhitespace() throws Exception {
    String indexBody = copyToStringFromClasspath("/org/elasticsearch/search/query/all-query-index.json");
    prepareCreate("test").setSource(indexBody, XContentType.JSON).get();
    ensureGreen("test");

    List<IndexRequestBuilder> reqs = new ArrayList<>();
    reqs.add(client().prepareIndex("test", "doc", "1").setSource("f2", "Foo Bar"));
    reqs.add(client().prepareIndex("test", "doc", "2").setSource("f1", "bar"));
    reqs.add(client().prepareIndex("test", "doc", "3").setSource("f1", "foo bar"));
    indexRandom(true, false, reqs);

    SearchResponse resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("foo")).get();
    assertHits(resp.getHits(), "3");
    assertHitCount(resp, 1L);

    resp = client().prepareSearch("test").setQuery(simpleQueryStringQuery("bar")).get();
    assertHits(resp.getHits(), "2", "3");
    assertHitCount(resp, 2L);
}
项目:elasticsearch_my    文件:XContentBuilderTests.java   
public void testPrettyWithLfAtEnd() throws Exception {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    XContentGenerator generator = XContentFactory.xContent(XContentType.JSON).createGenerator(os);
    generator.usePrettyPrint();
    generator.usePrintLineFeedAtEnd();

    generator.writeStartObject();
    generator.writeStringField("test", "value");
    generator.writeEndObject();
    generator.flush();

    generator.close();
    // double close, and check there is no error...
    generator.close();

    byte[] bytes = os.toByteArray();
    assertThat((char) bytes[bytes.length - 1], equalTo('\n'));
}
项目:elasticsearch_my    文件:CreateIndexRequestTests.java   
public void testSerialization() throws IOException {
    CreateIndexRequest request = new CreateIndexRequest("foo");
    String mapping = JsonXContent.contentBuilder().startObject().startObject("type").endObject().endObject().string();
    request.mapping("my_type", mapping, XContentType.JSON);

    try (BytesStreamOutput output = new BytesStreamOutput()) {
        request.writeTo(output);

        try (StreamInput in = output.bytes().streamInput()) {
            CreateIndexRequest serialized = new CreateIndexRequest();
            serialized.readFrom(in);
            assertEquals(request.index(), serialized.index());
            assertEquals(mapping, serialized.mappings().get("my_type"));
        }
    }
}
项目:elasticsearch_my    文件:DirectCandidateGeneratorTests.java   
/**
 *  creates random candidate generator, renders it to xContent and back to new instance that should be equal to original
 */
public void testFromXContent() throws IOException {
    for (int runs = 0; runs < NUMBER_OF_RUNS; runs++) {
        DirectCandidateGeneratorBuilder generator = randomCandidateGenerator();
        XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
        if (randomBoolean()) {
            builder.prettyPrint();
        }
        generator.toXContent(builder, ToXContent.EMPTY_PARAMS);
        XContentParser parser = createParser(shuffleXContent(builder));
        parser.nextToken();
        DirectCandidateGeneratorBuilder secondGenerator = DirectCandidateGeneratorBuilder.PARSER.apply(parser, null);
        assertNotSame(generator, secondGenerator);
        assertEquals(generator, secondGenerator);
        assertEquals(generator.hashCode(), secondGenerator.hashCode());
    }
}
项目:elasticsearch_my    文件:SimulatePipelineRequestTests.java   
public void testSerializationWithXContentBwc() throws IOException {
    final byte[] data = Base64.getDecoder().decode("AAAAAnt9AAA=");
    final Version version = randomFrom(Version.V_5_0_0, Version.V_5_0_1, Version.V_5_0_2,
        Version.V_5_0_3_UNRELEASED, Version.V_5_1_1_UNRELEASED, Version.V_5_1_2_UNRELEASED, Version.V_5_2_0_UNRELEASED);
    try (StreamInput in = StreamInput.wrap(data)) {
        in.setVersion(version);
        SimulatePipelineRequest request = new SimulatePipelineRequest();
        request.readFrom(in);
        assertEquals(XContentType.JSON, request.getXContentType());
        assertEquals("{}", request.getSource().utf8ToString());

        try (BytesStreamOutput out = new BytesStreamOutput()) {
            out.setVersion(version);
            request.writeTo(out);
            assertArrayEquals(data, out.bytes().toBytesRef().bytes);
        }
    }
}
项目:elasticsearch_my    文件:ParsedDocument.java   
public ParsedDocument(Field version,
                      SeqNoFieldMapper.SequenceID seqID,
                      String id,
                      String type,
                      String routing,
                      List<Document> documents,
                      BytesReference source,
                      XContentType xContentType,
                      Mapping dynamicMappingsUpdate) {
    this.version = version;
    this.seqID = seqID;
    this.id = id;
    this.type = type;
    this.uid = Uid.createUidAsBytes(type, id);
    this.routing = routing;
    this.documents = documents;
    this.source = source;
    this.dynamicMappingsUpdate = dynamicMappingsUpdate;
    this.xContentType = xContentType;
}
项目:elasticsearch_my    文件:IndexRequestTests.java   
public void testIndexRequestXContentSerialization() throws IOException {
    IndexRequest indexRequest = new IndexRequest("foo", "bar", "1");
    indexRequest.source("{}", XContentType.JSON);
    assertEquals(XContentType.JSON, indexRequest.getContentType());

    BytesStreamOutput out = new BytesStreamOutput();
    indexRequest.writeTo(out);
    StreamInput in = StreamInput.wrap(out.bytes().toBytesRef().bytes);
    IndexRequest serialized = new IndexRequest();
    serialized.readFrom(in);
    assertEquals(XContentType.JSON, serialized.getContentType());
    assertEquals(new BytesArray("{}"), serialized.source());
}
项目:ESAuthPlugin    文件:ContentBuilder.java   
public static XContentBuilder restContentBuilder(RestRequest request)  
        throws IOException {
    XContentType contentType = XContentType  
            .fromRestContentType(request.header("Content-Type"));  
    if (contentType == null) {  
        // try and guess it from the body, if exists  
        if (request.hasContent()) {  
            contentType = XContentFactory.xContentType(request.content());  
        }  
    }  
    if (contentType == null) {  
        // default to JSON  
        contentType = XContentType.JSON;  
    }  
    BytesStreamOutput out = new BytesStreamOutput();  
    XContentBuilder builder = new XContentBuilder(  
            XContentFactory.xContent(contentType), out);  

    if (request.paramAsBoolean("pretty", false)) {  
        builder.prettyPrint();  
    }  
    String casing = request.param("case");  
    if (casing != null && "camelCase".equals(casing)) {  
        builder.fieldCaseConversion(  
                XContentBuilder.FieldCaseConversion.CAMELCASE);  
    } else {  
        builder.fieldCaseConversion(  
                XContentBuilder.FieldCaseConversion.NONE);  
    }  
    return builder;  
}
项目:servicebuilder    文件:Indexer.java   
public void index(String schema, List<T> rowTypes, Function<T, String> id) {

        int bulkSize = 2000;
        int bulkConcurrent = 5;

        Client client = indexAddon.elasticsearchAddon.getClient();
        String indexName = indexAddon.indexname;

        IndicesExistsResponse indicesExistsResponse = client.admin().indices().prepareExists(indexName).get();

        if (indicesExistsResponse.isExists()) {
            client.admin().indices().preparePutMapping(indexName).setType(indexName).setSource(schema, XContentType.JSON).get();
        } else {
            CreateIndexResponse createIndexResponse = client.admin().indices().prepareCreate(indexName).addMapping(indexName, schema, XContentType.JSON).get();
        }

        if (! isIndexingRunning(client.admin(), indexName)) {
            Map<String, String> rows = transform(rowTypes, id);

            BulkProcessor bulkRequest = bulkProcessorSupplier(client, bulkSize, indexAddon.elasticsearchAddon.isUnitTest() ? 0 : bulkConcurrent).get();
            rows.entrySet()
                    .forEach(entry ->
                            bulkRequest.add(createConverter(indexName, indexName).apply(entry.getKey(), entry.getValue()))
                    );


            try {
                boolean b = bulkRequest.awaitClose(60000, TimeUnit.SECONDS);
                System.out.println("Fra bulkRequest: " + b);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            client.admin().indices().prepareRefresh().get();
        }
    }
项目:elasticsearch_my    文件:XContentMapValuesTests.java   
@SuppressWarnings({"unchecked"})
public void testNotOmittingObjectWithNestedExcludedObject() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject()
            .startObject("obj1")
            .startObject("obj2")
            .startObject("obj3")
            .endObject()
            .endObject()
            .endObject()
            .endObject();

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

    assertThat(filteredSource.size(), equalTo(1));
    assertThat(filteredSource, hasKey("obj1"));
    assertThat(((Map) filteredSource.get("obj1")).size(), equalTo(0));

    // explicit include
    filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[]{"obj1"}, new String[]{"*.obj2"});
    assertThat(filteredSource.size(), equalTo(1));
    assertThat(filteredSource, hasKey("obj1"));
    assertThat(((Map) filteredSource.get("obj1")).size(), equalTo(0));

    // wild card include
    filteredSource = XContentMapValues.filter(mapTuple.v2(), new String[]{"*.obj2"}, new String[]{"*.obj3"});
    assertThat(filteredSource.size(), equalTo(1));
    assertThat(filteredSource, hasKey("obj1"));
    assertThat(((Map<String, Object>) filteredSource.get("obj1")), hasKey("obj2"));
    assertThat(((Map) ((Map) filteredSource.get("obj1")).get("obj2")).size(), equalTo(0));
}
项目:elasticsearch_my    文件:DateHistogramIT.java   
public void testSingleValueWithMultipleDateFormatsFromMapping() throws Exception {
    String mappingJson = jsonBuilder().startObject().startObject("type").startObject("properties").startObject("date").field("type", "date").field("format", "dateOptionalTime||dd-MM-yyyy").endObject().endObject().endObject().endObject().string();
    prepareCreate("idx2").addMapping("type", mappingJson, XContentType.JSON).execute().actionGet();
    IndexRequestBuilder[] reqs = new IndexRequestBuilder[5];
    for (int i = 0; i < reqs.length; i++) {
        reqs[i] = client().prepareIndex("idx2", "type", "" + i).setSource(jsonBuilder().startObject().field("date", "10-03-2014").endObject());
    }
    indexRandom(true, reqs);

    SearchResponse response = client().prepareSearch("idx2")
            .setQuery(matchAllQuery())
            .addAggregation(dateHistogram("date_histo")
                    .field("date")
                    .dateHistogramInterval(DateHistogramInterval.DAY))
            .execute().actionGet();

    assertSearchHits(response, "0", "1", "2", "3", "4");

    Histogram histo = response.getAggregations().get("date_histo");
    List<? extends Histogram.Bucket> buckets = histo.getBuckets();
    assertThat(buckets.size(), equalTo(1));

    DateTime key = new DateTime(2014, 3, 10, 0, 0, DateTimeZone.UTC);
    Histogram.Bucket bucket = buckets.get(0);
    assertThat(bucket, notNullValue());
    assertThat(bucket.getKeyAsString(), equalTo(getBucketKeyAsString(key)));
    assertThat(((DateTime) bucket.getKey()), equalTo(key));
    assertThat(bucket.getDocCount(), equalTo(5L));
}
项目:Elasticsearch    文件:CompressedXContent.java   
/**
 * Create a {@link CompressedXContent} out of a {@link ToXContent} instance.
 */
public CompressedXContent(ToXContent xcontent, XContentType type, ToXContent.Params params) throws IOException {
    BytesStreamOutput bStream = new BytesStreamOutput();
    OutputStream compressedStream = CompressorFactory.defaultCompressor().streamOutput(bStream);
    CRC32 crc32 = new CRC32();
    OutputStream checkedStream = new CheckedOutputStream(compressedStream, crc32);
    try (XContentBuilder builder = XContentFactory.contentBuilder(type, checkedStream)) {
        builder.startObject();
        xcontent.toXContent(builder, params);
        builder.endObject();
    }
    this.bytes = bStream.bytes().toBytes();
    this.crc32 = (int) crc32.getValue();
    assertConsistent();
}
项目:elasticsearch_my    文件:MetaDataStateFormat.java   
/**
 * Creates a new {@link MetaDataStateFormat} instance
 * @param format the format of the x-content
 */
protected MetaDataStateFormat(XContentType format, String prefix) {
    this.format = format;
    this.prefix = prefix;
    this.stateFilePattern = Pattern.compile(Pattern.quote(prefix) + "(\\d+)(" + MetaDataStateFormat.STATE_FILE_EXTENSION + ")?");

}
项目:elasticsearch_my    文件:RestControllerTests.java   
public void testDispatchRequestAddsAndFreesBytesOnError() {
    int contentLength = BREAKER_LIMIT.bytesAsInt();
    String content = randomAsciiOfLength(contentLength);
    TestRestRequest request = new TestRestRequest("/error", content, XContentType.JSON);
    AssertingChannel channel = new AssertingChannel(request, true, RestStatus.BAD_REQUEST);

    restController.dispatchRequest(request, channel, new ThreadContext(Settings.EMPTY));

    assertEquals(0, inFlightRequestsBreaker.getTrippedCount());
    assertEquals(0, inFlightRequestsBreaker.getUsed());
}
项目: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    文件:GetResultTests.java   
public void testToAndFromXContentEmbedded() throws Exception {
    XContentType xContentType = randomFrom(XContentType.values());
    Tuple<GetResult, GetResult> tuple = randomGetResult(xContentType);
    GetResult getResult = tuple.v1();

    // We don't expect to retrieve the index/type/id of the GetResult because they are not rendered
    // by the toXContentEmbedded method.
    GetResult expectedGetResult = new GetResult(null, null, null, -1,
            tuple.v2().isExists(), tuple.v2().sourceRef(), tuple.v2().getFields());

    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContentEmbedded(getResult, xContentType, humanReadable);

    // Test that we can parse the result of toXContentEmbedded()
    GetResult parsedEmbeddedGetResult;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        parsedEmbeddedGetResult = GetResult.fromXContentEmbedded(parser);
        assertNull(parser.nextToken());
    }

    assertEquals(expectedGetResult, parsedEmbeddedGetResult);
    //print the parsed object out and test that the output is the same as the original output
    BytesReference finalBytes = toXContentEmbedded(parsedEmbeddedGetResult, xContentType, humanReadable);
    assertToXContentEquivalent(originalBytes, finalBytes, xContentType);
    //check that the source stays unchanged, no shuffling of keys nor anything like that
    assertEquals(expectedGetResult.sourceAsString(), parsedEmbeddedGetResult.sourceAsString());
}
项目:elasticsearch_my    文件:SearchTemplateResponse.java   
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    if (hasResponse()) {
        response.toXContent(builder, params);
    } else {
        builder.startObject();
        //we can assume the template is always json as we convert it before compiling it
        builder.rawField("template_output", source, XContentType.JSON);
        builder.endObject();
    }
    return builder;
}
项目:elasticsearch_my    文件:RestSimulatePipelineAction.java   
@Override
public RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
    Tuple<XContentType, BytesReference> sourceTuple = restRequest.contentOrSourceParam();
    SimulatePipelineRequest request = new SimulatePipelineRequest(sourceTuple.v2(), sourceTuple.v1());
    request.setId(restRequest.param("id"));
    request.setVerbose(restRequest.paramAsBoolean("verbose", false));
    return channel -> client.admin().cluster().simulatePipeline(request, new RestToXContentListener<>(channel));
}
项目:elasticsearch_my    文件:SearchTemplateIT.java   
public void testIndexedTemplateWithArray() throws Exception {
    String multiQuery = "{\"query\":{\"terms\":{\"theField\":[\"{{#fieldParam}}\",\"{{.}}\",\"{{/fieldParam}}\"]}}}";
    assertAcked(
            client().admin().cluster().preparePutStoredScript()
                    .setLang(MustacheScriptEngineService.NAME)
                    .setId("4")
                    .setContent(jsonBuilder().startObject().field("template", multiQuery).endObject().bytes(), XContentType.JSON)
    );
    BulkRequestBuilder bulkRequestBuilder = client().prepareBulk();
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "1").setSource("{\"theField\":\"foo\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "2").setSource("{\"theField\":\"foo 2\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "3").setSource("{\"theField\":\"foo 3\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "4").setSource("{\"theField\":\"foo 4\"}", XContentType.JSON));
    bulkRequestBuilder.add(client().prepareIndex("test", "type", "5").setSource("{\"theField\":\"bar\"}", XContentType.JSON));
    bulkRequestBuilder.get();
    client().admin().indices().prepareRefresh().get();

    Map<String, Object> arrayTemplateParams = new HashMap<>();
    String[] fieldParams = {"foo", "bar"};
    arrayTemplateParams.put("fieldParam", fieldParams);

    SearchTemplateResponse searchResponse = new SearchTemplateRequestBuilder(client())
            .setRequest(new SearchRequest("test").types("type"))
            .setScript("4").setScriptType(ScriptType.STORED).setScriptParams(arrayTemplateParams)
            .get();
    assertHitCount(searchResponse.getResponse(), 5);
}
项目:elasticsearch_my    文件:JsonXContentGenerator.java   
@Override
public final void writeRawField(String name, BytesReference content, XContentType contentType) throws IOException {
    if (mayWriteRawData(contentType) == false) {
        writeFieldName(name);
        copyRawValue(content, contentType.xContent());
    } else {
        writeStartRaw(name);
        flush();
        content.writeTo(os);
        writeEndRaw();
    }
}
项目:Elasticsearch    文件:MetaDataStateFormat.java   
/**
 * Creates a new {@link MetaDataStateFormat} instance
 * @param format the format of the x-content
 */
protected MetaDataStateFormat(XContentType format, String prefix) {
    this.format = format;
    this.prefix = prefix;
    this.stateFilePattern = Pattern.compile(Pattern.quote(prefix) + "(\\d+)(" + MetaDataStateFormat.STATE_FILE_EXTENSION + ")?");

}
项目:elasticsearch_my    文件:AliasActionsTests.java   
public void testParseRemove() throws IOException {
    String[] indices = generateRandomStringArray(10, 5, false, false);
    String[] aliases = generateRandomStringArray(10, 5, false, false);
    XContentBuilder b = XContentBuilder.builder(randomFrom(XContentType.values()).xContent());
    b.startObject(); {
        b.startObject("remove"); {
            if (indices.length > 1 || randomBoolean()) {
                b.array("indices", indices);
            } else {
                b.field("index", indices[0]);
            }
            if (aliases.length > 1 || randomBoolean()) {
                b.array("aliases", aliases);
            } else {
                b.field("alias", aliases[0]);
            }
        }
        b.endObject();
    }
    b.endObject();
    b = shuffleXContent(b);
    try (XContentParser parser = createParser(b)) {
        AliasActions action = AliasActions.PARSER.apply(parser, null);
        assertEquals(AliasActions.Type.REMOVE, action.actionType());
        assertThat(action.indices(), equalTo(indices));
        assertThat(action.aliases(), equalTo(aliases));
    }
}
项目:elasticsearch_my    文件:TemplateQueryBuilderTests.java   
public void testRewriteWithInnerBoost() throws IOException {
    final TermQueryBuilder query = new TermQueryBuilder("foo", "bar").boost(2);
    QueryBuilder builder = new TemplateQueryBuilder(new Script(ScriptType.INLINE, "mockscript", query.toString(),
        Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()), Collections.emptyMap()));
    assertEquals(query, builder.rewrite(createShardContext()));

    builder = new TemplateQueryBuilder(new Script(ScriptType.INLINE, "mockscript", query.toString(),
        Collections.singletonMap(Script.CONTENT_TYPE_OPTION, XContentType.JSON.mediaType()), Collections.emptyMap())).boost(3);
    assertEquals(new BoolQueryBuilder().must(query).boost(3), builder.rewrite(createShardContext()));
}
项目:elasticsearch_my    文件:IndexAliasesIT.java   
public void testWaitForAliasCreationSingleShard() throws Exception {
    logger.info("--> creating index [test]");
    assertAcked(admin().indices().create(createIndexRequest("test").settings(Settings.builder().put("index.number_of_replicas", 0).put("index.number_of_shards", 1))).get());

    ensureGreen();

    for (int i = 0; i < 10; i++) {
        assertAcked(admin().indices().prepareAliases().addAlias("test", "alias" + i));
        client().index(indexRequest("alias" + i).type("type1").id("1").source(source("1", "test"), XContentType.JSON)).get();
    }
}