Skip to content

Commit a43a5af

Browse files
committed
Generate IndexOrDocValuesQuery for term queries in IpFieldMapper
1 parent 56de057 commit a43a5af

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

server/src/main/java/org/elasticsearch/index/mapper/IpFieldMapper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ public Query termQuery(Object value, @Nullable SearchExecutionContext context) {
347347
query = InetAddressPoint.newExactQuery(name(), address);
348348
}
349349
}
350-
if (isIndexed()) {
350+
if (isIndexed() && hasDocValues()) {
351+
return new IndexOrDocValuesQuery(query, convertToDocValuesQuery(query));
352+
} else if (isIndexed()) {
351353
return query;
352354
} else {
353355
return convertToDocValuesQuery(query);

server/src/test/java/org/elasticsearch/index/mapper/IpFieldTypeTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,33 @@ public void testTermQuery() {
5656
MappedFieldType ft = new IpFieldMapper.IpFieldType("field");
5757

5858
String ip = "2001:db8::2:1";
59+
Query query = InetAddressPoint.newExactQuery("field", InetAddresses.forString(ip));
60+
assertEquals(new IndexOrDocValuesQuery(query, convertToDocValuesQuery(query)), ft.termQuery(ip, MOCK_CONTEXT));
61+
62+
ip = "192.168.1.7";
63+
query = InetAddressPoint.newExactQuery("field", InetAddresses.forString(ip));
64+
assertEquals(new IndexOrDocValuesQuery(query, convertToDocValuesQuery(query)), ft.termQuery(ip, MOCK_CONTEXT));
65+
66+
ip = "2001:db8::2:1";
67+
String prefix = ip + "/64";
68+
query = InetAddressPoint.newPrefixQuery("field", InetAddresses.forString(ip), 64);
69+
assertEquals(new IndexOrDocValuesQuery(query, convertToDocValuesQuery(query)), ft.termQuery(prefix, MOCK_CONTEXT));
70+
71+
ip = "192.168.1.7";
72+
prefix = ip + "/16";
73+
query = InetAddressPoint.newPrefixQuery("field", InetAddresses.forString(ip), 16);
74+
assertEquals(new IndexOrDocValuesQuery(query, convertToDocValuesQuery(query)), ft.termQuery(prefix, MOCK_CONTEXT));
75+
76+
ft = new IpFieldMapper.IpFieldType("field", true, false);
77+
78+
ip = "2001:db8::2:1";
5979
assertEquals(InetAddressPoint.newExactQuery("field", InetAddresses.forString(ip)), ft.termQuery(ip, MOCK_CONTEXT));
6080

6181
ip = "192.168.1.7";
6282
assertEquals(InetAddressPoint.newExactQuery("field", InetAddresses.forString(ip)), ft.termQuery(ip, MOCK_CONTEXT));
6383

6484
ip = "2001:db8::2:1";
65-
String prefix = ip + "/64";
85+
prefix = ip + "/64";
6686
assertEquals(InetAddressPoint.newPrefixQuery("field", InetAddresses.forString(ip), 64), ft.termQuery(prefix, MOCK_CONTEXT));
6787

6888
ip = "192.168.1.7";

0 commit comments

Comments
 (0)