使用 ElasticSearch,我们将文档添加到索引中,绕过 IndexRequest 项到 BulkRequestBuilder。
我希望在经过一段时间后从索引中删除文档(生存时间/ttl)
这可以通过为索引设置默认值或基于每个文档来完成。这两种方法对我来说都很好。
下面的代码是尝试按文档执行此操作。这没用。我认为这是因为没有为索引启用 TTL。要么向我展示我需要添加哪些 Java 代码以启用 TTL,以便下面的代码正常工作,要么向我展示启用 TTL 的不同代码 + 为 Java 中的索引设置默认 TTL 值我知道如何从 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();
}
}