Fix parsing of ip range queries. (#25768)

Closes #25636
This commit is contained in:
Adrien Grand 2017-07-19 14:12:54 +02:00 committed by GitHub
parent 01f083ca83
commit 94a98daa37
2 changed files with 16 additions and 1 deletions

View File

@ -452,7 +452,14 @@ public class RangeFieldMapper extends FieldMapper {
}
@Override
public InetAddress parse(Object value, boolean coerce) {
return value instanceof InetAddress ? (InetAddress) value : InetAddresses.forString((String) value);
if (value instanceof InetAddress) {
return (InetAddress) value;
} else {
if (value instanceof BytesRef) {
value = ((BytesRef) value).utf8ToString();
}
return InetAddresses.forString(value.toString());
}
}
@Override
public InetAddress minValue() {

View File

@ -29,10 +29,12 @@ import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.queries.BinaryDocValuesRangeQuery;
import org.apache.lucene.search.IndexOrDocValuesQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.common.geo.ShapeRelation;
import org.elasticsearch.common.joda.Joda;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.RangeFieldMapper.RangeType;
@ -267,4 +269,10 @@ public class RangeFieldTypeTests extends FieldTypeTestCase {
return (Float)from + DISTANCE;
}
}
public void testParseIp() {
assertEquals(InetAddresses.forString("::1"), RangeFieldMapper.RangeType.IP.parse(InetAddresses.forString("::1"), randomBoolean()));
assertEquals(InetAddresses.forString("::1"), RangeFieldMapper.RangeType.IP.parse("::1", randomBoolean()));
assertEquals(InetAddresses.forString("::1"), RangeFieldMapper.RangeType.IP.parse(new BytesRef("::1"), randomBoolean()));
}
}