java操作ElasticSearch(包含增删改查及基础语法操作)

这篇博客详细介绍了如何使用Java进行Elasticsearch的增删改查操作,包括更新、删除、查询的各种场景,如等值查询、范围查询、模糊查询、多值查询和存在查询等。同时,还讨论了Elasticsearch的Master选举、节点冲突处理、客户端连接策略以及索引、更新和删除文档的内部机制。
摘要由CSDN通过智能技术生成

使用put方式,并自己提供id

类似于下面的格式

PUT /{index}/{type}/{id}

{

“field”: “value”,

}

请求

PUT /website/blog/123

{

“title”: “My first blog entry”,

“text”: “Just trying this out…”,

“date”: “2014/01/01”

}

响应

{

“_index”: “website”,

“_type”: “blog”,

“_id”: “123”,

“_version”: 1,

“created”: true

}

自动生成的 ID 是 URL-safe、 基于 Base64 编码且长度为20个字符的 GUID 字符串。 这些 GUID 字符串由可修改的 FlakeID 模式生成,这种模式允许多个节点并行生成唯一 ID ,且互相之间的冲突概率几乎为零。

1.2.2.更改操作

Java示例代码:

private static void update() throws IOException {

String type = “_doc”;

String index = “test1”;

// 唯一编号

String id = “1”;

UpdateRequest upateRequest = new UpdateRequest();

upateRequest.id(id);

upateRequest.index(index);

upateRequest.type(type);

// 依旧可以使用Map这种集合作为更新条件

Map<String, Object> jsonMap = new HashMap<>();

jsonMap.put(“uid”, 12345);

jsonMap.put(“phone”, 123456789019L);

jsonMap.put(“msgcode”, 2);

jsonMap.put(“sendtime”, “2019-03-14 01:57:04”);

jsonMap.put(“message”, “xuwujing study Elasticsearch”);

upateRequest.doc(jsonMap);

// upsert 方法表示如果数据不存在,那么就新增一条

upateRequest.docAsUpsert(true);

client.update(upateRequest, RequestOptions.DEFAULT);

System.out.println(“更新成功!”);

}

控制台输入

PUT /website/blog/123

{

“title”: “My first blog entry”,

“text”: “Just trying this out…”,

“date”: “2014/01/01”

}

在响应体中,我们能看到 Elasticsearch 已经增加了 _version 字段值,created 标志设置成 false ,是因为相同的索引、类型和 ID 的文档已经存在。

{

“_index”: “website”,

“_type”: “blog”,

“_id”: “123”,

“_version”: 2,

“created”: false

}

1.2.3.删除操作

Java示例代码:

示例一(正常根据ID删除):

private static void delete() throws IOException {

String type = “_doc”;

String index = “test1”;

// 唯一编号

String id = “1”;

DeleteRequest deleteRequest = new DeleteRequest();

deleteRequest.id(id);

deleteRequest.index(index);

deleteRequest.type(type);

// 设置超时时间

deleteRequest.timeout(TimeValue.timeValueMinutes(2));

// 设置刷新策略"wait_for"

// 保持此请求打开,直到刷新使此请求的内容可以搜索为止。此刷新策略与高索引和搜索吞吐量兼容,但它会导致请求等待响应,直到发生刷新

deleteRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);

// 同步删除

DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT);

}

示例二(条件删除):

private static void deleteByQuery() throws IOException {

String type = “_doc”;

String index = “test1”;

DeleteByQueryRequest request = new DeleteByQueryRequest(index,type);

// 设置查询条件

request.setQuery(QueryBuilders.termsQuery(“uid”,1234));

// 同步执行

BulkByScrollResponse bulkResponse = client.deleteByQuery(request, RequestOptions.DEFAULT);

}

DELETE /website/blog/123

如果找到该文档,Elasticsearch 将要返回一个 200 ok 的 HTTP 响应码,和一个类似以下结构的响应体。注意,字段 _version 值已经增加:

{

“found” : true,

“_index” : “website”,

“_type” : “blog”,

“_id” : “123”,

“_version” : 3

}

如果文档没有 找到,我们将得到 404 Not Found 的响应码和类似这样的响应体:

{

“found” : false,

“_index” : “website”,

“_type” : “blog”,

“_id” : “123”,

“_version” : 4

}

1.2.4.查询操作

查询API

等值(term查询:QueryBuilders.termQuery(name,value);

多值(terms)查询:QueryBuilders.termsQuery(name,value,value2,value3…);

范围(range)查询:QueryBuilders.rangeQuery(name).gte(value).lte(value);

存在(exists)查询:QueryBuilders.ex

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值