node.js - 在嵌套对象数组中过滤 geo_point

标签 node.js elasticsearch mongoose mongoosastic

我正在尝试针对嵌套在对象 (Location) 中的 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
    },
[...]
)

Mongoose 对象 Location 如下所示:

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
        }

最佳答案

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

Mongoose geoLocation 数据模型

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

在 geoLocation 字段上创建映射

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
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

关于node.js - 在嵌套对象数组中过滤 geo_point,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38441906/

相关文章:

search - 如何获得 Elasticsearch 以在尖括号内返回结果?

node.js - Mongoose - 灵活的领域

javascript - Node 中的请求流在被消费之前会发生什么?

node.js - 如何在 Node.js 中无限读取行

javascript - 使用 jQuery 在服务器端操作 Wordpress 数据库

elasticsearch - 更新elasticsearch模板而不覆盖它

heroku - 连接到 Elasticsearch Heroku 数据库

node.js - Nodejs Mongoose - 模型未定义

javascript - TypeError : this. isModified 不是一个函数 - Mongoose 模型 FindOneAndUpdate

node.js - 查找安装间接依赖项的原因