required _routing fails when path points to an integer field, closes #1357.

This commit is contained in:
Shay Banon 2011-09-24 01:59:21 +03:00
parent d954a93d9d
commit 9d9133a451
9 changed files with 65 additions and 1 deletions

View File

@ -307,5 +307,9 @@ public class ByteFieldMapper extends NumberFieldMapper<Byte> {
}
return null;
}
@Override public String numericAsString() {
return Byte.toString(number);
}
}
}

View File

@ -308,5 +308,9 @@ public class DoubleFieldMapper extends NumberFieldMapper<Double> {
}
return null;
}
@Override public String numericAsString() {
return Double.toString(number);
}
}
}

View File

@ -304,5 +304,9 @@ public class FloatFieldMapper extends NumberFieldMapper<Float> {
}
return null;
}
@Override public String numericAsString() {
return Float.toString(number);
}
}
}

View File

@ -308,5 +308,9 @@ public class IntegerFieldMapper extends NumberFieldMapper<Integer> {
}
return null;
}
@Override public String numericAsString() {
return Integer.toString(number);
}
}
}

View File

@ -307,5 +307,9 @@ public class LongFieldMapper extends NumberFieldMapper<Long> {
}
return null;
}
@Override public String numericAsString() {
return Long.toString(number);
}
}
}

View File

@ -248,5 +248,7 @@ public abstract class NumberFieldMapper<T extends Number> extends AbstractFieldM
@Override public Reader readerValue() {
return null;
}
public abstract String numericAsString();
}
}

View File

@ -307,5 +307,9 @@ public class ShortFieldMapper extends NumberFieldMapper<Short> {
}
return null;
}
@Override public String numericAsString() {
return Short.toString(number);
}
}
}

View File

@ -33,6 +33,7 @@ import org.elasticsearch.index.mapper.MergeMappingException;
import org.elasticsearch.index.mapper.ParseContext;
import org.elasticsearch.index.mapper.RootMapper;
import org.elasticsearch.index.mapper.core.AbstractFieldMapper;
import org.elasticsearch.index.mapper.core.NumberFieldMapper;
import java.io.IOException;
import java.util.Map;
@ -156,10 +157,23 @@ public class RoutingFieldMapper extends AbstractFieldMapper<String> implements I
String routing = context.sourceToParse().routing();
if (path != null && routing != null) {
// we have a path, check if we can validate we have the same routing value as the one in the doc...
String value = context.doc().get(path);
String value = null;
Fieldable field = context.doc().getFieldable(path);
if (field != null) {
value = field.stringValue();
if (value == null) {
// maybe its a numeric field...
if (field instanceof NumberFieldMapper.CustomNumericField) {
value = ((NumberFieldMapper.CustomNumericField) field).numericAsString();
}
}
}
if (value == null) {
value = context.ignoredValue(path);
}
if (value == null) {
// maybe its a numeric field
}
if (!routing.equals(value)) {
throw new MapperParsingException("External routing [" + routing + "] and document path routing [" + value + "] mismatch");
}

View File

@ -300,4 +300,28 @@ public class SimpleRoutingTests extends AbstractNodesTests {
assertThat(client.prepareGet("test", "type1", "1").setRouting("0").execute().actionGet().exists(), equalTo(true));
}
}
@Test public void testRequiredRoutingWithPathNumericType() throws Exception {
client.admin().indices().prepareDelete().execute().actionGet();
client.admin().indices().prepareCreate("test")
.addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1")
.startObject("_routing").field("required", true).field("path", "routing_field").endObject()
.endObject().endObject())
.execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
logger.info("--> indexing with id [1], and routing [0]");
client.prepareIndex("test", "type1", "1").setSource("field", "value1", "routing_field", 0).execute().actionGet();
client.admin().indices().prepareRefresh().execute().actionGet();
logger.info("--> verifying get with no routing, should not find anything");
for (int i = 0; i < 5; i++) {
assertThat(client.prepareGet("test", "type1", "1").execute().actionGet().exists(), equalTo(false));
}
logger.info("--> verifying get with routing, should find");
for (int i = 0; i < 5; i++) {
assertThat(client.prepareGet("test", "type1", "1").setRouting("0").execute().actionGet().exists(), equalTo(true));
}
}
}