geoip.asciidoc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. [[geoip-processor]]
  2. === GeoIP processor
  3. ++++
  4. <titleabbrev>GeoIP</titleabbrev>
  5. ++++
  6. The `geoip` processor adds information about the geographical location of an
  7. IPv4 or IPv6 address.
  8. [[geoip-automatic-updates]]
  9. By default, the processor uses the GeoLite2 City, GeoLite2 Country, and GeoLite2
  10. ASN GeoIP2 databases from
  11. http://dev.maxmind.com/geoip/geoip2/geolite2/[MaxMind], shared under the
  12. CCA-ShareAlike 4.0 license. {es} automatically downloads updates for
  13. these databases from the Elastic GeoIP endpoint:
  14. https://geoip.elastic.co/v1/database. To get download statistics for these
  15. updates, use the <<geoip-stats-api,GeoIP stats API>>.
  16. If your cluster can't connect to the Elastic GeoIP endpoint or you want to
  17. manage your own updates, see <<manage-geoip-database-updates>>.
  18. [[using-ingest-geoip]]
  19. ==== Using the `geoip` Processor in a Pipeline
  20. [[ingest-geoip-options]]
  21. .`geoip` options
  22. [options="header"]
  23. |======
  24. | Name | Required | Default | Description
  25. | `field` | yes | - | The field to get the ip address from for the geographical lookup.
  26. | `target_field` | no | geoip | The field that will hold the geographical information looked up from the MaxMind database.
  27. | `database_file` | no | GeoLite2-City.mmdb | The database filename referring to a database the module ships with (GeoLite2-City.mmdb, GeoLite2-Country.mmdb, or GeoLite2-ASN.mmdb) or a custom database in the `ingest-geoip` config directory.
  28. | `properties` | no | [`continent_name`, `country_iso_code`, `country_name`, `region_iso_code`, `region_name`, `city_name`, `location`] * | Controls what properties are added to the `target_field` based on the geoip lookup.
  29. | `ignore_missing` | no | `false` | If `true` and `field` does not exist, the processor quietly exits without modifying the document
  30. | `first_only` | no | `true` | If `true` only first found geoip data will be returned, even if `field` contains array
  31. |======
  32. *Depends on what is available in `database_file`:
  33. * If the GeoLite2 City database is used, then the following fields may be added under the `target_field`: `ip`,
  34. `country_iso_code`, `country_name`, `continent_name`, `region_iso_code`, `region_name`, `city_name`, `timezone`, `latitude`, `longitude`
  35. and `location`. The fields actually added depend on what has been found and which properties were configured in `properties`.
  36. * If the GeoLite2 Country database is used, then the following fields may be added under the `target_field`: `ip`,
  37. `country_iso_code`, `country_name` and `continent_name`. The fields actually added depend on what has been found and which properties
  38. were configured in `properties`.
  39. * If the GeoLite2 ASN database is used, then the following fields may be added under the `target_field`: `ip`,
  40. `asn`, `organization_name` and `network`. The fields actually added depend on what has been found and which properties were configured
  41. in `properties`.
  42. Here is an example that uses the default city database and adds the geographical information to the `geoip` field based on the `ip` field:
  43. [source,console]
  44. --------------------------------------------------
  45. PUT _ingest/pipeline/geoip
  46. {
  47. "description" : "Add geoip info",
  48. "processors" : [
  49. {
  50. "geoip" : {
  51. "field" : "ip"
  52. }
  53. }
  54. ]
  55. }
  56. PUT my-index-000001/_doc/my_id?pipeline=geoip
  57. {
  58. "ip": "8.8.8.8"
  59. }
  60. GET my-index-000001/_doc/my_id
  61. --------------------------------------------------
  62. Which returns:
  63. [source,console-result]
  64. --------------------------------------------------
  65. {
  66. "found": true,
  67. "_index": "my-index-000001",
  68. "_id": "my_id",
  69. "_version": 1,
  70. "_seq_no": 55,
  71. "_primary_term": 1,
  72. "_source": {
  73. "ip": "8.8.8.8",
  74. "geoip": {
  75. "continent_name": "North America",
  76. "country_name": "United States",
  77. "country_iso_code": "US",
  78. "location": { "lat": 37.751, "lon": -97.822 }
  79. }
  80. }
  81. }
  82. --------------------------------------------------
  83. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term":1/"_primary_term" : $body._primary_term/]
  84. Here is an example that uses the default country database and adds the
  85. geographical information to the `geo` field based on the `ip` field. Note that
  86. this database is included in the module. So this:
  87. [source,console]
  88. --------------------------------------------------
  89. PUT _ingest/pipeline/geoip
  90. {
  91. "description" : "Add geoip info",
  92. "processors" : [
  93. {
  94. "geoip" : {
  95. "field" : "ip",
  96. "target_field" : "geo",
  97. "database_file" : "GeoLite2-Country.mmdb"
  98. }
  99. }
  100. ]
  101. }
  102. PUT my-index-000001/_doc/my_id?pipeline=geoip
  103. {
  104. "ip": "8.8.8.8"
  105. }
  106. GET my-index-000001/_doc/my_id
  107. --------------------------------------------------
  108. returns this:
  109. [source,console-result]
  110. --------------------------------------------------
  111. {
  112. "found": true,
  113. "_index": "my-index-000001",
  114. "_id": "my_id",
  115. "_version": 1,
  116. "_seq_no": 65,
  117. "_primary_term": 1,
  118. "_source": {
  119. "ip": "8.8.8.8",
  120. "geo": {
  121. "continent_name": "North America",
  122. "country_name": "United States",
  123. "country_iso_code": "US",
  124. }
  125. }
  126. }
  127. --------------------------------------------------
  128. // TESTRESPONSE[s/"_seq_no": \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  129. Not all IP addresses find geo information from the database, When this
  130. occurs, no `target_field` is inserted into the document.
  131. Here is an example of what documents will be indexed as when information for "80.231.5.0"
  132. cannot be found:
  133. [source,console]
  134. --------------------------------------------------
  135. PUT _ingest/pipeline/geoip
  136. {
  137. "description" : "Add geoip info",
  138. "processors" : [
  139. {
  140. "geoip" : {
  141. "field" : "ip"
  142. }
  143. }
  144. ]
  145. }
  146. PUT my-index-000001/_doc/my_id?pipeline=geoip
  147. {
  148. "ip": "80.231.5.0"
  149. }
  150. GET my-index-000001/_doc/my_id
  151. --------------------------------------------------
  152. Which returns:
  153. [source,console-result]
  154. --------------------------------------------------
  155. {
  156. "_index" : "my-index-000001",
  157. "_id" : "my_id",
  158. "_version" : 1,
  159. "_seq_no" : 71,
  160. "_primary_term": 1,
  161. "found" : true,
  162. "_source" : {
  163. "ip" : "80.231.5.0"
  164. }
  165. }
  166. --------------------------------------------------
  167. // TESTRESPONSE[s/"_seq_no" : \d+/"_seq_no" : $body._seq_no/ s/"_primary_term" : 1/"_primary_term" : $body._primary_term/]
  168. [[ingest-geoip-mappings-note]]
  169. ===== Recognizing Location as a Geopoint
  170. Although this processor enriches your document with a `location` field containing
  171. the estimated latitude and longitude of the IP address, this field will not be
  172. indexed as a {ref}/geo-point.html[`geo_point`] type in Elasticsearch without explicitly defining it
  173. as such in the mapping.
  174. You can use the following mapping for the example index above:
  175. [source,console]
  176. --------------------------------------------------
  177. PUT my_ip_locations
  178. {
  179. "mappings": {
  180. "properties": {
  181. "geoip": {
  182. "properties": {
  183. "location": { "type": "geo_point" }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. --------------------------------------------------
  190. ////
  191. [source,console]
  192. --------------------------------------------------
  193. PUT _ingest/pipeline/geoip
  194. {
  195. "description" : "Add geoip info",
  196. "processors" : [
  197. {
  198. "geoip" : {
  199. "field" : "ip"
  200. }
  201. }
  202. ]
  203. }
  204. PUT my_ip_locations/_doc/1?refresh=true&pipeline=geoip
  205. {
  206. "ip": "8.8.8.8"
  207. }
  208. GET /my_ip_locations/_search
  209. {
  210. "query": {
  211. "bool": {
  212. "must": {
  213. "match_all": {}
  214. },
  215. "filter": {
  216. "geo_distance": {
  217. "distance": "1m",
  218. "geoip.location": {
  219. "lon": -97.822,
  220. "lat": 37.751
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. --------------------------------------------------
  228. // TEST[continued]
  229. [source,console-result]
  230. --------------------------------------------------
  231. {
  232. "took" : 3,
  233. "timed_out" : false,
  234. "_shards" : {
  235. "total" : 1,
  236. "successful" : 1,
  237. "skipped" : 0,
  238. "failed" : 0
  239. },
  240. "hits" : {
  241. "total" : {
  242. "value": 1,
  243. "relation": "eq"
  244. },
  245. "max_score" : 1.0,
  246. "hits" : [
  247. {
  248. "_index" : "my_ip_locations",
  249. "_id" : "1",
  250. "_score" : 1.0,
  251. "_source" : {
  252. "geoip" : {
  253. "continent_name" : "North America",
  254. "country_name" : "United States",
  255. "country_iso_code" : "US",
  256. "location" : {
  257. "lon" : -97.822,
  258. "lat" : 37.751
  259. }
  260. },
  261. "ip" : "8.8.8.8"
  262. }
  263. }
  264. ]
  265. }
  266. }
  267. --------------------------------------------------
  268. // TESTRESPONSE[s/"took" : 3/"took" : $body.took/]
  269. ////
  270. [[manage-geoip-database-updates]]
  271. ==== Manage your own GeoIP2 database updates
  272. If you can't <<geoip-automatic-updates,automatically update>> your GeoIP2
  273. databases from the Elastic endpoint, you have a few other options:
  274. * <<use-proxy-geoip-endpoint,Use a proxy endpoint>>
  275. * <<use-custom-geoip-endpoint,Use a custom endpoint>>
  276. * <<manually-update-geoip-databases,Manually update your GeoIP2 databases>>
  277. [[use-proxy-geoip-endpoint]]
  278. **Use a proxy endpoint**
  279. If you can't connect directly to the Elastic GeoIP endpoint, consider setting up
  280. a secure proxy. You can then specify the proxy endpoint URL in the
  281. <<ingest-geoip-downloader-endpoint,`ingest.geoip.downloader.endpoint`>> setting
  282. of each node’s `elasticsearch.yml` file.
  283. [[use-custom-geoip-endpoint]]
  284. **Use a custom endpoint**
  285. You can create a service that mimics the Elastic GeoIP endpoint. You can then
  286. get automatic updates from this service.
  287. . Download your `.mmdb` database files from the
  288. http://dev.maxmind.com/geoip/geoip2/geolite2[MaxMind site].
  289. . Copy your database files to a single directory.
  290. . From your {es} directory, run:
  291. +
  292. [source,sh]
  293. ----
  294. ./bin/elasticsearch-geoip -s my/source/dir [-t target/directory]
  295. ----
  296. . Serve the static database files from your directory. For example, you can use
  297. Docker to serve the files from an nginx server:
  298. +
  299. [source,sh]
  300. ----
  301. docker run -v my/source/dir:/usr/share/nginx/html:ro nginx
  302. ----
  303. . Specify the service's endpoint URL in the
  304. <<ingest-geoip-downloader-endpoint,`ingest.geoip.downloader.endpoint`>> setting
  305. of each node’s `elasticsearch.yml` file.
  306. +
  307. By default, {es} checks the endpoint for updates every three days. To use
  308. another polling interval, use the <<cluster-update-settings,update cluster
  309. settings API>> to set
  310. <<ingest-geoip-downloader-poll-interval,`ingest.geoip.downloader.poll.interval`>>.
  311. [[manually-update-geoip-databases]]
  312. **Manually update your GeoIP2 databases**
  313. . Use the <<cluster-update-settings,update cluster settings API>> to set
  314. `ingest.geoip.downloader.enabled` to `false`. This disables automatic updates
  315. that may overwrite your database changes. This also deletes all downloaded
  316. databases.
  317. . Download your `.mmdb` database files from the
  318. http://dev.maxmind.com/geoip/geoip2/geolite2[MaxMind site].
  319. +
  320. You can also use custom city, country, and ASN `.mmdb` files. These files must
  321. be uncompressed and use the respective `-City.mmdb`, `-Country.mmdb`, or
  322. `-ASN.mmdb` extensions.
  323. . Copy the database files to `$ES_CONFIG/ingest-geoip`.
  324. . In your `geoip` processors, configure the `database_file` parameter to use a
  325. custom database file.
  326. [[ingest-geoip-settings]]
  327. ===== Node Settings
  328. The `geoip` processor supports the following setting:
  329. `ingest.geoip.cache_size`::
  330. The maximum number of results that should be cached. Defaults to `1000`.
  331. Note that these settings are node settings and apply to all `geoip` processors, i.e. there is one cache for all defined `geoip` processors.
  332. [[geoip-cluster-settings]]
  333. ===== Cluster settings
  334. [[ingest-geoip-downloader-enabled]]
  335. `ingest.geoip.downloader.enabled`::
  336. (<<dynamic-cluster-setting,Dynamic>>, Boolean)
  337. If `true`, {es} automatically downloads and manages updates for GeoIP2 databases
  338. from the `ingest.geoip.downloader.endpoint`. If `false`, {es} does not download
  339. updates and deletes all downloaded databases. Defaults to `true`.
  340. [[ingest-geoip-downloader-endpoint]]
  341. `ingest.geoip.downloader.endpoint`::
  342. (<<static-cluster-setting,Static>>, string)
  343. Endpoint URL used to download updates for GeoIP2 databases. Defaults to
  344. `https://geoip.elastic.co/v1/database`. {es} stores downloaded database files in
  345. each node's <<es-tmpdir,temporary directory>> at
  346. `$ES_TMPDIR/geoip-databases/<node_id>`.
  347. [[ingest-geoip-downloader-poll-interval]]
  348. `ingest.geoip.downloader.poll.interval`::
  349. (<<dynamic-cluster-setting,Dynamic>>, <<time-units,time value>>)
  350. How often {es} checks for GeoIP2 database updates at the
  351. `ingest.geoip.downloader.endpoint`. Must be greater than `1d` (one day). Defaults
  352. to `3d` (three days).