2

编辑: 这基本上是我想做的,仅在 Java 中

使用 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();
    }
}
4

1 回答 1

1

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

呃,好吧。

此处的代码:从 Java 添加映射到类型 - 我该怎么做?

于 2014-02-27T15:20:56.337 回答