Search code examples
mapreducemarklogicmarklogic-optic-api

MarkLogic Optic javaScript Geospatial Difference


I want to reduce the selected items by their distance from a point using MarkLogic Optic.

I have a table with data and a lat long

const geoData = op.fromView("namespace","coordinate");
geoData.where(op.le(distance(-28.13,153.4,geoData.col(lat),geoData(long)),100))

The distance function I have already written and utilises geo.distance(cts.point(lat,long),cts.point(lat,long)) but the geoData.col("lat") passes an object that describes the full names space of the col not the value.

op.schemaCol('namespace', 'coordinate', 'long')

I suspect I need to do a map/reduce function but MarkLogic documentation gives the normal simplistic examples that are next to useless.

I would appreciate at some help.

FURTHER INFORMATION I have mostly solved most of this problem except that some column have null values. The data is sparse and not all rows have a long lat.

So when the cts.points runs in the where statement and two null values are passed it raises an exception.

How do I coalesce or prevent execution of cts.points when data columns are null? I dont want to reduce the data set as the null value records still need to be returned they will just have a null distance.


Solution

  • Where possible, it's best to do filtering by passing a constraining cts.query() to where().

    A constraining query matches the indexed documents and filters the set of rows to the rows that TDE projected from those documents before retrieving the filtered rows from the indexes.

    If the lat and long columns are each distinct JSON properties or XML elements in the indexed documents, it may be possible to express the distance constraint using techniques similar to those summarized here:

    http://docs.marklogic.com/guide/search-dev/geospatial#id_42017

    In general, it's best to use map/reduce SJS functions for postprocessing on the filtered result set because the rows have to be retrieved to the enode to process in SJS.

    Hoping that helps,