GeoCode filter in mongoose elasticsearch

旧时模样 提交于 2020-01-05 08:32:30

问题


I am trying to do geo point filter in mongodb in meanjs. i have used mongoosastic modules but i am not able to perform geo point filter. here below are the mongoose schema and controller code for filter.

Mongoose schema

    'use strict';
    /**
     * Module dependencies.
     */
    var mongoose = require('mongoose'),
            Schema = mongoose.Schema,
            mongoosastic = require('mongoosastic');

    var BusinessSchema = new Schema({
        name: {type: String, unique: 'Name already exists', trim: true, required: 'Name is required.', es_indexed: true},
        searchTags: {type: [String], es_indexed: true},
        alias: {type: Array, es_indexed: true},
        // geoLocation: { type: [Number], /*/ [<longitude>, <latitude>]*/ index: '2d', /*/ create the geospatial index,*/ required: 'GeoLocation is required.', es_indexed:true,es_type:'geo_point'},
        geo_cords: {
            type: Array
        },
        address: {
            address1: {type: String, required: 'Address is required', trim: true},
            address2: String,
            city: {type: String, required: 'City is required', trim: true},
            // state: {type: String, required: 'State is required', trim: true},
            country: {type: String, required: 'Country is required', trim: true},
            postalCode: {type: String, required: 'Postal code is required', trim: true},
            neighbourhood: String
        },
        isActive: {
            type: Boolean,
            default: true,
            es_indexed: true
        },
        dateUpdated: {
            type: Date
            , es_indexed: true
        },
        dateCreated: {
            type: Date,
            default: Date.now
            , es_indexed: true

        }
    });

controller code for filter and query

   var mongoose = require('mongoose'),
        Business = mongoose.model('Businesses');

    var query = {
        "query_string": {
            "multi_match": {
                "query": categoryIds.join(' OR '),
                "fields": ["categoryIds", "relatedCategoryIds"]
            }
        },
        "filter": {
            "bool": {
                "should": [
                    {"term": {"address.postalCode": "110016"}},
                    {"geo_distance": {
                            "distance": "50km",
                            "geo_cords": [-122.3050, 37.9174]
                        }
                    }
                ],
            }
        }
    }

    Business.search(query, function (err, results) {
        // sendResponse(req, res, err, results)
        if (!err) {
            res.json(results);
        } else {
            res.status(400).send({message: 'Business Not Found'})
        }
    });

while doing this i am getting a long error saying

QueryParsingException[[businessess] failed to find geo_point field [geo_cords]

回答1:


According to the documentation of mongoosastic

Geo mapping

Prior to index any geo mapped data (or calling the synchronize), the mapping must be manualy created with the createMapping (see above).

First, in your schema, define 'geo_cords' this way:

geo_cords: : {
      geo_point: {
        type: String,
        es_type: 'geo_point',
        es_lat_lon: true
      },
      lat: { type: Number },
      lon: { type: Number }
    }

Add an es_type: 'object' to each Array or embbeded type

alias: {type: Array, es_indexed: true, es_type: 'object'}

Then call .createMapping() on the model just after you've created it.



来源:https://stackoverflow.com/questions/30500454/geocode-filter-in-mongoose-elasticsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!