ElasticSearch详解二之——index api

index API允许将JSON document转换为一个特定的index,使它便于搜索操作。

生成JSON文档:

几种不同的方法生成一个JSON document:

  • 手动使用 byte[] 或String
  • 使用一个map来等效转换为JSON
  • 使用第三方库来将beans装换(如Jackson)
  • 使用内置的XContentFactory.jsonBuilder()

在内部,每一个类型转换为 byte[] (所以String转换到byte[])。 因此,如果对象是已经在这个形式, 就直接使用它。 jsonBuilder 是高度优化的JSON生成机制,用来直接构造为byte[] 。
这里没有特别困难的,但请注意,您将必须根据DateFormate来格式化日期。

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

使用map

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");

序列化beans

Elasticsearch已经使用jackson,在org.elasticsearch.common.jackson 包中。 所以,你可以添加自己Jackson版本到 pom.xml 文件或在你的classpath中。

例如:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.1.3</version>
</dependency>

然后,您可以开始序列化JSON bean:

import com.fasterxml.jackson.databind.*;
// instance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
// generate json
String json = mapper.writeValueAsString(yourbeaninstance);

使用Elasticsearch helpers

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

Indexdocument:

下面的示例索引将JSON document转换为一个索引 (twitter),type为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();

请注意,您也可以index document作为JSON Strings,你不用给一个ID:

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

IndexResponse response = client.prepareIndex("twitter", "tweet")
    .setSource(json)
    .execute()
    .actionGet();

IndexResponse 对象将给你一个report:

// 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();

如果你使用percolation构造索引, IndexResponse对象会给相应的过滤器:

IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
    .setSource(json)
    .execute()
    .actionGet();
List<String> matches = response.matches();
Operation threading:

Index API允许你设置线程来执行操作,实际执行API上执行的是相同的节点(API上执行一个分配在同一服务器的shard上)。

不同的线程上执行操作,或调用线程执行(注意,API仍然是异步)。默认情况下, operationThreaded 被设置为true,这意味着操作是由不同的线程上执行。
来自
https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/generate.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值