geoip.asciidoc 13 KB

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