mongoose-elasticsearch-xp
Version:
A mongoose plugin that indexes models into elastic search (an alternative to mongoosastic)
82 lines (75 loc) • 1.34 kB
JavaScript
var utils = require('./utils');
var mongoose = require('mongoose');
var plugin = require('../');
mongoose.connect('mongodb://localhost/toven');
var tovenAds = new mongoose.Schema({
city: String,
country: String,
cntry: {
type: String,
es_indexed: true
},
currency: String,
enabled: Boolean,
images: [String],
pos: {
type: [Number],
index: '2dsphere',
es_indexed: true,
es_type: 'geo_point'
},
price: {
type: Number,
es_indexed: true
},
status: {
type: String
},
text: {
type: String,
es_indexed: true
},
title: {
type: String,
es_indexed: true
},
userId: String,
zip: String,
video: String,
site: String,
url: String,
startDate: {
type: Date,
"default": Date.now,
es_indexed: true
},
created: {
type: Date,
"default": Date.now
},
age: String
});
tovenAds.plugin(plugin);
var Ad = mongoose.model('ad', tovenAds);
var count = 0;
Ad
.on('es-bulk-data', function(doc) {
if (++count % 100 === 0) {
console.log(count);
}
})
.on('es-bulk-error', function(err) {
console.log(err);
});
Ad
.esCreateMapping()
.then(function () {
return Ad.esSynchronize();
})
.then(function () {
console.log('done');
})
.catch(function (err) {
console.log(err);
});
console.log('ok')