版权声明: https://blog.csdn.net/u012204535/article/details/82955828
Elasticsearch把输入文档和复杂的查询语法及输出的查询结果都封装成了XContent,这样数据就可以采用XML或者JSON格式表示成可读的形式。JSON表示形式更简短,所以Elasticsearch采用了JSON格式来表示XContent。因为要使用JSON和Elasticsearch服务端打交道。
一个表示Elasticsearch版本的对象如下:
"version":{
"number":"5.3.0",
"build_hash":"3adb13b",
"build_date":"2017-03-23T03:31:50.652Z",
"build_snapshot":false,
"lucene_version":"6.4.1"
}
可以使用Elasticsearch提供的API构建JSON串
例如,在Eclipse中创建一个Gradle项目,首先引入jackson相关的jar包,然后在build.gradle文件中增加依赖库:
runtime group:'org.elasticsearch', name: 'elasticsearch', version:'5.6.2'
最后运行如下代码:
XContentBuilder b = XContentFactory.jsonBuilder().startObject();
b.field("title", "了解Elasticsearch");
b.field("body", "content");
b.endObject();
// 从XContent到JSON String json = b.bytes().utf8ToString();
System.out.println(json);
输出结果如下:
{"title":"了解Elasticsearch","body":"content"}