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,这意味着操作将在不同的线程上执行。

 

 

时间: 2024-11-05 19:35:57

Index API的相关文章

Elasticsearch Index API & Aggregations API & Query DSL

这篇小菜给大家演示和讲解一些Elasticsearch的API,如在工作中用到时,方便查阅. 一.Index API 创建索引库 curl -XPUT 'http://127.0.0.1:9200/test_index/' -d '{     "settings" : {       "index" : {       "number_of_shards" : 3,       "number_of_replicas" : 1

elasticsearch6.7 05. Document APIs(2)Index API

目录 1.Index API 1.1 自动创建索引(Automatic Index Creation) 1.2 操作类型(Operation Type) 1.3 自动生成id(Automatic ID Generation) 1.4 并发控制(Optimistic concurrency control) 1.5 路由(Routing) 1.6 分布式(Distributed) 1.7 等待活跃分片(Wait For Active Shards) 1.8 刷新(refresh) 1.9 空操作(

ElasticSearch Index API &amp;&amp; Mapping

ElasticSearch  NEST Client 操作Index var indexName="twitter"; var deleteIndexResponse = client.DeleteIndex(indexName);                var createIndexResponse = client.CreateIndex(indexName);                var getIndexResponse = client.GetIndex(in

elasticsearch的javaAPI之index

Index API 原文:http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.html index API允许你将JSON document转换为一个特定的index,使它便于搜索操作. 生成JSON文档: 有几种不同的方法生成一个JSON document: 手动使用 byte[] 或String 使用一个map来等效转换为JSON 使用第三方库来将beans装换(如Jackson

Elasticsearch Java API简要总汇

3.1 集群的连接 3.1.1 作为Elasticsearch节点 [html] view plain copy 代码: import static org.elasticsearch.node.NodeBuilder.nodeBuilder; import org.elasticsearch.client.Client; import org.elasticsearch.node.Node; Node node = nodeBuilder().clusterName("escluster2&q

http://elasticsearch-py.readthedocs.io/en/master/api.html

API Documentation All the API calls map the raw REST api as closely as possible, including the distinction between required and optional arguments to the calls. This means that the code makes distinction between positional and keyword arguments; we,

elasticsearch中常用的API

elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: 按照更直观的形式返回数据,更适用于控制台请求展示 集群API: 对集群进行查看和操作的API 下面简单的一一介绍记录一下. 文档类API Index API: 创建并建立索引 PUT twitter/tweet/1{ "user" : "kimchy", "p

elasticsearch建立索引操作的API

ElasticSearch-API-Index 索引创建API允许初始化一个索引.ElasticSearch对多重索引提供了支持,包括跨多个索引执行操作.每个索引在创建时可以让一个特定的设置项与其关联. 最简单的方式创建索引 curl -XPUT 'http://localhost:9200/twitter/' 在创建索引的时候指定分片和副本数量,参数格式采用YAML格式 curl -XPUT 'http://localhost:9200/twitter/' -d ' index:   numb

[转载]Elasticsearch Java API总汇

from: http://blog.csdn.net/changong28/article/details/38445805#comments 3.1 集群的连接 3.1.1 作为Elasticsearch节点 [html] view plaincopy 代码: import static org.elasticsearch.node.NodeBuilder.nodeBuilder; import org.elasticsearch.client.Client; import org.elast