mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-24 17:09:48 +00:00
* #26260 Allow ip_range to accept CIDR notation * #26260 added non-byte-alligned cidr test cases
This commit is contained in:
parent
8f0f024507
commit
3deba0ed1f
@ -42,6 +42,7 @@ import org.elasticsearch.ElasticsearchException;
|
||||
import org.elasticsearch.Version;
|
||||
import org.elasticsearch.common.Explicit;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
import org.elasticsearch.common.collect.Tuple;
|
||||
import org.elasticsearch.common.geo.ShapeRelation;
|
||||
import org.elasticsearch.common.joda.DateMathParser;
|
||||
import org.elasticsearch.common.joda.FormatDateTimeFormatter;
|
||||
@ -57,6 +58,7 @@ import org.joda.time.DateTimeZone;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
@ -353,7 +355,8 @@ public class RangeFieldMapper extends FieldMapper {
|
||||
range = context.parseExternalValue(Range.class);
|
||||
} else {
|
||||
XContentParser parser = context.parser();
|
||||
if (parser.currentToken() == XContentParser.Token.START_OBJECT) {
|
||||
final XContentParser.Token start = parser.currentToken();
|
||||
if (start == XContentParser.Token.START_OBJECT) {
|
||||
RangeFieldType fieldType = fieldType();
|
||||
RangeType rangeType = fieldType.rangeType;
|
||||
String fieldName = null;
|
||||
@ -393,6 +396,8 @@ public class RangeFieldMapper extends FieldMapper {
|
||||
}
|
||||
}
|
||||
range = new Range(rangeType, from, to, includeFrom, includeTo);
|
||||
} else if (fieldType().rangeType == RangeType.IP && start == XContentParser.Token.VALUE_STRING) {
|
||||
range = parseIpRangeFromCidr(parser);
|
||||
} else {
|
||||
throw new MapperParsingException("error parsing field ["
|
||||
+ name() + "], expected an object but got " + parser.currentName());
|
||||
@ -435,6 +440,23 @@ public class RangeFieldMapper extends FieldMapper {
|
||||
}
|
||||
}
|
||||
|
||||
private static Range parseIpRangeFromCidr(final XContentParser parser) throws IOException {
|
||||
final Tuple<InetAddress, Integer> cidr = InetAddresses.parseCidr(parser.text());
|
||||
// create the lower value by zeroing out the host portion, upper value by filling it with all ones.
|
||||
byte[] lower = cidr.v1().getAddress();
|
||||
byte[] upper = lower.clone();
|
||||
for (int i = cidr.v2(); i < 8 * lower.length; i++) {
|
||||
int m = 1 << 7 - (i & 7);
|
||||
lower[i >> 3] &= ~m;
|
||||
upper[i >> 3] |= m;
|
||||
}
|
||||
try {
|
||||
return new Range(RangeType.IP, InetAddress.getByAddress(lower), InetAddress.getByAddress(upper), true, true);
|
||||
} catch (UnknownHostException bogus) {
|
||||
throw new AssertionError(bogus);
|
||||
}
|
||||
}
|
||||
|
||||
/** Enum defining the type of range */
|
||||
public enum RangeType {
|
||||
IP("ip_range") {
|
||||
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.index.mapper;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.apache.lucene.index.DocValuesType;
|
||||
import org.apache.lucene.index.IndexableField;
|
||||
import org.elasticsearch.common.compress.CompressedXContent;
|
||||
import org.elasticsearch.common.network.InetAddresses;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.common.xcontent.XContentType;
|
||||
import org.elasticsearch.index.IndexService;
|
||||
import org.elasticsearch.plugins.Plugin;
|
||||
import org.elasticsearch.test.ESSingleNodeTestCase;
|
||||
import org.junit.Before;
|
||||
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
|
||||
public class IpRangeFieldMapperTests extends ESSingleNodeTestCase {
|
||||
|
||||
private IndexService indexService;
|
||||
private DocumentMapperParser parser;
|
||||
|
||||
@Override
|
||||
protected Collection<Class<? extends Plugin>> getPlugins() {
|
||||
return pluginList(MapperExtrasPlugin.class);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
indexService = createIndex("test");
|
||||
parser = indexService.mapperService().documentMapperParser();
|
||||
}
|
||||
|
||||
public void testStoreCidr() throws Exception {
|
||||
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject("type")
|
||||
.startObject("properties").startObject("field").field("type", "ip_range")
|
||||
.field("store", true);
|
||||
mapping = mapping.endObject().endObject().endObject().endObject();
|
||||
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping.string()));
|
||||
assertEquals(mapping.string(), mapper.mappingSource().toString());
|
||||
final Map<String, String> cases = new HashMap<>();
|
||||
cases.put("192.168.0.0/15", "192.169.255.255");
|
||||
cases.put("192.168.0.0/16", "192.168.255.255");
|
||||
cases.put("192.168.0.0/17", "192.168.127.255");
|
||||
for (final Map.Entry<String, String> entry : cases.entrySet()) {
|
||||
ParsedDocument doc =
|
||||
mapper.parse(SourceToParse.source("test", "type", "1", XContentFactory.jsonBuilder()
|
||||
.startObject()
|
||||
.field("field", entry.getKey())
|
||||
.endObject().bytes(),
|
||||
XContentType.JSON
|
||||
));
|
||||
IndexableField[] fields = doc.rootDoc().getFields("field");
|
||||
assertEquals(3, fields.length);
|
||||
IndexableField dvField = fields[0];
|
||||
assertEquals(DocValuesType.BINARY, dvField.fieldType().docValuesType());
|
||||
IndexableField pointField = fields[1];
|
||||
assertEquals(2, pointField.fieldType().pointDimensionCount());
|
||||
IndexableField storedField = fields[2];
|
||||
assertTrue(storedField.fieldType().stored());
|
||||
String strVal =
|
||||
InetAddresses.toAddrString(InetAddresses.forString("192.168.0.0")) + " : " +
|
||||
InetAddresses.toAddrString(InetAddresses.forString(entry.getValue()));
|
||||
assertThat(storedField.stringValue(), containsString(strVal));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user