当前位置: 首页>>代码示例>>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;未经允许,请勿转载。