0

I received an error when I manually deleted an index from elasticsearch. This happen after manually deleted and I use User.search function in the route. This is the error:

Error: [search_phase_execution_exception] all shards failed

The reason why I manually deleted the index is because mongoosastic has a known issue where, whenever I delete documents from mongodb, elasticsearch still has the documents with it.

Here's the code

models/user.js

var mongoose = require('mongoose');
var mongoosastic = require('mongoosastic');
var Schema = mongoose.Schema;

var UserSchema = new Schema({
  private: false,
  twitter: String,
  tokens: Array,
  username: String,
  displayName: String,
  picture: String,

});

UserSchema.plugin(mongoosastic, {
  hosts: [
    'localhost:9200'
  ]});

  module.exports = mongoose.model('User', UserSchema);

router.js

User.createMapping(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);
    }
  });


  var stream = User.synchronize();
  var count = 0;

  stream.on('data', function(err, doc){
    count++;
  });

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

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

      /* The result for searching for User's */
  router.get('/search', function(req, res, next) {
    console.log(req.query.q);
    if (req.query.q) {
      User.search({
        query_string:
        { query: req.query.q }
      }, function(err, results) {
        if (err) return next(err);
        console.log(results);
        var data = results.hits.hits.map(function(hit) {
          return hit;
        });
        console.log(data);
        return res.render('main/search_results', { data: data });
      });
    }
  });
Louay Alakkad
  • 7,132
  • 2
  • 22
  • 45
Jack Moscovi
  • 2,195
  • 7
  • 30
  • 49
  • Something is wrong with your ES, shard error, apparently, has nothing to do with program . Have u tried curl -XGET 'http://localhost:9200/_cluster/health?pretty=true' and found the cluster status. – Somum Jan 24 '16 at 14:53
  • The health is yellow, is that a good thing? – Jack Moscovi Jan 24 '16 at 15:29
  • I fixed the problem by following this stackoverflow answer. http://stackoverflow.com/questions/30073759/searchphaseexecutionexceptionfailed-to-execute-phase-query-all-shards-failed – Jack Moscovi Jan 24 '16 at 15:29

0 Answers0