當前位置: 首頁>>代碼示例>>Java>>正文


Java JsonXContent類代碼示例

本文整理匯總了Java中org.elasticsearch.common.xcontent.json.JsonXContent的典型用法代碼示例。如果您正苦於以下問題:Java JsonXContent類的具體用法?Java JsonXContent怎麽用?Java JsonXContent使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


JsonXContent類屬於org.elasticsearch.common.xcontent.json包,在下文中一共展示了JsonXContent類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: sendResponse

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
protected void sendResponse(final RestChannel channel, final Map<String, Object> params, final boolean pretty) {
    try {
        final XContentBuilder builder = JsonXContent.contentBuilder();
        if (pretty) {
            builder.prettyPrint();
        }
        builder.startObject();
        builder.field("acknowledged", true);
        if (params != null) {
            for (final Map.Entry<String, Object> entry : params.entrySet()) {
                builder.field(entry.getKey(), entry.getValue());
            }
        }
        builder.endObject();
        channel.sendResponse(new BytesRestResponse(OK, builder));
    } catch (final IOException e) {
        throw new ElasticsearchException("Failed to create a resposne.", e);
    }
}
 
開發者ID:codelibs,項目名稱:elasticsearch-indexing-proxy,代碼行數:20,代碼來源:RestIndexingProxyProcessAction.java

示例2: execute

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
@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);
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:17,代碼來源:JsonProcessor.java

示例3: testTemplateQueryAsEscapedStringStartingWithConditionalClause

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
/**
 * Test that template can contain conditional clause. In this case it is at
 * the beginning of the string.
 */
