Geofile_ elasticsearch geo queries

Enterprise Pricing Articles Sign in Free 30-Day Trial GeoFile: Elasticsearch Geo Queries Share on Twitter Share on Facebook Share on Google+ Vote on Hacker News Published Jan 19, 2017

GeoFile is a series dedicated to looking at geographical data, its features, and uses. Data recovery expert In this article, we’ll be covering Elasticsearch and its Geo mapping datatypes, geo_point and geo_shape, and Geo querying capabilities. Data recovery jobs We’ll show you how to construct your mappings and demonstrate how to query some data.

The use of GeoData in databases has risen in popularity.


Database concepts 6th edition In other GeoFile articles, we’ve covered database extensions like PostGIS for PostgreSQL, as well as MongoDB and Redis’s Geo querying capabilities that come out of the box. Database kernel In this article, we’ll look at Elasticsearch’s Geo queries, how you can set up mappings and indices, and provide you with some example of how you can query your data.

While Elasticsearch may not be your first choice when searching through your GeoData, its developers have been improving on its capabilities since 1.x by adding and enhancing querying features. Raid 5 data recovery software For this article, we’ll be using version 2.4, but you should be able to use these queries from 2.x on. Data recovery pro Elasticsearch and GeoData

Geo Point allows you to store data as latitude and longitude coordinate pairs. Data recovery tools mac Use this field type when you want to filter data for distances between points, search within bounding boxes, or when using aggregations. Z wave database There are a lot of features and options that you can specify which are beyond the scope of this article. Database orm We’ll cover a couple here, but you can view the options for Geo Bounding Box, Geo Distance, and Geo Aggregations in Elasticsearch’s documentation.

Use Geo-Shape when you have GeoData that represents a shape, or when you want to query points within a shape. Database workbench geo_shape data must be encoded in GeoJSON format which is converted into strings representing long/lat coordinate pairs on a grid of Geohash cells. Database best practices Since Elasticsearch indexes shapes as terms, it’s simple for it to determine the relationships between shapes, which can be queried using intersects, disjoint, contains, or within query spatial relation operators.

Unfortunately, geo-point and geo-shape cannot be queried together. Database schema For example, if you want to get all the cities within a specified polygon, you cannot use cities that are indexed with geo-point. Data recovery external hard drive They must be indexed using a “type”: “Point” in GeoJSON and indexed as geo-shape. Database 1 to many We’ll see how this works later. Data recovery galaxy s6 However, note that you must determine how you’ll query your data prior to indexing it in Elasticsearch otherwise you’ll end up remapping and reindexing your data. Os x data recovery software The Data and Conversion to Usable GeoJSON

The data that we’ll be using for this walkthrough is taken from the Washington State Department of Transportation (WSDOT) GeoData Catalogue. Database technology Download the shapefiles for “City Points” and “WSDOT Regions 24k”. Database queries definition City Points will give us the cities in Washington, while WSDOT Regions will provide us with regions designated by WSDOT. Data recovery mac free You can view the data before downloading by clicking View next to their download link.

We’ll also be using data from Washington State’s Office of Financial Management, which has census geographic files that provide us with geographic coordinates for county boundaries and cities. Data recovery phone Download the file for “Counties”.

The unzipped file contain all of the necessary files needed with a shapefile. Database normalization Since Elasticsearch does not use shapefiles, we’ll have to convert it to GeoJSON. Data recovery quote A simple way to convert shapefiles to GeoJSON is to use the GDAL’s ogr2ogr, which is a command line program that converts geographic data files from one format to another. Database key types We recommend downloading GDAL using homebrew brew install gdal. Database instance Once GDAL has been downloaded, you can use ogr2ogr on the command line.

To convert the “City Points” shapefile to GeoJSON, enter the following command into a terminal: ogr2ogr -f “GeoJSON” /destination/file.json -t_srs “EPSG:4326” /path/to/shapefile/shapefile.shp

Here, we write ogr2ogr to start the program, use the -f switch to indicate the file format “GeoJSON” along with the new file destination, use the -t_srs switch to indicate the encoding of our GeoJSON file, and then provide the path of the shape file. Data recovery raid 0 There are other options that you can define that are located in ogr2ogr’s documentation, but the ones used above will suit our needs. Data recovery tools Once we run the command, you’ll see the GeoJSON file you defined location.

{ “type”: “Feature”, “properties”: { “OBJECTID”: 1, “NAME”: “Sumas”, “CountySeat”: null, “GNIS”: 2412000, “LastUpdate”: “2009\/08\/31”, “MajorCity”: null, “CountyFIPS”: 73, “CityFIPS”: “5368330WA” }, “geometry”: { “type”: “Point”, “coordinates”: [ -122.264923557847354, 49.00004692672551 ] } },

