Geo search
How to do geo searching in Optimizely Graph.
Geo search enables the use of geo-coordinated locations to match and rank content effectively. You can deliver personalized results based on user location by geo-tagging content with a geographic coordinate system, such as latitude (lat) and longitude (lon). This feature not only lets users discover nearby businesses and services but also provides access to location-based content, like travel articles, celebrity concert venues, sports events, and noteworthy occurrences.
Leveraging location data enhances personalization, making content more relevant to users, which can lead to improved conversion rates and increased engagement. Optimizely Graph supports storing, querying, and ranking such content by fully utilizing the geographic coordinate system.
📘
NoteGeo search is not currently supported for CMS content types. To use this feature, you must connect to an external data source, which may involve additional cost. Contact your Customer Success Manager (CSM) for guidance and setup options.
GeoPoint type
GeoPoint typeFields can be configured with a custom type. To enable geo search, you need to use a custom type calledGeoPoint that consists of two properties:lat andlon, which are of type float.
"contentTypes":{ "test": { "contentType": [], "properties": { "GeoPointField": { "type": "GeoPoint" } } }}The following is an example of this type implemented as a field, where valid coordinates are -90 to 90 forlat (latitude) and -180 to 180 forlon (longitude):
"GeoPointField": { "lat": 59.334591, "lon": 18.06324}//With useTypedFieldNames = true"GeoPointField$$GeoPoint": { "lat": 59.334591, "lon": 18.06324}//With useTypedFieldNames = true and searchable"GeoPointField$$GeoPoint___searchable": { "lat": 59.334591, "lon": 18.06324}How to search
📘
Geo search with full-text searchThe
GeoPointtype is not supported forfull-text search with_fulltext. In case a field with this type is set assearchable, Optimizely Graph will not add this to_fulltext.
Fields configured asGeoPoint have the following special operators to geo search:
distance– Filters your results by distance given:- an
origin– Mandatory - a
radius - Default –
1000 - a
unit- Default –
M/ meters
- Default –
- The
unitcan be the following metrics:M– MetersKM– KilometersCM– CentimetersMM– MillimetersMI– MilesIN– InchesYD– Yards
- an
withIn– Retrieves content that is within a polygon shape described by a set of geo coordinates. It requires at least four coordinates, and the polygon needs to be closed, so the starting and end coordinates need to be the same. When you want to filter on multiple polygons, use the_orBoolean operator.exist– Checks if the field exists in your content. But the above operators will automatically ignore or treat it as undefined in case it does not exist.
Examples
The following example retrieves biography pages of people within 1,200 km from London:
{ BiographyPage( where: { GeoPointField: { distance: { radius: 1200, origin: { lat: 51.509865, lon: -0.118092 }, unit: KM } } } ) { total items { Name GeoPointField { lat lon } } }}To search within a polygon with a set of coordinates, we have the following example with Stockholm usingwithIn, but note that the polygon shape is not complete in this example because of space considerations:
{ BiographyPage( where: { GeoPointField: { withIn: [ { lat: 59.381319, lon: 17.839936 } { lat: 59.381092, lon: 17.830846 } { lat: 59.381125, lon: 17.83038 } { lat: 59.381354, lon: 17.828188 } { lat: 59.381639, lon: 17.825344 } { lat: 59.385607, lon: 17.846594 } { lat: 59.384736, lon: 17.845159 } { lat: 59.383968, lon: 17.843804 } { lat: 59.383115, lon: 17.842609 } { lat: 59.381319, lon: 17.839936 } ] } } ) { total items { Name GeoPointField { lat lon } } }}How to sort
You can rank by closest (default) locations near anorigin usingorderBy, which is by defaultorder: ASC, so theorder does not need to be set when you want to sort nearest locations.
Optionally andnot commonly used, you can specify by farthest distance by setttingorder: DESC. In case you havenull values or missing fields, these will be treated asinfinity and sorted to the top.
Example
{ BiographyPage( orderBy: { GeoPointField: { origin: { lat: 52.377956, lon: 4.897070 } } } ) { total items { Name GeoPointField { lat lon } } }}How to facet
Groups of content items can be created by defining them withranges (at least one object withfrom andto) with aunit (default:M / meters) from anorigin (mandatory). The unit supports the same metrics as defined for thedistance operator. These groups can be then used to filter by thedistance operator.
📘
NoteIf
distanceis not specified, no facets are returned.
Example
{ BiographyPage (limit: 0) { facets { GeoPointField( distance: { ranges: [{ to: 500 }, { from: 500, to: 1000 }, { from: 1000 }] unit: MI origin: { lat: 59.334591, lon: 18.06324 } } ) { name count } } }}Updated 9 days ago
