Mapping an IP field
Elasticsearch is used in a lot of systems to collect and search logs, such as Kibana (https://www.elastic.co/products/kibana) and LogStash (https://www.elastic.co/products/logstash). To improve search when using IP addresses, Elasticsearch provides the IPv4 and IPv6 types, which can be used to store IP addresses in an optimized way.
Getting ready
You will need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe of Chapter 1, Getting Started.
How to do it…
You need to define the type of field that contains an IP address as ip
.
Regarding the preceding order example, we can extend it by adding the customer IP, like so:
"customer_ip": { "type": "ip" }
The IP must be in the standard point notation form, as follows:
"customer_ip":"19.18.200.201"
How it works…
When Elasticsearch is processing a document and if a field is an IP one, it tries to convert its value into a numerical form and generates tokens for fast value searching.
The IP has special properties:
index
(the default istrue
): This defines whether the field must be indexed. If not,false
must be used.doc_values
(the default istrue
): This defines whether the field values should be stored in a column-stride fashion to speed up sorting and aggregations.
The other properties (store
, boost
, null_value
, and include_in_all
) work as other base types.
The advantage of using IP fields over strings is more speed in every range and filter and lower resource usage (disk and memory).