4

I'm trying to filter against a geo_point (geo_coordinates) which is nested in an object (Location), which is nested in an array in the queried object (MyObject).
The problem is that the mapping for the object MyObject does not see the geo_coordinates field as a geo_point:

// Index mapping for object MyObject
"myobjects": {
    "mappings": {
      "myobject": {
        "properties": {
            "locations": {
            "type": "nested",
            "include_in_parent": true,
            "properties": {
                "geo_coordinates": {
                "properties": {
                  "lat": {  "type": "double" },
                  "lon": { "type": "double" }
                }
              },
  }
[...]
// Index mapping for object Location 
"locations": {
    "mappings": {
      "location": {
        "properties": {
          "geo_coordinates": {
            "type": "geo_point"
          },
        }
      }
    }

The mongoose object MyObject looks like this:

var MyObjectSchema = new Schema(
[...]
    locations: {
      type: [{ type: Schema.Types.ObjectId, ref: 'Location', es_schema: LocationSchema.schema}],
      es_type: 'nested',
      es_include_in_parent: true
    },
[...]
)

The mongoose object Location looks like this:

var LocationSchema = new Schema(
[...]
  geo_coordinates: {
      type: {
        geo_point: {
          type: String,
          es_type: 'geo_point',
          es_lat_lon: true
        },

        lat: {type: Number, default: 0},
        lon: {type: Number, default: 0}
      },
      es_type: 'geo_point'
  }
[...]
);

My query looks like this:

// POST http://ES-endpoint/locations/_search
{
    "query": {
        "filtered" : {
            "query": {
                    "match_all" : {}
                },
                "filter" : {
                    "geo_distance" : {
                        "distance" : "20000km",
                        "locations.geo_coordinates" : {
                            "lat" : 40,
                            "lon" : -70
                        }}}}}}

I must miss something, but what ?
PS: When exploring the indexes with Kibana, both objects have the same data for geo_location:

"geo_coordinates": {
          "lat": x.xxxx,
          "lon": y.yyy
        }
Quentin Hayot
  • 7,786
  • 6
  • 45
  • 62

1 Answers1

2

Following is an example on applying filter query against geo_point with an nested object. I have used mongoosastic which is a Mongoose plugin to create indexing in elasticsearch.

Mongoose geoLocation data model

geoLocation: {
    type: {
        type: String,
        default: 'Point'
    },
    coordinates: [Number]
}

Create mapping on geoLocation field

Model.createMapping({
            "mappings": {
                "event": {
                    "properties": {
                        "geoLocation": {
                            "type": "nested",
                            "properties": {
                                "coordinates": {
                                    "type": "geo_point",
                                    "lat_lon": true
                                }
                            }
                        }
                    }
                }
            }
        },
        function(err, mapping) {
            if (!err) {
                console.log('mapping created!');
            }
        });

Query with geo filter

{
    "query": {
        "filtered": {
            "query": {
                "multi_match": {
                    "query": "Pop",
                    "fields": ["name","city"],
                    "type": "phrase_prefix"
                }
            },
            "filter": {
                "nested": {
                    "path": "geoLocation",
                    "query": {
                        "filtered": {
                            "filter": {
                                "geo_distance": {
                                    "distance": "5km",
                                    "geoLocation.coordinates": {
                                        "lat": 19.1074861,
                                        "lon": 72.9156988
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Deepika Chalpe
  • 404
  • 8
  • 19