Java Code Examples for org.elasticsearch.common.xcontent.XContentBuilder#string()

The following examples show how to use org.elasticsearch.common.xcontent.XContentBuilder#string() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CrudDemo.java    From javabase with Apache License 2.0 6 votes vote down vote up
/**
 * https://github.com/medcl/elasticsearch-analysis-pinyin/tree/v1.7.5
 * pinyin配置分析器
 * @return
 */
public static String getIndexPinYinSetting() throws IOException {
    XContentBuilder mapping = XContentFactory.jsonBuilder()
            .startObject()
                .startObject("analysis")
                    .startObject("analyzer")
                        .startObject("pinyin_analyzer")
                            .field("tokenizer", "my_pinyin")
                            .array("filter","nGram","word_delimiter")
                        .endObject()
                     .endObject()
                    .startObject("tokenizer")
                        .startObject("my_pinyin")
                            .field("type", "pinyin")
                            .field("first_letter","prefix")
                            .field("padding_char","")
                        .endObject()
                     .endObject()
                .endObject()
            .endObject();
    return mapping.string();
}
 
Example 2
Source File: Alias.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 3
Source File: PendingClusterTasksResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
 
Example 4
Source File: ToXContentToBytes.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public final String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.prettyPrint();
        toXContent(builder, EMPTY_PARAMS);
        return builder.string();
    } catch (Exception e) {
        return "{ \"error\" : \"" + ExceptionsHelper.detailedMessage(e) + "\"}";
    }
}
 
Example 5
Source File: MultiSearchResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
 
Example 6
Source File: NodesStatsResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
 
Example 7
Source File: NodesInfoResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
 
Example 8
Source File: SnapshotStatus.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
 
Example 9
Source File: IndicesStatsResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
 
Example 10
Source File: ElasticsearchTemplateRecordConsumer.java    From baleen with Apache License 2.0 5 votes vote down vote up
/** Add a mapping to Elasticsearch. This will only be called if a new index has been created */
public void addMapping(XContentBuilder mapping) {
  try {
    HttpEntity entity = new StringEntity(mapping.string(), ContentType.APPLICATION_JSON);

    esrResource
        .getClient()
        .performRequest("PUT", "/" + index + "/_mapping/" + type, Collections.emptyMap(), entity);
  } catch (IOException ioe) {
    getMonitor().error("Unable to add mapping to index", ioe);
  }
}
 
Example 11
Source File: ToXContentAsString.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
public String asString(ToXContent toXContent) throws IOException {
    XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
    try {
        toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS);
    } catch (IOException e) {
        //hack: the first object in the builder might need to be opened, depending on the ToXContent implementation
        //Let's just try again, hopefully it'll work
        builder.startObject();
        toXContent.toXContent(builder, ToXContent.EMPTY_PARAMS);
        builder.endObject();
    }
    return builder.string();
}
 
Example 12
Source File: ClusterState.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
 
Example 13
Source File: SearchStats.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
 
Example 14
Source File: TransportClient.java    From elasticsearch-jest-example with MIT License 5 votes vote down vote up
/**
 * 使用Elasticsearch XContentBuilder 创建索引
 * @throws Exception 
 */
private static void useXContentBuilderCreatIndex() throws Exception {
	XContentBuilder builder = jsonBuilder()
		    .startObject()
		    .field("name","Go Web编程")
			.field("author","谢孟军 ")
			.field("pubinfo","电子工业出版社")
			.field("pubtime","2013-6")
			.field("desc","《Go Web编程》介绍如何使用Go语言编写Web,包含了Go语言的入门、Web相关的一些知识、Go中如何处理Web的各方面设计(表单、session、cookie等)、数据库以及如何编写GoWeb应用等相关知识。通过《Go Web编程》的学习能够让读者了解Go的运行机制,如何用Go编写Web应用,以及Go的应用程序的部署和维护等,让读者对整个的Go的开发了如指掌。")
		    .endObject();
	String jsonstring = builder.string();
	createIndex("book","book",jsonstring);
}
 
