索引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();

最新文章

  1. Error: unable to connect to node rabbit@mail: nodedown
  2. css3 -- 过渡与动画
  3. hessionproxy
  4. &lt;雨季&gt;
  5. Dynamic Time Warping 动态时间规整算法
  6. CSS中一些不经意的细节问题1
  7. 浅析LRU(K-V)缓存
  8. django配置fcgi参数解释
  9. VS禁止特定警告
  10. HDU 1213 How Many Tables(并查集,简单)
  11. 不要在头文件中使用 using namespace std;
  12. 数据库对象(视图,序列,索引,同义词)【weber出品必属精品】
  13. CSS3的一些前缀
  14. Cocos2D游戏项目CCTableView在Xcode7.2下的无法滚动问题
  15. git 服务器 LINUX端的使用
  16. vue 解决IE不能用的问题
  17. NLP入门(五)用深度学习实现命名实体识别(NER)
  18. Codeforces Round #503 (by SIS, Div. 2) Solution
  19. Java编程的逻辑 (76) - 并发容器 - 各种队列
  20. 20-java 对象链表空没空呢

热门文章

  1. springboot+mybatis 非web项目构建
  2. php开发面试题---面试常用英语(你能介绍你自己吗?)
  3. id(), is, ==, 的区别与小数据池
  4. 分类算法之朴素贝叶斯分类(Naive Bayesian classification)
  5. 第二篇:怕碰到是因为没掌握,来吧,zTree!
  6. MyBatis - 常用标签与动态Sql
  7. 08_springboot2.x自定义starter
  8. Ring HDU - 2296 AC自动机+简单DP和恶心的方案输出
  9. NEO4J 图数据库使用APOC数据导入
  10. js 调用接口并传参