Unfortunately, I couldn't find any pointers in the documentation. I'd like to have some IP ranges saved and be able to:
Perform a query for an IP address.
Perform a query for a CIDR range.
I got #1 working with query string query (e.g., {"query":{"query_string":{"query":"iprangefield:172.16.0.0"}}}) but it doesn't look elegant and I'm wondering if there's a better way to do this. Something like the term query?
The other issue I have is that I don't know how to "match" a document. I need a way to see if there's already a document with the same field. Also, a way to check if a subnet is part of an existing document (larger prefix).
Though, I still don't know how to match an exact range to avoid duplication. Do you have any idea? I want to avoid translating CIDR to two IP addresses get the result set, then post-process it to see if I get a match.
Matching the exact range is a bit more involved as range fields aren't meant for this sort of query.
One strategy would be to change your mapping and add a keywordmulti-field that holds the original network address and mask. You can then do an exact match against that using a match query.
I see, I was trying to avoid that (adding an extra field) if possible. So no CIDR for search.
Thank you again @whatgeorgemade! In this case I use the workarounds for CIDR and do pre+post-processing. I think I'll still skip adding an extra field, since I won't be able to have everything in ES. I'll query the range and process the result set to see if there's an exact match.
If you only have a single IP in your data, then you do not need an iprange as long as your ranges can be expressed using CIDR. You can use the ip datatype
Thanks, but I'll need both and want to avoid adding too many fields (or documents) and use different queries. In the long run, I'll see if using ranges was the right move.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.