{ “type”: “Feature”, “properties”: { “OBJECTID”: 281, “NAME”: “Tacoma”, “CountySeat”: “yes”, “GNIS”: 2412025, “LastUpdate”: “2006\/08\/31”, “MajorCity”: “yes”, “CountyFIPS”: 53, “CityFIPS”: “5370000WA” }, “geometry”: { “type”: “Point”, “coordinates”: [ -122.440097136359299, 47.253172271414293 ] } }

One of the first issues you may notice with GeoJSON is that the “coordinates” key is defined as lon/lat rather than lat/lon as we specified for geo_point above. Database programming languages The specifications for GeoJSON require coordinates to be in lon/lat, and since the coordinates are in an array, geo_point requires the same coordinate format lon/lat. Super 8 database Therefore, we won’t have to reformat coordinates if we are indexing using geo_point or geo_shape.

On the other hand, the GeoJSON file will have to be modified a little to use in Elasticsearch. 5 databases An easy way to do this is to delete the following, since we don’t need it: {

{ “type”: “Feature”, “properties”: { “OBJECTID”: 1, “NAME”: “Sumas”, “CountySeat”: null, “GNIS”: 2412000, “LastUpdate”: “2009\/08\/31”, “MajorCity”: null, “CountyFIPS”: 73, “CityFIPS”: “5368330WA” }, “geometry”: { “type”: “Point”, “coordinates”: [ -122.264923557847354, 49.00004692672551 ] } },

{ “type”: “Feature”, “properties”: { “OBJECTID”: 281, “NAME”: “Tacoma”, “CountySeat”: “yes”, “GNIS”: 2412025, “LastUpdate”: “2006\/08\/31”, “MajorCity”: “yes”, “CountyFIPS”: 53, “CityFIPS”: “5370000WA” }, “geometry”: { “type”: “Point”, “coordinates”: [ -122.440097136359299, 47.253172271414293 ] } }

You will want to format the file further depending on whether you want all of the GeoJSON contents within your Elasticsearch index. H data recovery registration code For this article, we will modify the file slightly by only using the “NAME”, “OBJECTID”, and “geometry” keys. Database link A code sample can be found in the repository here. Database google Mappings

Mappings of both geo_point and geo_shape is fairly straight-forward, but there are a couple differences that you should be aware of. Database update When defining a mapping in Elasticsearch using geo_point, you do not have to include the type of shape your longitude and latitude coordinates. Data recovery news What is necessary is that you have the correct lat/lon order, otherwise Elasticsearch will give you an error, except if your coordinates are in an array, then coordinates should be in lon/lat.

The geo_shape mapping is different in that it only accepts GeoJSON formatted data and that you must include “type” and “coordinates” within the GeoJSON “geometry” object to tell Elasticsearch which type of shape it’s indexing.

Before indexing geo-points or geo-shapes you must define the mapping beforehand since their fields are not dynamically mapped. Data recovery austin Mappings for both datatypes look like the following: {

By using geo_point or geo_shape, Elasticsearch will automatically find the coordinates, validate them according to the needed format, and index them.

We’ll be using a similar mapping for the downloaded data. Data recovery micro sd card To create the mapping, we’re using NodeJS and the elasticsearch client. Database relationship diagram If you’re not familiar with NodeJS and Elasticsearch see a good primer on setting up, building and deploying an application in our five-part series “Getting started with Elasticsearch and Node.js”.

What you’ll first need to do is create an index and call the index “wa_cities”. Data recovery services near me Compose’s Elasticsearch browser allows you to do this easily. Database worksheet From your deployment, click on the Browser button on the sidebar, which will take you to the browser page. Database constraints Then click the Create Index button on the browser and insert the name of the index then click Run.

After setting up the index, we can write the code for setting up the mapping and run it node mapping.js from the terminal. Database vault const elasticsearch = require(‘elasticsearch’),

Once the code has been run, you’ll see {“acknowledged”: true} returned indicating that the mapping was successfully created. Qmobile data recovery software You’ll want to create two more mappings using {“type”: “geo_shape”} for the “County” and again for the “City Point” data for when we look at Geo Queries.

Once your mappings have been created, you can insert the data using the _bulk API. Tally erp 9 data recovery software All of the code to create the mappings, modify the data, and insert it into an index has been provided in the example repository here. 7 databases in 7 weeks You can modify it accordingly. 3 database models Geo Queries

Elasticsearch uses the terms queries and filters. Database programmer salary Querying relies on “scoring”, or if and how well a document matches the query. Database developer salary Filtering, on the other hand, is “non-scoring” and determines if the document matches a query. Data recovery agent According to Elasticsearch, as of 2.x querying and filtering have become synonymous in that you can have queries that are both scoring and non-scoring. Data recovery usb flash drive There are various performance benefits and drawbacks to using scoring or non-scoring queries, but the rule-of-thumb is to use scoring queries when a relevance score is important, and non-scoring queries for everything else.

The queries that we will look at here will focus on some of the basic queries that you can do with Elasticsearch. Data recovery windows We’ll look at aggregations and Geohashes in an upcoming supplement.

Since we have some data in our indices, it’s time to start querying. Database definition We’ll look at some of the basic queries that we can use for geo_point and geo_shape.

The queries available for Elasticsearch 2.x are geo_shape, geo_bounding_box, geo_distance, geo_distance_range, geo_polygon, and geohash_cell. Data recovery definition As of Elasticsearch 5.x, geohash_cell has been deprecated. Database or database Distance with Geo Point

To get the distance between any two points, our data must be stored using the geo_point type. Data recovery software windows The documentation provides various data formats as examples. Cost of data recovery But, since our data is stored as an array that conforms to GeoJSON, our query would look something like the following: {

This query asks Elasticsearch to look for all matching points within a radius of 10 miles of the “location” you provide. Database first entity framework Here, our location is Seattle so we’re searching for all cities within a 10-mile radius. Data recovery iphone 5 If you want to use other distance units other than miles, see the documentation for acceptable units of measurement.

Instead of locating cities within a radius from a point of origin, you can also set the start and end distances, forming a donut shape instead of a circle. Raid 0 data recovery The query is similar to geo_distance except that define distances within “from” and “to”. 7 data recovery serial key {

Elasticsearch mentions that while this query is available, it’s expensive, and geo-shapes should be used instead. Database modeling Geo Bounding Box with Geo Point

All geo-shape queries require your data to be mapped using the geo_shape mapping. A database record is an entry that contains This is why we asked you to create two indices for cities using geo_point and geo_shape. Java database Using geo-shapes we can find documents that intersect with the query shape.

What’s nice about geo-shape queries is that you do not have to define all of the coordinates of the shape. Easeus data recovery 9 serial What does this mean? Elasticsearch will allow you to reference a pre-indexed shape in another index, or provide the entire coordinates of a shape within the query.

Querying using a user defined shape is similar to querying using geo_point. Iphone 5 data recovery software free If we want to get all the points within a specified radius we can use the following query: {

Here we only provide the starting point of the coordinates, which corresponds to Seattle, WA. Database partitioning Using the geo_shape query, we tell the query to look at the location field and provide the type of shape ( circle), how wide the radius of the circle is (10 miles), and provide the point of origin ( coordinates). Database gif This produces the same results as our geo_point query above. Database project ideas {

One of the useful features of geo_shape is being able to use pre-indexed shapes. Iphone 5 data recovery without backup When using pre-indexed shapes, we don’t have to insert a shape’s coordinates in the query, but we only have to refer to a shape’s index, type, and id. Bplan data recovery The query looks like the following: {

Here, we tell the query to look in the location field, but this time we use index_shape instead of shape so that Elasticsearch retrieves the shape from a specified index and id. Data recovery boston ma In this example, we use the wa_counties index, the name of our index containing the County data, our mapping type is county, and we want all the points within “id”: “King”, which we specified when inserting the data into the index. Ipad 2 data recovery We also mention the path of the county document coordinates, which is location like out cities data.

We can add more to this query by defining another field called relation, which allows us to add spatial relation operators: intersects, disjoint, within, or contains. Data recovery company A handy guide to these is located here. Data recovery network drive Te default value is intersects which in our case will give us all the cities within and on the border of our county. Data recovery yelp If we use a relation like disjoint, all the cities outside of King County will be counted. Database join table {

This will result in 243 cities to be returned. Google database In total, there are 281 cities that we indexed, so 281-243 = 38 (the cities in King County). Database quizlet {

So, we’ve given you an overview of the Geo querying capabilities of Elasticsearch and looked at the basics of how to set up your mappings and do some basic querying. Database synonym In the next installment, we’ll look at Elasticsearch aggregations and GeoData further, and show you how to build an application that will allow you to see your GeoData on a map.

If you have any feedback about this or any other Compose article, drop the Compose Articles team a line at articles@compose.com. Database management jobs We’re happy to hear from you.

banner