Note: Upon further research, I have reduced this question to a more specific inquiry.
Objective is to fetch a document _id by matching a field in a Javascript / Node.js application that leverages the Get API and CURL. Let's assume that no more than one document _id will match the field ("email" in this example).
Step 1 - Query for the document based on email.
I'm familiar with how to run the query via Sense.
GET /users/user/_search
{
"query": {
"match": {
"email": "foo@bar.com"
}
}
}
I believe this would look like something like this if using the API and CURL in a custom JavaScript / Node.js application based on an example from Elastic | Search, this seems very lightweight:
curl -XGET 'http://localhost:9200/users/user/_search?q=email:'foo@bar.com''
Or is there a signficant advantage to using Elasticsearch.js like this (Note: I've omitted the 'require' statements and client setup from the discussion here but that is well covered in the Elasticsearch.js link)?:
client.search({
index: 'users',
type: 'user',
body: {
query: {
match: {
email: userEmail
}
}
}
}).then(function (resp) {
var hits = resp.hits.hits;
}, function (err) {
console.trace(err.message);
});
Step 2 - Return the document _id value as a string in the JavaScript / Node.js application for use
This is where I have seen few examples and could use some guidance. If using the elasticsearch.js example, perhaps the next step is to do a regex on hits to capture the value that follows '_id:'? Or is there a member of 'resp.hits.hits' that can return the _id(s)?