Java API [2.3] » Document APIs » Index API

翻译水平有限,仅供参考

Java API [2.3] » Document APIs » Index API
Index API
Index API让我们能够把一个特定类型的JSON文档索引到特定的index中,以供我们搜索

产生JSON文档
有四种方法产生一个JSON文档:
1、利用字节数组byte[]或字符数String手动产生
2、利用Map类型,Map类会自动转换成内容相同的JSON
3、利用第三方库来序列化实体,如jackson
4、利用内置的XContentFactory.jsonBuilder()

在ES内部,上面的方法都会转换到字节数组byte[]。因此,如果对象已经是字节数组,直接使用即可。
jsonBuilder是一个高度优化的JSON产生器,能够直接构造字节数组byte

1、利用字节数组byte[]或字符数String手动产生
下面这个例子中,需要注意把date手动编码成日期格式
String json = "{" +
        "\"user\":\"kimchy\"," +
        "\"postDate\":\"2013-01-30\"," +
        "\"message\":\"trying out Elasticsearch\"" +
    "}";
    
2、利用Map类型,Map类会自动转换成内容相同的JSON
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");    

3、利用第三方库来序列化实体,如jackson
ES已经使用了jackson,因此你可以用它来序列化实体

import com.fasterxml.jackson.databind.*;

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

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

4、利用内置的XContentFactory.jsonBuilder()
ES提供了内置的助手来产生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和endArray来添加数组。而且,field()可以接受多种
对象类型,包括JSON。
如果你需要看JSON的内容,可以使用string()方法。
String json = builder.string();

建立索引
下面是一个例子,把一个JSON文档索引到index为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()
                  )
        .get();
        
你也可以利用String生成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 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();
// isCreated() is true if the document is a new one, false if it has been updated
boolean created = response.isCreated();

想看更多索引操作,可以看REST的index文档

网页链接https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-docs-index.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值