Elasticsearch Index API

Index API

The index API allows one to index a typed JSON document into a specific index and make it searchable.

索引API允许索引为JSON文件类型到一个特定索引使其可被搜索。

Generate JSON document

There are several different ways of generating a JSON document:

  • Manually (aka do it yourself) using native byte[] or as a String
  • Using a Map that will be automatically converted to its JSON equivalent
  • Using a third party library to serialize your beans such as Jackson
  • Using built-in helpers XContentFactory.jsonBuilder()

Internally, each type is converted to byte[] (so a String is converted to a byte[]). Therefore, if the object is in this form already, then use it. The jsonBuilder is highly optimized JSON generator that directly constructs a byte[].

 

这里有几个不同的生成JSON文档的方法:

 

  • 使用本地byte[]或String来手动生成。
  • 使用Map来自动转换成JSON
  • 使用第三方jar来序列化你的实体类(例如jackson)
  • 使用内部帮助 XContentFactory.jsonBuilder()
在内部,所有类型都被转换成byte[](String也被转换成byte[])。因此如果一个对象已经是这种形式,那么就使用它。jsonBuilder是一个高度优化的直接构建byte[]的JSON生成器。

 

Do It Yourself

Nothing really difficult here but note that you will have to encode dates according to the Date Format.

这真的没什么难的,但是注意你需要编码日期格式。

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

Map is a key:values pair collection. It represents a JSON structure:

哈希表 是一个键值对的稽核。它表现为JSON结构:

1 Map<String,Object> json =newHashMap<String,Object>();
2 json.put("user","kimchy");
3 json.put("postDate",newDate());
4 json.put("message","trying out Elasticsearch");

 

 

 

 
Serialize your beans

Elasticsearch already uses Jackson. So you can use it to serialize your beans to JSON:

es已经使用了Jackson.因此你可以使用它来序列化你的实体类为JSON:

1 import com.fasterxml.jackson.databind.*;
2 // instance a json mapper
3 ObjectMapper mapper =newObjectMapper();// create once, reuse
4 // generate json
5 byte[] json = mapper.writeValueAsBytes(yourbeaninstance);

 

 

 

 

Use Elasticsearch helpers

Elasticsearch provides built-in helpers to generate JSON content.

Elasticsearch提供了内置的助手生成JSON的内容。

1 importstatic org.elasticsearch.common.xcontent.XContentFactory.*;
2     XContentBuilder builder = jsonBuilder()
3     .startObject()
4     .field("user","kimchy")
5     .field("postDate",newDate())
6     .field("message","trying out Elasticsearch")
7     .endObject()

 

 

 

 

 

Note that you can also add arrays with startArray(String) and endArray() methods. By the way, the field method accepts many object types. You can directly pass numbers, dates and even other XContentBuilder objects.

If you need to see the generated JSON content, you can use the string() method.

请注意,您还可以使用startArray(String)和endArray()方法来添加数组。顺便说下,这个域方法接受许多对象类型。你可以直接传入 number,date,甚至其它XContentBuilder对象。

说过你需要查看生成的JSON内容,你可以使用string()方法。

1 String json = builder.string();

 

Index document

The following example indexes a JSON document into an index called twitter, under a type called tweet, with id valued 1:

下面的例子索引一个JSON文档到一个 index为twitter,type为tweet里的id,id为1:

 

 1     importstatic org.elasticsearch.common.xcontent.XContentFactory.*;
 2     IndexResponse response = client.prepareIndex("twitter","tweet","1")
 3     .setSource(jsonBuilder()
 4     .startObject()
 5     .field("user","kimchy")
 6     .field("postDate",newDate())
 7     .field("message","trying out Elasticsearch")
 8     .endObject()
 9     )
10     .get();

 

 

Note that you can also index your documents as JSON String and that you don’t have to give an ID:

注意你也可以索引你的文档为没有提供id的JSON字符串:

1     String json ="{"+
2     "\"user\":\"kimchy\","+
3     "\"postDate\":\"2013-01-30\","+
4     "\"message\":\"trying out Elasticsearch\""+
5     "}";
6     IndexResponse response = client.prepareIndex("twitter","tweet")
7     .setSource(json)
8     .get();

 

 

IndexResponse object will give you a report:

IndexResponse对象将会给你一个报告:

 

 1     // Index name
 2     String _index = response.getIndex();
 3     // Type name
 4     String _type = response.getType();
 5     // Document ID (generated or not)
 6     String _id = response.getId();
 7     // Version (if it's the first time you index this document, you will get: 1)
 8     long _version = response.getVersion();
 9     // isCreated() is true if the document is a new one, false if it has been updated
10     boolean created = response.isCreated();

 

 

For more information on the index operation, check out the REST index docs.

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

Operation Threading 操作线程

The index API allows one to set the threading model the operation will be performed when the actual execution of the API is performed on the same node (the API is executed on a shard that is allocated on the same server).

The options are to execute the operation on a different thread, or to execute it on the calling thread (note that the API is still asynchronous). By default, operationThreaded is set to true which means the operation is executed on a different thread.

索引API允许设置线程模型,这个操作真实执行API的时候是执行在同一个节点上(API是执行在一个分配在相同服务器的分片上)。

这个选择是在不同的线程上执行操作,或者调用线程来执行它(注意这个API是异步的)。operationThreaded默认设置为true,这意味着操作将在不同的线程上执行。

 

 

 

 



转载于:https://www.cnblogs.com/churao/p/5856159.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值