geoWithin $search on Atlas "geo" indexed field returning nothing

Hello,
I tried to create a first Atlas Search index with a “geo” type of field according to this syntax:

{
  "mappings": {
    "dynamic": true,
    "fields": {
      "LOKATION": {
        "type": "geo"
      }  }  }  }

The index, incidentally, is named “geo4”.
The data in the underlying database contains about 1000 documents, each of which has the following structure (most fields omitted – content mainly cut+pasted from Compass JSON view of a document:

{
    "_id": {
        "$oid": "..."
    },
    "ID": 1,
    "COLL": 0,
    "GROUP": 0,
    "INDUSTRY": "ABB",
    <... other fields ...>,
    "LOKATION": {
        "type": "Point",
        "coordinates": [{
            "$numberDecimal": "12.071365000000000"
        }, {
            "$numberDecimal": "45.781243000000000"
        }]
    },
    <... other fields ...>
}

I’ve tried many different queries (esp with “circle”), all returning 0 – the following is the last one, tried both from Compass and from the mongodb shell – the parameters may seem quite extreme, but I was trying to make sure I’d hit at least one document, as the one partially reported above:

[
    {
        '$search': {
            'index': 'geo4', 
            'geoWithin': {
                'box': {
                    'bottomLeft': {
                        'type': 'Point', 
                        'coordinates': [
                            0, 0
                        ]
                    }, 
                    'topRight': {
                        'type': 'Point', 
                        'coordinates': [
                            90, 90
                        ]
                    }
                }, 
                'path': 'LOKATION'
            }
        }
    }
]

Note that from Atlas, the search index is shown to have 100% of documents indexed.
Note also that I have previously created a 2dsphere index on a previous version of the LOKATION field where the field was an Array of lon, lat values, as required – it worked just fine on the same data.

Anyone has a hint of what’s going wrong?

Thanks
stefano-

Well, for anyone who might stumble on this in the future:

I’ve fixed the issue, and it had nothing to do with the index setup or the query syntax and parameters.
The problem was with the type the lon and lat values in the LOKATION.coordinates field were assigned upon creation. The data in fact comes from a mongoimport operation, and the lon/lat values were specified there to be of type decimal().
It turns out that this way, as shown above, the resulting lon and lat values are represented as:
{ "$numberDecimal": "<...some value...>" }
instead of just decimal numbers.
While this is apparently not an issue with 2dsphere indexes (which I had set up and successfully used on the same data, imported as decimal() as well), it doesn’t work with “geo” index fields in Atlas Search indexes.
For me, the solution was to mongoimport that data with a type of “auto()”.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.