Skip to content

Commit

Permalink
Fixes ignore_malformed behaviour for ip fields
Browse files Browse the repository at this point in the history
  • Loading branch information
colings86 committed Mar 16, 2015
1 parent dc4c8c2 commit 68c043f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
Expand Up @@ -37,6 +37,7 @@
import org.apache.lucene.search.Query;
import org.apache.lucene.store.ByteArrayDataOutput;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.Version;
import org.elasticsearch.common.Explicit;
import org.elasticsearch.common.Nullable;
Expand Down Expand Up @@ -235,7 +236,7 @@ protected void parseCreateField(ParseContext context, List<Field> fields) throws
RuntimeException e = null;
try {
innerParseCreateField(context, fields);
} catch (IllegalArgumentException e1) {
} catch (IllegalArgumentException | ElasticsearchIllegalArgumentException e1) {
e = e1;
} catch (MapperParsingException e2) {
e = e2;
Expand Down
Expand Up @@ -20,14 +20,20 @@
package org.elasticsearch.index.mapper.ip;

import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.bootstrap.Elasticsearch;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.ParsedDocument;
import org.elasticsearch.test.ElasticsearchSingleNodeTest;
import org.junit.Test;

import static org.hamcrest.Matchers.*;
import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;

/**
*
Expand Down Expand Up @@ -77,4 +83,45 @@ public void testThatIpv6AddressThrowsException() throws Exception {
}
}

@Test
public void testIgnoreMalformedOption() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1")
.field("type", "ip").field("ignore_malformed", true).endObject().startObject("field2").field("type", "ip")
.field("ignore_malformed", false).endObject().startObject("field3").field("type", "ip").endObject().endObject().endObject()
.endObject().string();

DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse(mapping);

ParsedDocument doc = defaultMapper.parse("type", "1",
XContentFactory.jsonBuilder().startObject().field("field1", "").field("field2", "10.20.30.40").endObject().bytes());
assertThat(doc.rootDoc().getField("field1"), nullValue());
assertThat(doc.rootDoc().getField("field2"), notNullValue());

try {
defaultMapper.parse("type", "1", XContentFactory.jsonBuilder().startObject().field("field2", "").endObject().bytes());
} catch (MapperParsingException e) {
assertThat(e.getCause(), instanceOf(ElasticsearchIllegalArgumentException.class));
}

// Verify that the default is false
try {
defaultMapper.parse("type", "1", XContentFactory.jsonBuilder().startObject().field("field3", "").endObject().bytes());
} catch (MapperParsingException e) {
assertThat(e.getCause(), instanceOf(ElasticsearchIllegalArgumentException.class));
}

// Unless the global ignore_malformed option is set to true
Settings indexSettings = settingsBuilder().put("index.mapping.ignore_malformed", true).build();
defaultMapper = createIndex("test2", indexSettings).mapperService().documentMapperParser().parse(mapping);
doc = defaultMapper.parse("type", "1", XContentFactory.jsonBuilder().startObject().field("field3", "").endObject().bytes());
assertThat(doc.rootDoc().getField("field3"), nullValue());

// This should still throw an exception, since field2 is specifically set to ignore_malformed=false
try {
defaultMapper.parse("type", "1", XContentFactory.jsonBuilder().startObject().field("field2", "").endObject().bytes());
} catch (MapperParsingException e) {
assertThat(e.getCause(), instanceOf(ElasticsearchIllegalArgumentException.class));
}
}

}

0 comments on commit 68c043f

Please sign in to comment.