Elasticsearch java API (6) index API

来源:互联网 发布:羊毛刷批发淘宝网 编辑:程序博客网 时间:2024/06/05 03:43

连接客户端客户节点编辑

你可以开始在本地客户机节点然后简单地创建一个 TransportClient在您的应用程序连接到这个客户机节点。

这样,客户机节点能够加载任何你所需要的插件(例如思考发现插件)。

索引API编辑

指数API允许一个索引类型的JSON文档转换为一个特定的索引和搜索。

生成JSON文档编辑

有几种不同的方法生成一个JSON文档:

  • 手动(又名自己动手)使用本机 byte[]或作为一个 String
  • 使用一个 Map将自动转换为JSON
  • 使用第三方库来序列化豆类等杰克逊
  • 使用内置的助手XContentFactory.jsonBuilder()

在内部,每一个类型转换为 byte[](所以被转换为一个字符串 byte[])。因此,如果对象是已经在这个形式,然后使用它。 jsonBuilder高度优化的JSON发电机直接构造一个吗 byte[].

自己动手编辑

没什么困难的在这里,但是请注意,您必须根据编码日期日期格式.

String json = "{" +        "\"user\":\"kimchy\"," +        "\"postDate\":\"2013-01-30\"," +        "\"message\":\"trying out Elasticsearch\"" +    "}";

使用 Map编辑

地图是一个关键:值对集合。它代表一个JSON结构:

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已经使用杰克逊所以你可以使用它来序列化JSON bean:

import com.fasterxml.jackson.databind.*;// instance a json mapperObjectMapper mapper = new ObjectMapper(); // create once, reuse// generate jsonbyte[] json = mapper.writeValueAsBytes(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()方法。

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()                  )        .get();

请注意,您也可以索引你的文档作为JSON字符串,你没有给一个ID:
String json = "{" +        "\"user\":\"kimchy\"," +        "\"postDate\":\"2013-01-30\"," +        "\"message\":\"trying out Elasticsearch\"" +    "}";IndexResponse response = client.prepareIndex("twitter", "tweet")        .setSource(json)        .get();

IndexResponse对象会给你一个报告:
// Index nameString _index = response.getIndex();// Type nameString _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();// isCreated() is true if the document is a new one, false if it has been updatedboolean created = response.isCreated();

有关索引操作的更多信息,请查看指数文档。

操作线程编辑

指数API允许设置线程模型时将执行的操作的实际执行API在同一个节点上执行(执行API碎片分配在同一台服务器上)。

选择一个不同的线程上执行操作,或调用线程上执行它(注意,API仍然是异步)。默认情况下, operationThreaded被设置为 true这意味着一个不同的线程上执行的操作。

0 0
原创粉丝点击