过滤嵌套对象数组中的geo_point

时间:2016-07-18 16:37:13

标签: node.js elasticsearch mongoose mongoosastic

我试图过滤嵌套在对象(位置)中的geo_point(geo_coordinates),该对象嵌套在查询对象(MyObject)的数组中。
问题是对象MyObject的映射没有将geo_coordinates字段视为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"
          },
        }
      }
    }

mongoose对象MyObject如下所示:

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

猫鼬对象位置如下:

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'
  }
[...]
);

我的查询如下:

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

我必须错过一些东西,但是什么? PS:使用Kibana探索索引时,两个对象都具有相同的geo_location数据:

"geo_coordinates": {
          "lat": x.xxxx,
          "lon": y.yyy
        }

1 个答案:

答案 0 :(得分:2)

以下是使用嵌套对象对geo_point应用过滤器查询的示例。我使用了mongoosastic这是一个Mongoose插件,可以在elasticsearch中创建索引。

猫鼬地理位置数据模型

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

在地理位置字段

上创建地图
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": {
        "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
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}