ElasticSearch(三) DocumentAPIS Index API

本文介绍如何在Elasticsearch中索引JSON文档,包括手动构造JSON字符串、使用Map自动转换、通过Jackson序列化对象及利用Elasticsearch内置工具等方法,并提供了具体的示例代码。

ElasticSearch 允许将一个JSON字符串作为文档。

生成JSON文档有下面几种方式:

  • Manually (aka do it yourself) using native byte[] or as a String(自己构造byte[] 或者json串)
  • Using a Map that will be automatically converted to its JSON equivalent(使用Map自动转换为JSON)
  • Using a third party library to serialize your beans such as Jackson(将对象用第三方JSON库转换)
  • Using built-in helpers XContentFactory.jsonBuilder()(用XContentFactory)

实际上每种类型最终都是转化为byte[],所以XContentFactory.jsonBuilder() 是最高效的办法。

Do It Yourselfedit

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

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

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

Map<String, Object> json = new HashMap<String, Object>();
json.put("user","kimchy");
json.put("postDate",new Date());
json.put("message","trying out Elasticsearch");
Serialize your beansedit

You can use Jackson to serialize your beans to JSON. Please add Jackson Databind to your project. Then you can use ObjectMapper to serialize your beans:

import com.fasterxml.jackson.databind.*;

// instance a json mapper
ObjectMapper mapper = new ObjectMapper(); // create once, reuse

// generate json
byte[] json = mapper.writeValueAsBytes(yourbeaninstance);
Use Elasticsearch helpersedit

Elasticsearch provides built-in helpers to generate JSON content.

import static org.elasticsearch.common.xcontent.XContentFactory.*;

XContentBuilder builder = jsonBuilder()
    .startObject()
        .field("user", "kimchy")
        .field("postDate", new Date())
        .field("message", "trying out Elasticsearch")
    .endObject()

Index documentedit

The following example indexes a JSON document into an index called twitter, under a type called tweet, with id valued 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();

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

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

IndexResponse response = client.prepareIndex("twitter", "tweet")
        .setSource(json, XContentType.JSON)
        .get();

IndexResponse object will give you a 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();
// status has stored current instance statement.
RestStatus status = response.status();

 

转载于:https://www.cnblogs.com/xiaocandou/p/8117438.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值