Example 15
Source File: IngestClusterBlockTest.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@Test(expected = ClusterBlockException.class)
public void testClusterBlockNodeClient() throws Exception {
    IngestRequestBuilder brb = client("1").prepareExecute(IngestAction.INSTANCE);
    XContentBuilder builder = jsonBuilder().startObject().field("field", "value").endObject();
    String jsonString = builder.string();
    IndexRequest indexRequest = client("1").prepareExecute(IndexAction.INSTANCE)
            .setIndex("test").setType("test").setId("1").setSource(jsonString).request();
    brb.add(indexRequest);
    brb.execute().actionGet();
}
 
Example 16
Source File: RecoveryState.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized String toString() {
    try {
        XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint();
        builder.startObject();
        toXContent(builder, EMPTY_PARAMS);
        builder.endObject();
        return builder.string();
    } catch (IOException e) {
        return "{ \"error\" : \"" + e.getMessage() + "\"}";
    }
}
 
Example 17
Source File: PredicateAnalyzer.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
public static String queryAsJson(QueryBuilder query) throws IOException {
  XContentBuilder x = XContentFactory.jsonBuilder();
  x.prettyPrint().lfAtEnd();
  query.toXContent(x, ToXContent.EMPTY_PARAMS);
  return x.string();
}
 
Example 18
Source File: LocalWeatherDataMapperTest.java    From ElasticUtils with MIT License 4 votes vote down vote up
@Test
public void valid_json_mapping_generated_when_calling_get_mapping() throws Exception {

    // Create the Mapper:
    LocalWeatherDataMapper mapper = new LocalWeatherDataMapper();

    // Get the Mapping:
    XContentBuilder mapping = mapper.getMapping();

    // Turn it into a String:
    String actualJsonContent = mapping.string();

    // Expected Mapping:
    String expectedJsonContent = "{\"document\":{\"properties\":{\"dateTime\":{\"type\":\"date\",\"format\":\"strict_date_optional_time||epoch_millis\"},\"skyCondition\":{\"type\":\"string\"},\"station\":{\"type\":\"nested\",\"include_in_parent\":true,\"properties\":{\"coordinates\":{\"type\":\"geo_point\",\"lat_lon\":true},\"location\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"},\"state\":{\"type\":\"string\"},\"wban\":{\"type\":\"string\"}}},\"stationPressure\":{\"type\":\"float\"},\"temperature\":{\"type\":\"float\"},\"windSpeed\":{\"type\":\"float\"}}}}";


    final JsonNode tree1 = jacksonObjectMapper.readTree(expectedJsonContent);
    final JsonNode tree2 = jacksonObjectMapper.readTree(actualJsonContent);

    Assert.assertEquals(true, tree1.equals(tree2));

}
 
Example 19
Source File: LocalWeatherDataMapperTest.java    From ElasticUtils with MIT License 4 votes vote down vote up
@Test
public void valid_json_mapping_generated_when_calling_get_mapping() throws Exception {

    // Create the Mapper:
    LocalWeatherDataMapper mapper = new LocalWeatherDataMapper();

    // Get the Mapping:
    XContentBuilder mapping = mapper.getMapping();

    // Turn it into a String:
    String actualJsonContent = mapping.string();

    // Expected Mapping:
    String expectedJsonContent = "{\"document\":{\"properties\":{\"dateTime\":{\"type\":\"date\",\"format\":\"strict_date_optional_time||epoch_millis\"},\"skyCondition\":{\"type\":\"string\"},\"station\":{\"type\":\"nested\",\"include_in_parent\":true,\"properties\":{\"coordinates\":{\"type\":\"geo_point\",\"lat_lon\":true},\"location\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"},\"state\":{\"type\":\"string\"},\"wban\":{\"type\":\"string\"}}},\"stationPressure\":{\"type\":\"float\"},\"temperature\":{\"type\":\"float\"},\"windSpeed\":{\"type\":\"float\"}}}}";


    final JsonNode tree1 = jacksonObjectMapper.readTree(expectedJsonContent);
    final JsonNode tree2 = jacksonObjectMapper.readTree(actualJsonContent);

    Assert.assertEquals(true, tree1.equals(tree2));

}
 
Example 20
Source File: PlainIndexableObject.java    From elasticsearch-gatherer with Apache License 2.0 4 votes vote down vote up
/**
 * Build JSON with the help of XContentBuilder.
 *
 * @throws java.io.IOException
 */
public String build() throws IOException {
    XContentBuilder builder = jsonBuilder();
    build(builder, source);
    return builder.string();
}