Tuesday, 6 June 2017

Mongoosastic Search, Cannt Find Indexed Values

I am trying to index and search in a mongodb collection that I've created using mongoose.But when run the code it can't find anything even if it's indexed. I wrote to code according to mongoosastic documentation.Could you help me with showing my mistake ? Here's the main code;

var mongoose = require("mongoose"),
  mongoosastic =require("mongoosastic"),
  Schema = mongoose.Schema;

var BunchSchema = new Schema({
  name : String,
  address : String
});


BunchSchema.plugin(mongoosastic);

var Bunch = mongoose.model("try",BunchSchema);
var stream = Bunch.synchronize();
var  count = 0;

stream.on('data',function(err,doc){
  if(err){
    console.log("err data");
    console.log(err);
  }
  else{
    console.log("data +");
    console.log(doc);
  }
  count++;
});


stream.on('close',function () {
  console.log('indexed'+count+'documents!');
});

stream.on('error',function (err) {
  console.log("error on synchronize");
  console.log(err);
});

console.log('working');

Bunch.createMapping(function (err,mapping) {
  if (err) {
    console.log('error in mapping');
    console.log(err);
  }
  else{
    console.log("mapping successfull");
    console.log(mapping);
  }
});
BunchSchema.search({
  query_string:{
    query:"John"
  }
},function (err,result) {
  if (err){
    console.log("err in search");
    console.log(err);
  }
  else {
    console.log("search success");
    console.log(result);
  }
});

Also, this is my database.js script;

var MongoClient = require("mongodb").MongoClient;
var url = "mongodb://localhost:27017/";

MongoClient.connect(url,function(err,result){
  if(err){
    console.log("error happened");
  }
  else{
    console.log("it worked");
    console.log(result)
  }
});

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  db.createCollection("try", function(err, res) {
    if (err) throw err;
    console.log("Table created!");
    db.close();
  });
});

MongoClient.connect(url ,function(err,db){
  if (err) throw err;
  var myobj = [
    { name: 'John', address: 'Highway 71'},
    { name: 'Peter', address: 'Lowstreet 4'},
    { name: 'Amy', address: 'Apple st 652'},
    { name: 'Hannah', address: 'Mountain 21'},
    { name: 'Michael', address: 'Valley 345'},
    { name: 'Sandy', address: 'Ocean blvd 2'},
    { name: 'Betty', address: 'Green Grass 1'},
    { name: 'Richard', address: 'Sky st 331'},
    { name: 'Susan', address: 'One way 98'},
    { name: 'Vicky', address: 'Yellow Garden 2'},
    { name: 'Ben', address: 'Park Lane 38'},
    { name: 'William', address: 'Central st 954'},
    { name: 'Chuck', address: 'Main Road 989'},
    { name: 'Viola', address: 'Sideway 1633'}
  ];
  db.collection("try").insert(myobj,function(err,res){
    if (err) throw err;
    console.log("Number of recoreds inserted "+res.insertedCount);
    db.close();
  });
});


via E. Dem

No comments:

Post a Comment