ElasticSearch中json字符串的拼接
2015-06-17 11:10
731 查看
在ES中,所有的查询结果信息,包括doc,都是以json的形式返回的。在es中,提供了拼接json的特定接口,主要分为两种形式。
1、在新建索引的时候,需要指定索引的字段以及字段的属性,这个时候可以借助于json,具体代码如下:
XContentBuilder mapping = jsonBuilder().startObject()
.startObject("properties")
.startObject("ID").field("type", "string").field("store", "yes")
.endObject()
.startObject("title").field("type", "string").field("store", "yes")
.endObject()
.startObject("description").field("type", "string").field("index", "analyzed")
.endObject()
.startObject("number").field("type", "integer")
.endObject()
.startObject("price").field("type", "double")
.endObject()
.startObject("inDate").field("type", "string")
.endObject()
.startObject("outDate").field("type", "string")
.endObject()
.startObject("type").field("type", "boolean")
.endObject().endObject().endObject();
client.prepareIndex("product","wxt").setSource(mapping).execute().actionGet();
client.close();
2、是向es中插入数据时,也是需要将要插入的数据以json的格式送至es中,具体代码如下:
public static XContentBuilder getXContentBuilder(Product product) throws IOException {
return XContentFactory.jsonBuilder()
.startObject()
.field("ID", product.getID())
.field("title", product.getTitle())
.field("description", product.getDescription())
.field("number", product.getNumber())
.field("price", product.getPrice())
.field("inDate", product.getInDate())
.field("outDate", product.getOutDate())
.field("type", product.isType())
.endObject();
}
1、在新建索引的时候,需要指定索引的字段以及字段的属性,这个时候可以借助于json,具体代码如下:
XContentBuilder mapping = jsonBuilder().startObject()
.startObject("properties")
.startObject("ID").field("type", "string").field("store", "yes")
.endObject()
.startObject("title").field("type", "string").field("store", "yes")
.endObject()
.startObject("description").field("type", "string").field("index", "analyzed")
.endObject()
.startObject("number").field("type", "integer")
.endObject()
.startObject("price").field("type", "double")
.endObject()
.startObject("inDate").field("type", "string")
.endObject()
.startObject("outDate").field("type", "string")
.endObject()
.startObject("type").field("type", "boolean")
.endObject().endObject().endObject();
client.prepareIndex("product","wxt").setSource(mapping).execute().actionGet();
client.close();
2、是向es中插入数据时,也是需要将要插入的数据以json的格式送至es中,具体代码如下:
public static XContentBuilder getXContentBuilder(Product product) throws IOException {
return XContentFactory.jsonBuilder()
.startObject()
.field("ID", product.getID())
.field("title", product.getTitle())
.field("description", product.getDescription())
.field("number", product.getNumber())
.field("price", product.getPrice())
.field("inDate", product.getInDate())
.field("outDate", product.getOutDate())
.field("type", product.isType())
.endObject();
}
相关文章推荐
- javascript中top、clientTop、scrollTop、offsetTop的讲解
- extjs4.2.1 点滴
- js cookie 的创建读取删除
- JavaScript 闭包学习
- js swipe 图片滑动控件实现 任意尺寸适用任意屏幕
- Django中模型Model添加JSON类型字段的方法
- JavaScript表单验证
- jsoup:解析HTML用法小结
- cjhupload一个简单异步文件上传插件(html5+js)
- Json入门
- JSON入门之二:org.json的基本用法
- JSON之三:获取JSON文本并解释(以google的天气API为例)
- 利用jsonp、iframe和location.hash解决跨域问题
- js中对页面首次加载时需要数据,而数据是页面所填的问题的处理
- 未能加载文件或程序集“Newtonsoft.Json, Version=4.5.0.0
- 传说中的 events.js:72
- JavaScript随笔之——函数用法
- ArcGIS JavaScript API本地部署离线开发环境[转]
- js判断字符串是否包含中文或英文
- jshint warning翻译