Elasticsearch Document Index API详解、原理与示例,java并发编程实战第二版

这篇博客详细介绍了Elasticsearch的Index API,包括如何使用各种方式设置JSON源,如使用第三方类库和Elasticsearch自带类库。还通过Java代码展示了Index API的使用示例,并解释了Index API返回结果的各个字段含义。此外,文章还探讨了自动创建索引、版本工作机制、操作类型以及刷新机制等核心概念。
摘要由CSDN通过智能技术生成

IndexResponse response = client.prepareIndex(“twitter”, “tweet”)

.setSource(json)

.get();

3.3、使用第三方类库

import com.fasterxml.jackson.databind.*;

// instance a json mapper

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

// generate json

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

IndexResponse response = client.prepareIndex(“twitter”, “tweet”)

.setSource(json, XContentType.JSON)

.get();

3.4、使用ElasticSearch自带类库

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

4、Index API使用Demo

当前ElasticSearch为单机版。

package persistent.prestige.elasticsearchdemo;

import java.io.IOException;

import java.util.HashMap;

import java.util.Map;

import org.elasticsearch.action.index.IndexRequest;

import org.elasticsearch.action.index.IndexResponse;

import org.elasticsearch.client.RequestOptions;

import org.elasticsearch.client.RestHighLevelClient;

public class IndexApiDemo {

public static void main(String[] args) {

RestHighLevelClient client = EsClient.getClient();

try {

IndexRequest request = new IndexRequest();

request.index(“twitter”);

request.type("_doc");

request.id(“1”);

Map<String, String> source = new HashMap<>();

source.put(“user”, “dingw”);

source.put(“post_dat

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值