From 2e4d18b519a4074d2b0c25f51147f334bfc9c6a1 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Tue, 21 May 2013 17:32:41 +0200 Subject: [PATCH] Fixing percolation of documents with TTL set When a type is configured with a TTL, percolation of documents of this type was not possible. This fix ignores the TTL for percolation instead of throwing an exception that the document is already expired. Closes #2975 --- .../index/mapper/internal/TTLFieldMapper.java | 2 +- .../percolator/SimplePercolatorTests.java | 43 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java index 941c5068001..a1d28d55b72 100644 --- a/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/internal/TTLFieldMapper.java @@ -190,7 +190,7 @@ public class TTLFieldMapper extends LongFieldMapper implements InternalMapper, R @Override protected Field innerParseCreateField(ParseContext context) throws IOException, AlreadyExpiredException { - if (enabledState.enabled) { + if (enabledState.enabled && !context.sourceToParse().flyweight()) { long ttl = context.sourceToParse().ttl(); if (ttl <= 0 && defaultTTL > 0) { // no ttl provided so we use the default value ttl = defaultTTL; diff --git a/src/test/java/org/elasticsearch/test/integration/percolator/SimplePercolatorTests.java b/src/test/java/org/elasticsearch/test/integration/percolator/SimplePercolatorTests.java index 847aed7075d..863b576c086 100644 --- a/src/test/java/org/elasticsearch/test/integration/percolator/SimplePercolatorTests.java +++ b/src/test/java/org/elasticsearch/test/integration/percolator/SimplePercolatorTests.java @@ -486,4 +486,47 @@ public class SimplePercolatorTests extends AbstractNodesTests { assertThat(percolate.getMatches(), hasItem("kuku")); } + @Test + public void testThatPercolatingWithTimeToLiveWorks() throws Exception { + try { + client.admin().indices().prepareDelete("test").execute().actionGet(); + } catch (Exception e) { + // ignore + } + try { + client.admin().indices().prepareDelete("_percolator").execute().actionGet(); + } catch (Exception e) { + // ignore + } + + String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1") + .startObject("_ttl").field("enabled", true).field("default", "60d").endObject() + .startObject("_timestamp").field("enabled", true).endObject() + .endObject().endObject().string(); + + client.admin().indices().prepareCreate("test") + .setSettings(settingsBuilder().put("index.number_of_shards", 2)) + .addMapping("type1", mapping) + .execute().actionGet(); + client.admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet(); + + client.prepareIndex("_percolator", "test", "kuku").setSource(jsonBuilder() + .startObject() + .startObject("query") + .startObject("term") + .field("field1", "value1") + .endObject() + .endObject() + .endObject() + ).execute().actionGet(); + + PercolateResponse percolateResponse = client.preparePercolate("test", "type1").setSource(jsonBuilder() + .startObject() + .startObject("doc") + .field("field1", "value1") + .endObject() + .endObject() + ).execute().actionGet(); + assertThat(percolateResponse.getMatches(), hasItem("kuku")); + } }