使用geopoint的Node.js弹性搜索过滤器

时间:2017-06-14 15:03:03

标签: node.js mongodb elasticsearch mongoose

我尝试使用地理距离过滤数据。我的mongoose模式是:

'use strict';
    var mongoose = require('mongoose'),
      Schema = mongoose.Schema;
    var beautifyUnique = require('mongoose-beautiful-unique-validation');
    var mongoosastic = require("mongoosastic");

var schema = new Schema({
  vendor_id: { type: Schema.Types.ObjectId, ref: 'Vendor' },
  category: { type: Schema.Types.ObjectId, ref: 'Category' },
  name: { type: String, required: 'Business Name is required' }, // es_indexed: true },
  contact: {
    contact_name: { type: String, required: 'Name is required' },
    email: { type: String, required: 'Email is required' },
    s_email: String,
    mobile: { type: String, required: 'Mobile No. is required' },
    s_mobile: String,
  },
  images: [{
    title: String,
    desc: String,
    key_large: String,
    url_large: String,
    key_thumb: String,
    url_thumb: String
  }],
  cover_image: String,
  description: String,
  seat_capacity: Number,
  float_capacity: Number,
  address: {
    address1: String,
    city: String,
    state: String,
    country: String,
    pin: Number
  },
  geometry: {
    type: [Number]
      // index: "2dsphere",
  },
  events: [{ type: Schema.Types.ObjectId, ref: 'Event' }],
  highlights: [{ type: String }],
  advanced_percentage: Number,
  taxes: {
    food: Number,
    other: Number
  },
  cancellable: Boolean,
  time_slots: [{
    text: String,
    slot: String
  }],
  min_Order_quantity: String,
  outside_catering: Boolean,
  outside_decorators: Boolean,
  alcohol_allowed: Boolean,
  terms: String,
  aminities: [{ type: Schema.Types.ObjectId, ref: 'Aminity' }],
  sub_categories: [{ type: String }],
  status: { type: String, default: 'draft' },
  verifiedBy: { type: Schema.Types.ObjectId, ref: 'SuperAdmin' },
  verifyTime: { type: Date },
  isAvailable: Boolean,
  isBlocked: { type: Boolean, default: false },
  blockedBy: { type: Schema.Types.ObjectId, ref: 'SuperAdmin' },
  blockReason: String,
  created_at: { type: Date, default: Date.now() },
  updated_at: { type: Date },
  version: { type: Number }
});
schema.plugin(mongoosastic, {
  hosts: [
    'localhost:9200'
  ]
});
schema.plugin(beautifyUnique);
// enables beautifying 
var Business = module.exports = mongoose.model('BusinessDetail', schema);

Business.createMapping({
  "mappings": {
    "businessdetail": {
      "properties": {
        "geometry": {
          "type": "geo_point"
        }
      }
    }
  }
}, function(err, mapping) {
  if (err) {
    console.log('error creating mapping (you can safely ignore this)');
    console.log(err);
  } else {
    console.log('mapping created!');
    console.log(mapping);
  }
});

我的搜索代码是

BusinessDetail.search({
            "bool": {
              "must": {
                "match_all": {}
              },
              "filter": {
                "geo_distance": {
                  "distance": "200km",
                  "geometry": [lon, lat]
                }
              }
            }
          },
          function(err, business) {
            console.log('err:', err);
            console.log('data:', business);
            res.send({
              success: true,
              data: business
            });
          });

我尝试了很多过滤组合。但是它给出了错误

  

错误:{错误:[query_shard_exception]字段[geometry]不是   geo_point字段,{index_uuid =" 6e4ptkf0TFWwp1IN3tIRbA" &安培;   指数=" businessdetails" }       在响应(C:\ eventbucket \ eb-server \ node_modules \ mongoosastic \ node_modules \   elasticsearch的\ src \ LIB \ transport.js:289:15)       at checkRespForFailure(C:\ eventbucket \ eb-server \ node_modules \ mongoosastic \ n   ode_modules \ elasticsearch \ SRC \ lib中\ transport.js:248:7)       在HttpConnector。 (C:\ eventbucket \ EB-服务器\ node_modules \ mongoosa   STIC \ node_modules \ elasticsearch \ SRC \ lib中\连接器\ http.js:164:7)       在IncomingMessage.wrapper(C:\ eventbucket \ eb-server \ node_modules \ lodash \ lod ash.js:4968:19)       在emitNone(events.js:91:20)       在IncomingMessage.emit(events.js:185:7)       at endReadableNT(_stream_readable.js:974:12)       at _combinedTickCallback(internal / process / next_tick.js:74:11)       at process._tickDomainCallback(internal / process / next_tick.js:122:9)状态:400,displayName:   ' BadRequest',消息:' [query_shard_exception]字段[geometry]是   不是geo_point字段,而是{index_uuid =" 6e4ptkf0TFWwp1IN3tIRbA" &安培;   指数=" businessdetails" }',路径:   ' / businessdetails / businessdetail / _search',查询:{},正文:   ' {"查询" {"布尔" {"必须" {" MATCH_ALL":{}},&# 34;过滤器" {" geo_distance" {"二   姿态":"200公里""几何":[88.36389500000001,22.572646]}}}}}&#39 ;,
  statusCode:400,响应:   ' {"错误" {" ROOT_CAUSE":[{"类型":" query_shard_exception""理由&# 34;:"˚F   ield [geometry]不是geo_point   字段"" index_uuid":" 6e4ptkf0TFWwp1IN3tIRbA&#34 ;,   "指数":" businessdetails"}],"类型":" search_phase_execution_exception""理由":&#34 ;   所有碎片   失败""相":"查询""分组":真," failed_shards":[{"碎片& #34;:0,"我   ndex":" businessdetails""节点":" IQY-hWNlTnaiUL3cUlAWVQ""理由" {"类型&# 34;:" query_   shard_exception"," reason":" field [geometry]不是geo_point   字段"" index_uui   d":" 6e4ptkf0TFWwp1IN3tIRbA""指数":" businessdetails"}}]},"状态" 400}&#39 ;,   toString:[Function],toJSON:[Function]} data:undefined

如何解决错误?

0 个答案:

没有答案