Skip to content

Commit

Permalink
Add utility method to parse an IP to BytesRef (#95291)
Browse files Browse the repository at this point in the history
This adds a QL utility method that parses an IP address into a BytesRef
object.
  • Loading branch information
bpintea authored Apr 17, 2023
1 parent 5eea71d commit a3faa87
Showing 1 changed file with 8 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
*/
package org.elasticsearch.xpack.ql.util;

import org.apache.lucene.document.InetAddressPoint;
import org.apache.lucene.search.spell.LevenshteinDistance;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.CollectionUtil;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.network.InetAddresses;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.xcontent.ToXContent;
Expand Down Expand Up @@ -322,6 +325,11 @@ public static Number parseIntegral(String string) throws QlIllegalArgumentExcept
}
}

public static BytesRef parseIP(String string) {
var inetAddress = InetAddresses.forString(string);
return new BytesRef(InetAddressPoint.encode(inetAddress));
}

public static String ordinal(int i) {
return switch (i % 100) {
case 11, 12, 13 -> i + "th";
Expand Down

0 comments on commit a3faa87

Please sign in to comment.