gpt4 book ai didi

elasticsearch - 从 Java 设置生存时间 (TTL) - 请求示例

转载 作者:行者123 更新时间:2023-12-02 22:33:40 25 4
gpt4 key购买 nike

编辑:
This is basically what I want to do, only in Java

使用 ElasticSearch,我们将文档添加到索引中,绕过 IndexRequest 项到 BulkRequestBuilder。

我希望在经过一段时间后从索引中删除文档(生存时间/ttl)

这可以通过为索引设置默认值或基于每个文档来完成。 两种方法对我来说都很好 .

下面的代码是尝试按文档执行此操作。这没用。我认为这是因为没有为索引启用 TTL。 要么显示我需要添加哪些 Java 代码以启用 TTL,以便下面的代码正常工作,要么显示启用 TTL 的不同代码 + 为 Java 中的索引设置默认 TTL 值 我知道how to do it from the REST API但如果可能的话,我需要从 Java 代码中完成。

    logger.debug("Indexing record ({}): {}", id, map);
final IndexRequest indexRequest = new IndexRequest(_indexName, _documentType, id);
final long debug = indexRequest.ttl();
if (_ttl > 0) {
indexRequest.ttl(_ttl);
System.out.println("Setting TTL to " + _ttl);
System.out.println("IndexRequest now has ttl of " + indexRequest.ttl());
}
indexRequest.source(map);
indexRequest.operationThreaded(false);
bulkRequestBuilder.add(indexRequest);
}

// execute and block until done.
BulkResponse response;
try {
response = bulkRequestBuilder.execute().actionGet();

后来我通过轮询这个方法来检查我的单元测试,但是文档数量永远不会减少。
public long getDocumentCount() throws Exception {
Client client = getClient();
try {
client.admin().indices().refresh(new RefreshRequest(INDEX_NAME)).actionGet();

ActionFuture<CountResponse> response = client.count(new CountRequest(INDEX_NAME).types(DOCUMENT_TYPE));
CountResponse countResponse = response.get();
return countResponse.getCount();
} finally {
client.close();
}
}

最佳答案

经过一整天的谷歌搜索和编写测试程序,我想出了一个如何使用 ttl 和从 Java API 创建基本索引/对象的工作示例。坦率地说,文档中的大多数示例都是微不足道的,一些 JavaDoc 和端到端示例将对我们这些使用非 REST 接口(interface)的人大有帮助。

呃,好吧。

此处代码:Adding mapping to a type from Java - how do I do it?

关于elasticsearch - 从 Java 设置生存时间 (TTL) - 请求示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22063114/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com