From e52dbf4fdaf148cfd45b92c8301fe3d6af468ec2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Dev=C3=A8ze?= Date: Tue, 13 Sep 2011 17:57:38 +0200 Subject: [PATCH] fix bug when adding to BulkRequest with no TTL, add simple unit test for that --- .../action/bulk/BulkRequest.java | 2 +- .../action/index/IndexRequest.java | 5 ++- .../action/bulk/BulkActionTests.java | 35 +++++++++++++++++++ .../action/bulk/simple-bulk.json | 5 +++ 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 modules/elasticsearch/src/test/java/org/elasticsearch/action/bulk/BulkActionTests.java create mode 100644 modules/elasticsearch/src/test/java/org/elasticsearch/action/bulk/simple-bulk.json diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java index c9f736a19d2..9ceeeeec307 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/bulk/BulkRequest.java @@ -115,7 +115,7 @@ public class BulkRequest implements ActionRequest { String routing = null; String parent = null; String timestamp = null; - long ttl = -1; + Long ttl = null; String opType = null; long version = 0; VersionType versionType = VersionType.INTERNAL; diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/index/IndexRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/index/IndexRequest.java index bebebf4f6cc..d3251492be9 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/index/IndexRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/index/IndexRequest.java @@ -289,7 +289,10 @@ public class IndexRequest extends ShardReplicationOperationRequest { } // Sets the relative ttl value. It musts be > 0 as it makes little sense otherwise. - public IndexRequest ttl(long ttl) throws ElasticSearchGenerationException { + public IndexRequest ttl(Long ttl) throws ElasticSearchGenerationException { + if (ttl == null) { + return this; + } if (ttl <= 0) { throw new ElasticSearchIllegalArgumentException("TTL value must be > 0. Illegal value provided [" + ttl + "]"); } diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/action/bulk/BulkActionTests.java b/modules/elasticsearch/src/test/java/org/elasticsearch/action/bulk/BulkActionTests.java new file mode 100644 index 00000000000..a9cb2644e62 --- /dev/null +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/action/bulk/BulkActionTests.java @@ -0,0 +1,35 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.action.bulk; + +import org.testng.annotations.Test; + +import static org.elasticsearch.common.io.Streams.*; +import static org.hamcrest.MatcherAssert.*; +import static org.hamcrest.Matchers.*; + +public class BulkActionTests { + @Test public void testSimpleBulk() throws Exception { + String bulkAction = copyToStringFromClasspath("/org/elasticsearch/action/bulk/simple-bulk.json"); + BulkRequest bulkRequest = new BulkRequest(); + bulkRequest.add(bulkAction.getBytes(), 0, bulkAction.length(), true); + assertThat(bulkRequest.numberOfActions(), equalTo(3)); + } +} diff --git a/modules/elasticsearch/src/test/java/org/elasticsearch/action/bulk/simple-bulk.json b/modules/elasticsearch/src/test/java/org/elasticsearch/action/bulk/simple-bulk.json new file mode 100644 index 00000000000..b6f1b76ddf9 --- /dev/null +++ b/modules/elasticsearch/src/test/java/org/elasticsearch/action/bulk/simple-bulk.json @@ -0,0 +1,5 @@ +{ "index" : { "_index" : "test", "_type" : "type1", "_id" : "1" } } +{ "field1" : "value1" } +{ "delete" : { "_index" : "test", "_type" : "type1", "_id" : "2" } } +{ "create" : { "_index" : "test", "_type" : "type1", "_id" : "3" } } +{ "field1" : "value3" }