public void testTemplateQueryAsEscapedStringStartingWithConditionalClause() throws Exception {
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("_all");
    String templateString =
              "{"
            + "  \"inline\" : \"{ {{#use_size}} \\\"size\\\": \\\"{{size}}\\\", {{/use_size}} \\\"query\\\":{\\\"match_all\\\":{}}}\","
            + "  \"params\":{"
            + "    \"size\": 1,"
            + "    \"use_size\": true"
            + "  }"
            + "}";
    SearchTemplateRequest request = RestSearchTemplateAction.parse(createParser(JsonXContent.jsonXContent, templateString));
    request.setRequest(searchRequest);
    SearchTemplateResponse searchResponse = client().execute(SearchTemplateAction.INSTANCE, request).get();
    assertThat(searchResponse.getResponse().getHits().getHits().length, equalTo(1));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:21,代碼來源:SearchTemplateIT.java

示例4: testPipelineQueryParameterIsError

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testPipelineQueryParameterIsError() throws IOException {
    RestReindexAction action = new RestReindexAction(Settings.EMPTY, mock(RestController.class));

    FakeRestRequest.Builder request = new FakeRestRequest.Builder(xContentRegistry());
    try (XContentBuilder body = JsonXContent.contentBuilder().prettyPrint()) {
        body.startObject(); {
            body.startObject("source"); {
                body.field("index", "source");
            }
            body.endObject();
            body.startObject("dest"); {
                body.field("index", "dest");
            }
            body.endObject();
        }
        body.endObject();
        request.withContent(body.bytes(), body.contentType());
    }
    request.withParams(singletonMap("pipeline", "doesn't matter"));
    Exception e = expectThrows(IllegalArgumentException.class, () -> action.buildRequest(request.build()));

    assertEquals("_reindex doesn't support [pipeline] as a query parmaeter. Specify it in the [dest] object instead.", e.getMessage());
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:24,代碼來源:RestReindexActionTests.java

示例5: toString

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
/**
 * Return a {@link String} that is the json representation of the provided {@link ToXContent}.
 * Wraps the output into an anonymous object.
 */
public static String toString(ToXContent toXContent) {
    try {
        XContentBuilder builder = JsonXContent.contentBuilder();
        if (toXContent.isFragment()) {
            builder.startObject();
        }
        toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS);
        if (toXContent.isFragment()) {
            builder.endObject();
        }
        return builder.string();
    } catch (IOException e) {
        return "Error building toString out of XContent: " + ExceptionsHelper.stackTrace(e);
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:20,代碼來源:Strings.java

示例6: testXContent

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testXContent() throws IOException {
    IndexId indexId = new IndexId(randomAsciiOfLength(8), UUIDs.randomBase64UUID());
    XContentBuilder builder = JsonXContent.contentBuilder();
    indexId.toXContent(builder, ToXContent.EMPTY_PARAMS);
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
    String name = null;
    String id = null;
    while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
        final String currentFieldName = parser.currentName();
        parser.nextToken();
        if (currentFieldName.equals(IndexId.NAME)) {
            name = parser.text();
        } else if (currentFieldName.equals(IndexId.ID)) {
            id = parser.text();
        }
    }
    assertNotNull(name);
    assertNotNull(id);
    assertEquals(indexId, new IndexId(name, id));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:22,代碼來源:IndexIdTests.java

示例7: testFailWithSubAgg

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testFailWithSubAgg() throws Exception {
    String source = "{\n" +
        "    \"top-tags\": {\n" +
        "      \"terms\": {\n" +
        "        \"field\": \"tags\"\n" +
        "      },\n" +
        "      \"aggs\": {\n" +
        "        \"top_tags_hits\": {\n" +
        "          \"top_hits\": {},\n" +
        "          \"aggs\": {\n" +
        "            \"max\": {\n" +
        "              \"max\": {\n" +
        "                \"field\": \"age\"\n" +
        "              }\n" +
        "            }\n" +
        "          }\n" +
        "        }\n" +
        "      }\n" +
        "    }\n" +
        "}";
    XContentParser parser = createParser(JsonXContent.jsonXContent, source);
    QueryParseContext parseContext = new QueryParseContext(parser);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(AggregationInitializationException.class, () -> AggregatorFactories.parseAggregators(parseContext));
    assertThat(e.toString(), containsString("Aggregator [top_tags_hits] of type [top_hits] cannot accept sub-aggregations"));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:27,代碼來源:TopHitsTests.java

示例8: testSettingsFiltering

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testSettingsFiltering() throws IOException {
    AzureRepositoryPlugin p = new AzureRepositoryPlugin();
    SettingsModule module = new SettingsModule(Settings.EMPTY, p.getSettings(), p.getSettingsFilter());
    SettingsFilter settingsFilter = ModuleTestCase.bindAndGetInstance(module, SettingsFilter.class);

    // Test using direct filtering
    Settings filteredSettings = settingsFilter.filter(settings);
    assertThat(filteredSettings.getAsMap().keySet(), contains("cloud.azure.storage.azure1.default"));

    // Test using toXContent filtering
    RestRequest request = new FakeRestRequest();
    settingsFilter.addFilterSettingParams(request);
    XContentBuilder xContentBuilder = XContentBuilder.builder(JsonXContent.jsonXContent);
    xContentBuilder.startObject();
    settings.toXContent(xContentBuilder, request);
    xContentBuilder.endObject();
    String filteredSettingsString = xContentBuilder.string();
    filteredSettings = Settings.builder().loadFromSource(filteredSettingsString, xContentBuilder.contentType()).build();
    assertThat(filteredSettings.getAsMap().keySet(), contains("cloud.azure.storage.azure1.default"));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:21,代碼來源:AzureStorageSettingsFilterTests.java

示例9: testXContentRoundTrip

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testXContentRoundTrip() throws Exception {
    ExtendedBounds orig = randomExtendedBounds();

    try (XContentBuilder out = JsonXContent.contentBuilder()) {
        out.startObject();
        orig.toXContent(out, ToXContent.EMPTY_PARAMS);
        out.endObject();

        try (XContentParser in = createParser(JsonXContent.jsonXContent, out.bytes())) {
            XContentParser.Token token = in.currentToken();
            assertNull(token);

            token = in.nextToken();
            assertThat(token, equalTo(XContentParser.Token.START_OBJECT));

            token = in.nextToken();
            assertThat(token, equalTo(XContentParser.Token.FIELD_NAME));
            assertThat(in.currentName(), equalTo(ExtendedBounds.EXTENDED_BOUNDS_FIELD.getPreferredName()));

            ExtendedBounds read = ExtendedBounds.PARSER.apply(in, null);
            assertEquals(orig, read);
        } catch (Exception e) {
            throw new Exception("Error parsing [" + out.bytes().utf8ToString() + "]", e);
        }
    }
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:27,代碼來源:ExtendedBoundsTests.java

示例10: testTwoTypes

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testTwoTypes() throws Exception {
    XContentBuilder source = JsonXContent.contentBuilder()
            .startObject()
                .startObject("in_stock")
                    .startObject("filter")
                        .startObject("range")
                            .startObject("stock")
                                .field("gt", 0)
                            .endObject()
                        .endObject()
                    .endObject()
                    .startObject("terms")
                        .field("field", "stock")
                    .endObject()
                .endObject()
            .endObject();
    XContentParser parser = createParser(source);
    QueryParseContext parseContext = new QueryParseContext(parser);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parseContext));
    assertThat(e.toString(), containsString("Found two aggregation type definitions in [in_stock]: [filter] and [terms]"));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:23,代碼來源:AggregatorFactoriesTests.java

示例11: testTwoAggs

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testTwoAggs() throws Exception {
    assumeFalse("Test only makes sense if XContent parser doesn't have strict duplicate checks enabled",
        XContent.isStrictDuplicateDetectionEnabled());
    XContentBuilder source = JsonXContent.contentBuilder()
            .startObject()
                .startObject("by_date")
                    .startObject("date_histogram")
                        .field("field", "timestamp")
                        .field("interval", "month")
                    .endObject()
                    .startObject("aggs")
                        .startObject("tag_count")
                            .startObject("cardinality")
                                .field("field", "tag")
                            .endObject()
                        .endObject()
                    .endObject()
                    .startObject("aggs") // 2nd "aggs": illegal
                        .startObject("tag_count2")
                            .startObject("cardinality")
                                .field("field", "tag")
                            .endObject()
                        .endObject()
                    .endObject()
                .endObject()
            .endObject();
    XContentParser parser = createParser(source);
    QueryParseContext parseContext = new QueryParseContext(parser);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parseContext));
    assertThat(e.toString(), containsString("Found two sub aggregation definitions under [by_date]"));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:33,代碼來源:AggregatorFactoriesTests.java

示例12: testSameAggregationName

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testSameAggregationName() throws Exception {
    assumeFalse("Test only makes sense if XContent parser doesn't have strict duplicate checks enabled",
        XContent.isStrictDuplicateDetectionEnabled());
    final String name = randomAsciiOfLengthBetween(1, 10);
    XContentBuilder source = JsonXContent.contentBuilder()
            .startObject()
                .startObject(name)
                    .startObject("terms")
                        .field("field", "a")
                    .endObject()
                .endObject()
                .startObject(name)
                    .startObject("terms")
                        .field("field", "b")
                    .endObject()
                .endObject()
            .endObject();
    XContentParser parser = createParser(source);
    QueryParseContext parseContext = new QueryParseContext(parser);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parseContext));
    assertThat(e.toString(), containsString("Two sibling aggregations cannot have the same name: [" + name + "]"));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:24,代碼來源:AggregatorFactoriesTests.java

示例13: testMissingName

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testMissingName() throws Exception {
    XContentBuilder source = JsonXContent.contentBuilder()
            .startObject()
                .startObject("by_date")
                    .startObject("date_histogram")
                        .field("field", "timestamp")
                        .field("interval", "month")
                    .endObject()
                    .startObject("aggs")
                        // the aggregation name is missing
                        //.startObject("tag_count")
                        .startObject("cardinality")
                            .field("field", "tag")
                        .endObject()
                        //.endObject()
                    .endObject()
                .endObject()
            .endObject();
    XContentParser parser = createParser(source);
    QueryParseContext parseContext = new QueryParseContext(parser);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parseContext));
    assertThat(e.toString(), containsString("Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [cardinality]"));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:25,代碼來源:AggregatorFactoriesTests.java

示例14: testMissingType

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testMissingType() throws Exception {
    XContentBuilder source = JsonXContent.contentBuilder()
            .startObject()
                .startObject("by_date")
                    .startObject("date_histogram")
                        .field("field", "timestamp")
                        .field("interval", "month")
                    .endObject()
                    .startObject("aggs")
                        .startObject("tag_count")
                            // the aggregation type is missing
                            //.startObject("cardinality")
                            .field("field", "tag")
                            //.endObject()
                        .endObject()
                    .endObject()
                .endObject()
            .endObject();
    XContentParser parser = createParser(source);
    QueryParseContext parseContext = new QueryParseContext(parser);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parseContext));
    assertThat(e.toString(), containsString("Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [tag_count]"));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:25,代碼來源:AggregatorFactoriesTests.java

示例15: testQueryContextParsingArray

import org.elasticsearch.common.xcontent.json.JsonXContent; //導入依賴的package包/類
public void testQueryContextParsingArray() throws Exception {
    XContentBuilder builder = jsonBuilder().startArray()
                .value("context1")
                .value("context2")
            .endArray();
    XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes());
    CategoryContextMapping mapping = ContextBuilder.category("cat").build();
    List<ContextMapping.InternalQueryContext> internalQueryContexts = mapping.parseQueryContext(createParseContext(parser));
    assertThat(internalQueryContexts.size(), equalTo(2));
    assertThat(internalQueryContexts.get(0).context, equalTo("context1"));
    assertThat(internalQueryContexts.get(0).boost, equalTo(1));
    assertThat(internalQueryContexts.get(0).isPrefix, equalTo(false));
    assertThat(internalQueryContexts.get(1).context, equalTo("context2"));
    assertThat(internalQueryContexts.get(1).boost, equalTo(1));
    assertThat(internalQueryContexts.get(1).isPrefix, equalTo(false));
}
 
開發者ID:justor,項目名稱:elasticsearch_my,代碼行數:17,代碼來源:CategoryContextMappingTests.java


注:本文中的org.elasticsearch.common.xcontent.json.JsonXContent類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。