detect_noop now understands null as a valid value

If the source contrains a null value for a field then detect_noop should
consider setting it to null again to be a noop.

Closes #11208
This commit is contained in:
Nik Everett 2015-05-18 12:51:00 -04:00
parent 13e5c19dcf
commit 400abfceaf
2 changed files with 38 additions and 7 deletions

View File

@ -20,7 +20,9 @@
package org.elasticsearch.common.xcontent; package org.elasticsearch.common.xcontent;
import com.google.common.base.Charsets; import com.google.common.base.Charsets;
import com.google.common.base.Objects;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import org.elasticsearch.ElasticsearchException; import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesArray;
@ -260,11 +262,11 @@ public class XContentHelper {
if (modified) { if (modified) {
continue; continue;
} }
if (!checkUpdatesAreUnequal || old == null) { if (!checkUpdatesAreUnequal) {
modified = true; modified = true;
continue; continue;
} }
modified = !old.equals(changesEntry.getValue()); modified = !Objects.equal(old, changesEntry.getValue());
} }
return modified; return modified;
} }

View File

@ -19,7 +19,6 @@
package org.elasticsearch.update; package org.elasticsearch.update;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentFactory;
@ -42,8 +41,11 @@ public class UpdateNoopTests extends ElasticsearchIntegrationTest {
updateAndCheckSource(2, fields("bar", "bir")); updateAndCheckSource(2, fields("bar", "bir"));
updateAndCheckSource(2, fields("bar", "bir")); updateAndCheckSource(2, fields("bar", "bir"));
updateAndCheckSource(3, fields("bar", "foo")); updateAndCheckSource(3, fields("bar", "foo"));
updateAndCheckSource(4, fields("bar", null));
updateAndCheckSource(4, fields("bar", null));
updateAndCheckSource(5, fields("bar", "foo"));
assertEquals(2, totalNoopUpdates()); assertEquals(3, totalNoopUpdates());
} }
@Test @Test
@ -51,13 +53,22 @@ public class UpdateNoopTests extends ElasticsearchIntegrationTest {
// Use random keys so we get random iteration order. // Use random keys so we get random iteration order.
String key1 = 1 + randomAsciiOfLength(3); String key1 = 1 + randomAsciiOfLength(3);
String key2 = 2 + randomAsciiOfLength(3); String key2 = 2 + randomAsciiOfLength(3);
String key3 = 3 + randomAsciiOfLength(3);
updateAndCheckSource(1, fields(key1, "foo", key2, "baz")); updateAndCheckSource(1, fields(key1, "foo", key2, "baz"));
updateAndCheckSource(1, fields(key1, "foo", key2, "baz")); updateAndCheckSource(1, fields(key1, "foo", key2, "baz"));
updateAndCheckSource(2, fields(key1, "foo", key2, "bir")); updateAndCheckSource(2, fields(key1, "foo", key2, "bir"));
updateAndCheckSource(2, fields(key1, "foo", key2, "bir")); updateAndCheckSource(2, fields(key1, "foo", key2, "bir"));
updateAndCheckSource(3, fields(key1, "foo", key2, "foo")); updateAndCheckSource(3, fields(key1, "foo", key2, "foo"));
updateAndCheckSource(4, fields(key1, "foo", key2, null));
updateAndCheckSource(4, fields(key1, "foo", key2, null));
updateAndCheckSource(5, fields(key1, "foo", key2, "foo"));
updateAndCheckSource(6, fields(key1, null, key2, "foo"));
updateAndCheckSource(6, fields(key1, null, key2, "foo"));
updateAndCheckSource(7, fields(key1, null, key2, null));
updateAndCheckSource(7, fields(key1, null, key2, null));
updateAndCheckSource(8, fields(key1, null, key2, null, key3, null));
assertEquals(2, totalNoopUpdates()); assertEquals(5, totalNoopUpdates());
} }
@Test @Test
@ -83,6 +94,7 @@ public class UpdateNoopTests extends ElasticsearchIntegrationTest {
// Use random keys so we get variable iteration order. // Use random keys so we get variable iteration order.
String key1 = 1 + randomAsciiOfLength(3); String key1 = 1 + randomAsciiOfLength(3);
String key2 = 2 + randomAsciiOfLength(3); String key2 = 2 + randomAsciiOfLength(3);
String key3 = 3 + randomAsciiOfLength(3);
updateAndCheckSource(1, XContentFactory.jsonBuilder().startObject() updateAndCheckSource(1, XContentFactory.jsonBuilder().startObject()
.startObject("test") .startObject("test")
.field(key1, "foo") .field(key1, "foo")
@ -108,8 +120,24 @@ public class UpdateNoopTests extends ElasticsearchIntegrationTest {
.field(key1, "foo") .field(key1, "foo")
.field(key2, "foo") .field(key2, "foo")
.endObject().endObject()); .endObject().endObject());
updateAndCheckSource(4, XContentFactory.jsonBuilder().startObject()
.startObject("test")
.field(key1, "foo")
.field(key2, (Object) null)
.endObject().endObject());
updateAndCheckSource(4, XContentFactory.jsonBuilder().startObject()
.startObject("test")
.field(key1, "foo")
.field(key2, (Object) null)
.endObject().endObject());
updateAndCheckSource(5, XContentFactory.jsonBuilder().startObject()
.startObject("test")
.field(key1, "foo")
.field(key2, (Object) null)
.field(key3, (Object) null)
.endObject().endObject());
assertEquals(2, totalNoopUpdates()); assertEquals(3, totalNoopUpdates());
} }
@Test @Test
@ -229,6 +257,7 @@ public class UpdateNoopTests extends ElasticsearchIntegrationTest {
return client().admin().indices().prepareStats("test").setIndexing(true).get().getIndex("test").getTotal().getIndexing().getTotal() return client().admin().indices().prepareStats("test").setIndexing(true).get().getIndex("test").getTotal().getIndexing().getTotal()
.getNoopUpdateCount(); .getNoopUpdateCount();
} }
@Before @Before
public void setup() { public void setup() {
createIndex("test"); createIndex("test");