0

I'm using nodejs/express for a webapp and storing data in elastic search. I would like to put something like the user profile in elastic search, and when I pull it out call methods on it. I'm used to Java/C# so I would expect to do something like

var user = new User(elasticSearchResult)
user.getUserRoles()

The constructor would parse the data and the class would contain all the methods I need.

1
  • What's the question? Commented Sep 18, 2015 at 16:50

2 Answers 2

1

you need this https://www.npmjs.com/package/elasticsearch Then you can create function for that User class as

User.getUserRoles = function(){
client.search({
   index: ....,
   type: ....
   body: {
       query: {
        ....
       }
   }
}).then(function (resp) {
  //Handle records return here
}, function (err) {
  //handle error here
});
Sign up to request clarification or add additional context in comments.

Comments

0

I ended up using this for my user class

var proto = User.prototype;
function User(){}

proto.setRoles = function(roles) {
    this.roles = roles;
};

proto.fromJson = function(json) {
    if(json._source) {  // From ElasticSearch query
        this.roles = json._source.roles;
    }
};
exports.User = User;

And in my calling class

var user = new UserClass.User();
db.searchForUser(user.email, function(err, success) {
    if (success.hits.total == 1) {
        var userFromDb = success.hits.hits[0];
        user.fromJson(userFromDb);
        return done(null, user);
    }
});

and if anyone is wondering, here is my searchForUser method

exports.searchForUser = function (email, callback) {
    var qryObj = {
        "query": {
            "bool": {
                "must": [
                    {
                        "match": {
                            "email": email
                        }
                    }
                ]
            }
        }
    };
    client.search ({index:'myindex', type:'user', body: qryObj}, callback);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.