gurunglaxman0
gurunglaxman0

Reputation: 68

GeoCode filter in mongoose elasticsearch

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]

Upvotes: 1

Views: 615

Answers (1)

Paradise228
Paradise228

Reputation: 717

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.

Upvotes: 1

Related Questions