““message”:“trying out Elasticsearch”” +
“}”;
IndexResponse response = client.prepareIndex(“twitter”, “tweet”)
.setSource(json, XContentType.JSON)
.get();
3.2、Map
Map<String, Object> json = new HashMap<String, Object>();
json.put(“user”,“kimchy”);
json.put(“postDate”,new Date());
json.put(“message”,“trying out Elasticsearch”);
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_date”, “2009-11-16T14:12:12”);
source.put(“message”, “trying out Elasticsearch”);
request.source(source);
try {
IndexResponse result = client.index(request, RequestOptions.DEFAULT);
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}
} finally {
EsClient.close(client);
}
}
}
Index API返回结果:
{
“_shards” : {
“total” : 2,
“failed” : 0,
“successful” : 1
},
“_index” : “twitter”,
“_type” : “_doc”,
“_id” : “1”,
“_version” : 1,
“_seq_no” : 0,
“_primary_term” : 1,
“result” : “created”
}
接下来的内容将详细介绍Index API返回结果相关的扩展知识,让大家更加全面的了解Index API内部运行的机制。
5、Index API 内部实现机制
5.1 _shards 返回字段概述
_shards 结构体将反馈索引在副本级的复制信息。
-
total:表示本次请求应该在多少个分片上执行(包含主分片 + 副本)。
-
successful:表示本次请求成功执行的分片个数。
-
failed:表示本次请求为成功执行请求的分片个数。
注:索引操作成功的标志是successful大于0。当索引操作成功返回时,复制分片(副本)可能不会全部启动(默认情况下,只有主服务器是必需的,但是这种行为可以被更改)。
当前单机环境,total为2表示,一个分片存在1主一从,但同一个复制组内的分片不会分布在同一个机器上,故只启动了主分片,复制分片未启动;successful为1表示在主分片上已成功执行,failed为0表示没有执行失败的分片。
5.2 自动创建索引
使用Index API,如果索引不存在,则会自动创建对应的索引(类型映射类型为动态映射机制,具体关于字段映射,将会在Mapping章节中详细介绍)。Elasticsearch数据的组织形式为(index/type/document)。索引的管理(增删改查等API在后续文中会描述)。
自动索引创建可以通过配置来禁用。通过在所有节点的配置文件中添加action.auto_create_index=false来禁用。通过配置index.mapper.dynamic=false可以禁用索引的映射自动创建。
配置是否禁用自动创建索引可基于模式的白名单/黑名单列表模式,例如,action.auto_create_index=aaa*,-bbb*,+ccc*,-* 分别代表 aaa开头的索引自动创建,bbb开头的索引禁止自动创建,禁用索引自动创建。
小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Java工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Java开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
片转存中…(img-a9PfV1X2-1710872478484)]
[外链图片转存中…(img-08dR86bS-1710872478485)]
[外链图片转存中…(img-DoSgxN1r-1710872478485)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Java)
[外链图片转存中…(img-Vm1bgV8S-1710872478486)]