当前位置: 首页>>代码示例>>Java>>正文


Java XContentBuilder.close方法代码示例

本文整理汇总了Java中org.elasticsearch.common.xcontent.XContentBuilder.close方法的典型用法代码示例。如果您正苦于以下问题:Java XContentBuilder.close方法的具体用法?Java XContentBuilder.close怎么用?Java XContentBuilder.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.elasticsearch.common.xcontent.XContentBuilder的用法示例。


在下文中一共展示了XContentBuilder.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: filter

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
/**
 * Associates a filter to the alias
 */
public Alias filter(QueryBuilder filterBuilder) {
    if (filterBuilder == null) {
        this.filter = null;
        return this;
    }
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder();
        filterBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
        builder.close();
        this.filter = builder.string();
        return this;
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to build json for alias request", e);
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:19,代码来源:Alias.java

示例2: testXContentBuilderClosedInBuildResponse

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
public void testXContentBuilderClosedInBuildResponse() throws Exception {
    AtomicReference<XContentBuilder> builderAtomicReference = new AtomicReference<>();
    RestBuilderListener<TransportResponse.Empty> builderListener =
        new RestBuilderListener<Empty>(new FakeRestChannel(new FakeRestRequest(), randomBoolean(), 1)) {
            @Override
            public RestResponse buildResponse(Empty empty, XContentBuilder builder) throws Exception {
                builderAtomicReference.set(builder);
                builder.close();
                return new BytesRestResponse(RestStatus.OK, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY);
            }
    };

    builderListener.buildResponse(Empty.INSTANCE);
    assertNotNull(builderAtomicReference.get());
    assertTrue(builderAtomicReference.get().generator().isClosed());
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:17,代码来源:RestBuilderListenerTests.java

示例3: testSetSource

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
/**
 * test setting the source with available setters
 */
public void testSetSource() throws IOException {
    CreateIndexRequestBuilder builder = new CreateIndexRequestBuilder(this.testClient, CreateIndexAction.INSTANCE);
    builder.setSource("{\""+KEY+"\" : \""+VALUE+"\"}", XContentType.JSON);
    assertEquals(VALUE, builder.request().settings().get(KEY));

    XContentBuilder xContent = XContentFactory.jsonBuilder().startObject().field(KEY, VALUE).endObject();
    xContent.close();
    builder.setSource(xContent);
    assertEquals(VALUE, builder.request().settings().get(KEY));

    ByteArrayOutputStream docOut = new ByteArrayOutputStream();
    XContentBuilder doc = XContentFactory.jsonBuilder(docOut).startObject().field(KEY, VALUE).endObject();
    doc.close();
    builder.setSource(docOut.toByteArray(), XContentType.JSON);
    assertEquals(VALUE, builder.request().settings().get(KEY));

    Map<String, String> settingsMap = new HashMap<>();
    settingsMap.put(KEY, VALUE);
    builder.setSettings(settingsMap);
    assertEquals(VALUE, builder.request().settings().get(KEY));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:25,代码来源:CreateIndexRequestBuilderTests.java

示例4: testSetSettings

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
/**
 * test setting the settings with available setters
 */
public void testSetSettings() throws IOException {
    CreateIndexRequestBuilder builder = new CreateIndexRequestBuilder(this.testClient, CreateIndexAction.INSTANCE);
    builder.setSettings(KEY, VALUE);
    assertEquals(VALUE, builder.request().settings().get(KEY));

    builder.setSettings("{\""+KEY+"\" : \""+VALUE+"\"}", XContentType.JSON);
    assertEquals(VALUE, builder.request().settings().get(KEY));

    builder.setSettings(Settings.builder().put(KEY, VALUE));
    assertEquals(VALUE, builder.request().settings().get(KEY));

    builder.setSettings(Settings.builder().put(KEY, VALUE).build());
    assertEquals(VALUE, builder.request().settings().get(KEY));

    Map<String, String> settingsMap = new HashMap<>();
    settingsMap.put(KEY, VALUE);
    builder.setSettings(settingsMap);
    assertEquals(VALUE, builder.request().settings().get(KEY));

    XContentBuilder xContent = XContentFactory.jsonBuilder().startObject().field(KEY, VALUE).endObject();
    xContent.close();
    builder.setSettings(xContent);
    assertEquals(VALUE, builder.request().settings().get(KEY));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:28,代码来源:CreateIndexRequestBuilderTests.java

示例5: writeSnapshotList

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
/**
 * Writes snapshot index file
 * <p>
 * This file can be used by read-only repositories that are unable to list files in the repository
 *
 * @param snapshots list of snapshot ids
 * @throws IOException I/O errors
 */
protected void writeSnapshotList(List<SnapshotId> snapshots) throws IOException {
    final BytesReference bRef;
    try(BytesStreamOutput bStream = new BytesStreamOutput()) {
        try(StreamOutput stream = new OutputStreamStreamOutput(bStream)) {
            XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON, stream);
            builder.startObject();
            builder.startArray("snapshots");
            for (SnapshotId snapshot : snapshots) {
                builder.value(snapshot.getSnapshot());
            }
            builder.endArray();
            builder.endObject();
            builder.close();
        }
        bRef = bStream.bytes();
    }
    if (snapshotsBlobContainer.blobExists(SNAPSHOTS_FILE)) {
        snapshotsBlobContainer.deleteBlob(SNAPSHOTS_FILE);
    }
    snapshotsBlobContainer.writeBlob(SNAPSHOTS_FILE, bRef);
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:30,代码来源:BlobStoreRepository.java

示例6: testJSONGeneration

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
public void testJSONGeneration() throws IOException {
    Map<String, Object> vars = new HashMap<>();
    vars.put("template", "filled");
    TemplateQueryBuilder builder = new TemplateQueryBuilder("I am a $template string", ScriptType.INLINE, vars);
    XContentBuilder content = XContentFactory.jsonBuilder();
    content.startObject();
    builder.doXContent(content, null);
    content.endObject();
    content.close();
    assertEquals("{\"template\":{\"inline\":\"I am a $template string\",\"lang\":\"mustache\",\"params\":{\"template\":\"filled\"}}}",
            content.string());
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:13,代码来源:TemplateQueryBuilderTests.java

示例7: parseCreateField

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    if (!enabled) {
        return;
    }
    if (!fieldType().stored()) {
        return;
    }
    BytesReference source = context.sourceToParse().source();
    // Percolate and tv APIs may not set the source and that is ok, because these APIs will not index any data
    if (source == null) {
        return;
    }

    if (filter != null) {
        // we don't update the context source if we filter, we want to keep it as is...
        Tuple<XContentType, Map<String, Object>> mapTuple =
            XContentHelper.convertToMap(source, true, context.sourceToParse().getXContentType());
        Map<String, Object> filteredSource = filter.apply(mapTuple.v2());
        BytesStreamOutput bStream = new BytesStreamOutput();
        XContentType contentType = mapTuple.v1();
        XContentBuilder builder = XContentFactory.contentBuilder(contentType, bStream).map(filteredSource);
        builder.close();

        source = bStream.bytes();
    }
    BytesRef ref = source.toBytesRef();
    fields.add(new StoredField(fieldType().name(), ref.bytes, ref.offset, ref.length));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:30,代码来源:SourceFieldMapper.java

示例8: testTypeParsing

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
public void testTypeParsing() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1")
            .startObject("properties").startObject("completion")
            .field("type", "completion")
            .field("analyzer", "simple")
            .field("search_analyzer", "standard")
            .field("preserve_separators", false)
            .field("preserve_position_increments", true)
            .field("max_input_length", 14)
            .endObject().endObject()
            .endObject().endObject().string();

    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));

    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    assertThat(fieldMapper, instanceOf(CompletionFieldMapper.class));

    CompletionFieldMapper completionFieldMapper = (CompletionFieldMapper) fieldMapper;
    XContentBuilder builder = jsonBuilder().startObject();
    completionFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
    builder.close();
    Map<String, Object> serializedMap = createParser(JsonXContent.jsonXContent, builder.bytes()).map();
    Map<String, Object> configMap = (Map<String, Object>) serializedMap.get("completion");
    assertThat(configMap.get("analyzer").toString(), is("simple"));
    assertThat(configMap.get("search_analyzer").toString(), is("standard"));
    assertThat(Boolean.valueOf(configMap.get("preserve_separators").toString()), is(false));
    assertThat(Boolean.valueOf(configMap.get("preserve_position_increments").toString()), is(true));
    assertThat(Integer.valueOf(configMap.get("max_input_length").toString()), is(14));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:30,代码来源:CompletionFieldMapperTests.java

示例9: getPoolSettingsThroughJson

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
private Map<String, Object> getPoolSettingsThroughJson(ThreadPoolInfo info, String poolName) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    info.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    builder.close();
    Map<String, Object> poolsMap;
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.string())) {
        poolsMap = parser.map();
    }
    return (Map<String, Object>) ((Map<String, Object>) poolsMap.get("thread_pool")).get(poolName);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:13,代码来源:SimpleThreadPoolIT.java

示例10: testSetSource

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
/**
 * test setting the source for the request with different available setters
 */
public void testSetSource() throws Exception {
    IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(this.testClient, IndexAction.INSTANCE);
    Map<String, String> source = new HashMap<>();
    source.put("SomeKey", "SomeValue");
    indexRequestBuilder.setSource(source);
    assertEquals(EXPECTED_SOURCE, XContentHelper.convertToJson(indexRequestBuilder.request().source(), true));

    indexRequestBuilder.setSource(source, XContentType.JSON);
    assertEquals(EXPECTED_SOURCE, XContentHelper.convertToJson(indexRequestBuilder.request().source(), true));

    indexRequestBuilder.setSource("SomeKey", "SomeValue");
    assertEquals(EXPECTED_SOURCE, XContentHelper.convertToJson(indexRequestBuilder.request().source(), true));

    // force the Object... setter
    indexRequestBuilder.setSource((Object) "SomeKey", "SomeValue");
    assertEquals(EXPECTED_SOURCE, XContentHelper.convertToJson(indexRequestBuilder.request().source(), true));

    ByteArrayOutputStream docOut = new ByteArrayOutputStream();
    XContentBuilder doc = XContentFactory.jsonBuilder(docOut).startObject().field("SomeKey", "SomeValue").endObject();
    doc.close();
    indexRequestBuilder.setSource(docOut.toByteArray(), XContentType.JSON);
    assertEquals(EXPECTED_SOURCE, XContentHelper.convertToJson(indexRequestBuilder.request().source(), true,
        indexRequestBuilder.request().getContentType()));

    doc = XContentFactory.jsonBuilder().startObject().field("SomeKey", "SomeValue").endObject();
    doc.close();
    indexRequestBuilder.setSource(doc);
    assertEquals(EXPECTED_SOURCE, XContentHelper.convertToJson(indexRequestBuilder.request().source(), true));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:33,代码来源:IndexRequestBuilderTests.java

示例11: toXContent

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    /**
     * array mapping should look like:
     *
     * "fieldName": {
     *      "type": "array":
     *      "inner": {
     *          "type": "string"
     *          ...
     *      }
     * }
     *
     *
     * Use the innerMapper to generate the mapping for the inner type which will look like:
     *
     * "fieldName": {
     *      "type": "string",
     *      ...
     * }
     *
     * and then parse the contents of the object to set it into the "inner" field of the outer array type.
     */
    XContentBuilder innerBuilder = new XContentBuilder(builder.contentType().xContent(), new BytesStreamOutput(0));
    innerBuilder = innerMapper.toXContent(innerBuilder, params);
    innerBuilder.close();
    XContentParser parser = builder.contentType().xContent().createParser(innerBuilder.bytes());

    //noinspection StatementWithEmptyBody
    while ((parser.nextToken() != XContentParser.Token.START_OBJECT)) {
        // consume tokens until start of object
    }
    Map<String, Object> innerMap = parser.mapOrdered();

    builder.startObject(simpleName());
    builder.field("type", contentType());
    builder.field(INNER, innerMap);
    return builder.endObject();
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:40,代码来源:ArrayMapper.java

示例12: parsePercolatorDocument

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
Query parsePercolatorDocument(String id, BytesReference source) {
    String type = null;
    BytesReference querySource = null;
    try (XContentParser sourceParser = XContentHelper.createParser(source)) {
        String currentFieldName = null;
        XContentParser.Token token = sourceParser.nextToken(); // move the START_OBJECT
        if (token != XContentParser.Token.START_OBJECT) {
            throw new ElasticsearchException("failed to parse query [" + id + "], not starting with OBJECT");
        }
        while ((token = sourceParser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
                currentFieldName = sourceParser.currentName();
            } else if (token == XContentParser.Token.START_OBJECT) {
                if ("query".equals(currentFieldName)) {
                    if (type != null) {
                        return parseQuery(type, sourceParser);
                    } else {
                        XContentBuilder builder = XContentFactory.contentBuilder(sourceParser.contentType());
                        builder.copyCurrentStructure(sourceParser);
                        querySource = builder.bytes();
                        builder.close();
                    }
                } else {
                    sourceParser.skipChildren();
                }
            } else if (token == XContentParser.Token.START_ARRAY) {
                sourceParser.skipChildren();
            } else if (token.isValue()) {
                if ("type".equals(currentFieldName)) {
                    type = sourceParser.text();
                }
            }
        }
        try (XContentParser queryParser = XContentHelper.createParser(querySource)) {
            return parseQuery(type, queryParser);
        }
    } catch (Exception e) {
        throw new PercolatorException(shardId().index(), "failed to parse query [" + id + "]", e);
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:41,代码来源:PercolatorQueriesRegistry.java

示例13: filter

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
public AliasAction filter(QueryBuilder queryBuilder) {
    if (queryBuilder == null) {
        this.filter = null;
        return this;
    }
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder();
        queryBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
        builder.close();
        this.filter = builder.string();
        return this;
    } catch (IOException e) {
        throw new ElasticsearchGenerationException("Failed to build json for alias request", e);
    }
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:16,代码来源:AliasAction.java

示例14: filter

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
public static CompressedXContent filter(QueryBuilder filterBuilder) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder();
    filterBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.close();
    return new CompressedXContent(builder.string());
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:7,代码来源:ShardSearchTransportRequestTests.java

示例15: testCopyToFieldsParsing

import org.elasticsearch.common.xcontent.XContentBuilder; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public void testCopyToFieldsParsing() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties")
            .startObject("copy_test")
            .field("type", "text")
            .array("copy_to", "another_field", "cyclic_test")
            .endObject()

            .startObject("another_field")
            .field("type", "text")
            .endObject()

            .startObject("cyclic_test")
            .field("type", "text")
            .array("copy_to", "copy_test")
            .endObject()

            .startObject("int_to_str_test")
            .field("type", "integer")
            .field("doc_values", false)
            .array("copy_to",  "another_field", "new_field")
            .endObject()
            .endObject().endObject().endObject().string();

    IndexService index = createIndex("test");
    client().admin().indices().preparePutMapping("test").setType("type1").setSource(mapping, XContentType.JSON).get();
    DocumentMapper docMapper = index.mapperService().documentMapper("type1");
    FieldMapper fieldMapper = docMapper.mappers().getMapper("copy_test");

    // Check json serialization
    TextFieldMapper stringFieldMapper = (TextFieldMapper) fieldMapper;
    XContentBuilder builder = jsonBuilder().startObject();
    stringFieldMapper.toXContent(builder, ToXContent.EMPTY_PARAMS).endObject();
    builder.close();
    Map<String, Object> serializedMap;
    try (XContentParser parser = createParser(JsonXContent.jsonXContent, builder.bytes())) {
        serializedMap = parser.map();
    }
    Map<String, Object> copyTestMap = (Map<String, Object>) serializedMap.get("copy_test");
    assertThat(copyTestMap.get("type").toString(), is("text"));
    List<String> copyToList = (List<String>) copyTestMap.get("copy_to");
    assertThat(copyToList.size(), equalTo(2));
    assertThat(copyToList.get(0), equalTo("another_field"));
    assertThat(copyToList.get(1), equalTo("cyclic_test"));

    // Check data parsing
    BytesReference json = jsonBuilder().startObject()
            .field("copy_test", "foo")
            .field("cyclic_test", "bar")
            .field("int_to_str_test", 42)
            .endObject().bytes();

    ParsedDocument parsedDoc = docMapper.parse("test", "type1", "1", json);
    ParseContext.Document doc = parsedDoc.rootDoc();
    assertThat(doc.getFields("copy_test").length, equalTo(2));
    assertThat(doc.getFields("copy_test")[0].stringValue(), equalTo("foo"));
    assertThat(doc.getFields("copy_test")[1].stringValue(), equalTo("bar"));

    assertThat(doc.getFields("another_field").length, equalTo(2));
    assertThat(doc.getFields("another_field")[0].stringValue(), equalTo("foo"));
    assertThat(doc.getFields("another_field")[1].stringValue(), equalTo("42"));

    assertThat(doc.getFields("cyclic_test").length, equalTo(2));
    assertThat(doc.getFields("cyclic_test")[0].stringValue(), equalTo("foo"));
    assertThat(doc.getFields("cyclic_test")[1].stringValue(), equalTo("bar"));

    assertThat(doc.getFields("int_to_str_test").length, equalTo(1));
    assertThat(doc.getFields("int_to_str_test")[0].numericValue().intValue(), equalTo(42));

    assertThat(doc.getFields("new_field").length, equalTo(2)); // new field has doc values
    assertThat(doc.getFields("new_field")[0].numericValue().intValue(), equalTo(42));

    assertNotNull(parsedDoc.dynamicMappingsUpdate());
    client().admin().indices().preparePutMapping("test").setType("type1")
        .setSource(parsedDoc.dynamicMappingsUpdate().toString(), XContentType.JSON).get();

    docMapper = index.mapperService().documentMapper("type1");
    fieldMapper = docMapper.mappers().getMapper("new_field");
    assertThat(fieldMapper.fieldType().typeName(), equalTo("long"));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:81,代码来源:CopyToMapperTests.java


注:本文中的org.elasticsearch.common.xcontent.XContentBuilder.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。