elasticsearch 中文API 索引(三)
索引API
索引API允许开发者索引类型化的JSON文档到一个特定的索引,使其可以被搜索。
生成JSON文档
有几种不同的方式生成JSON文档
- 利用
byte[]或者作为一个String手动生成 - 利用一个
Map将其自动转换为相应的JSON - 利用第三方库如Jackson去序列化你的bean
- 利用内置的帮助函数XContentFactory.jsonBuilder()
手动生成
需要注意的是,要通过Date Format编码日期。
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
使用map
Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
序列化bean
elasticsearch早就用到了Jackson,把它放在了org.elasticsearch.common.jackson下面。你可以在你的pom.xml文件里面添加你自己的Jackson版本。
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.1.3</version>
</dependency>
这样,你就可以序列化你的bean为JSON。
import com.fasterxml.jackson.databind.*;
// instance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
// generate json
String json = mapper.writeValueAsString(yourbeaninstance);
利用elasticsearch帮助类
elasticsearch提供了内置的帮助类来将数据转换为JSON
import static org.elasticsearch.common.xcontent.XContentFactory.*;
XContentBuilder builder = jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
注意,你也可以使用startArray(String)和endArray()方法添加数组。另外,field可以接收任何类型的对象,你可以直接传递数字、时间甚至XContentBuilder对象。
可以用下面的方法查看json。
String json = builder.string();
索引文档
下面的例子将JSON文档索引为一个名字为“twitter”,类型为“tweet”,id值为1的索引。
import static org.elasticsearch.common.xcontent.XContentFactory.*;
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("user", "kimchy")
.field("postDate", new Date())
.field("message", "trying out Elasticsearch")
.endObject()
)
.execute()
.actionGet();
你也可以不提供id:
String json = "{" +
"\"user\":\"kimchy\"," +
"\"postDate\":\"2013-01-30\"," +
"\"message\":\"trying out Elasticsearch\"" +
"}";
IndexResponse response = client.prepareIndex("twitter", "tweet")
.setSource(json)
.execute()
.actionGet();
IndexResponse将会提供给你索引信息
// Index name
String _index = response.getIndex();
// Type name
String _type = response.getType();
// Document ID (generated or not)
String _id = response.getId();
// Version (if it's the first time you index this document, you will get: 1)
long _version = response.getVersion();
如果你在索引时提供了过滤,那么IndexResponse将会提供一个过滤器(percolator )
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(json)
.execute()
.actionGet();
List<String> matches = response.matches();
elasticsearch 中文API 索引(三)的更多相关文章
- elasticsearch 中文API 获得(三)
获取API 获取API允许你通过id从索引中获取类型化的JSON文档,如下例: GetResponse response = client.prepareGet("twitter" ...
- elasticsearch 中文API facets(⑩)
facets Elasticsearch提供完整的java API用来支持facets.在查询的过程中,将需要计数的facets添加到FacetBuilders中.然后将该FacetBuilders条 ...
- elasticsearch 中文API 基于查询的删除(九)
基于查询的删除API 基于查询的删除API允许开发者基于查询删除一个或者多个索引.一个或者多个类型.下面是一个例子. import static org.elasticsearch.index.que ...
- elasticsearch 中文API 记数(八)
计数API 计数API允许开发者简单的执行一个查询,返回和查询条件相匹配的文档的总数.它可以跨多个索引以及跨多个类型执行. import static org.elasticsearch.index. ...
- elasticsearch 中文API bulk(六)
bulk API bulk API允许开发者在一个请求中索引和删除多个文档.下面是使用实例. import static org.elasticsearch.common.xcontent.XCont ...
- elasticsearch 中文API river
river-jdbc 安装 ./bin/plugin --install jdbc --url http://xbib.org/repository/org/xbib/elasticsearch/pl ...
- elasticsearch 中文API 更新(五)
更新API 你能够创建一个UpdateRequest,然后将其发送给client. UpdateRequest updateRequest = new UpdateRequest(); updateR ...
- elasticsearch 中文API 删除(四)
删除API 删除api允许你通过id,从特定的索引中删除类型化的JSON文档.如下例: DeleteResponse response = client.prepareDelete("twi ...
- elasticsearch 中文API(二)
客户端 有多个地方需要使用Java client: 在存在的集群中执行标准的index, get, delete和search 在集群中执行管理任务 当你要运行嵌套在你的应用程序中的Elasticse ...
随机推荐
- winsock 服务器代码(不建议win服务器listen防火墙会禁止外部访问的)
int SessionBase::ServerSock() { /* 4 * WSADATA是个结构体,在WSAStartup中被填充. 5 * WSAStartup为调用WinSock准备初始化的工 ...
- VBA文件对话框的应用(VBA打开文件、VBA选择文件、VBA选择文件夹)
在VBA中经常要用到文件对话框来进行打开文件.选择文件或选择文件夹的操作.用Microsoft Office提供的文件对话框比较方便.用法如下Application.FileDialog(fileDi ...
- Leetcode241.Different Ways to Add Parentheses为运算表达式设计优先级
给定一个含有数字和运算符的字符串,为表达式添加括号,改变其运算优先级以求出不同的结果.你需要给出所有可能的组合的结果.有效的运算符号包含 +, - 以及 * . 示例 1: 输入: "2-1 ...
- sql (12) HAVING
HAVING 子句在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 新建表 StudentSS_id Grade Name phone1 98 小明 12345 ...
- 新建Application 报错android.app.Application cannot be cast
我在开发APP的时候重新使用了一个类,继承了android.app.Application.但是在运行的时候提示java.lang.ClassCastException: android.app.Ap ...
- Windows ipconfig
用法: ipconfig [/allcompartments] [/? | /all | /renew [adapter] | / ...
- thinkphp 运算符
我们可以对模板输出使用运算符,包括对“+”“ –” “*” “/”和“%”的支持. 大理石平台厂家 例如: 运算符 使用示例 + {$a+$b} - {$a-$b} * {$a*$b} / {$a/$ ...
- dart中extends、 implements、with的用法与区别
一.概述 继承(关键字 extends) 混入 mixins (关键字 with) 接口实现(关键字 implements) 这三种关系可以同时存在,但是有前后顺序: extends -> m ...
- 多线程的基本概念和Delphi线程对象Tthread介绍
多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...
- day20_函数的闭包 与 装饰器
#!/usr/bin/env python # -*- coding:utf-8 -*- # # 一些文章 # https://www.cnblogs.com/Vae1242/p/6944338.ht ...