# API CURL Get Document \_id by Query Match

**URL:** <https://discuss.elastic.co/t/api-curl-get-document--id-by-query-match/50504>\
**Category:** Elasticsearch\
**Created:** [May 19, 2016, 10:47pm UTC](https://discuss.elastic.co/t/api-curl-get-document--id-by-query-match/50504 "2016-05-19T22:47:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ngmgithub](https://avatars.discourse-cdn.com/v4/letter/n/bcef8e/32.png) [@ngmgithub](https://discuss.elastic.co/u/ngmgithub)\
**Post date:** [May 19, 2016, 10:47pm UTC](https://discuss.elastic.co/t/api-curl-get-document--id-by-query-match/50504/1 "2016-05-19T22:47:05Z")

</div>

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](https://www.elastic.co/guide/en/elasticsearch/reference/1.4/search-search.html), this seems very lightweight:

> curl -XGET '[http://localhost:9200/users/user/\_search?q=email:'foo@bar.com'](http://localhost:9200/users/user/_search?q=email:'foo@bar.com')'

Or is there a signficant advantage to using [Elasticsearch.js](https://www.npmjs.com/package/elasticsearch) 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)?

---

<div class="post-metadata">

**Author:** ![ngmgithub](https://avatars.discourse-cdn.com/v4/letter/n/bcef8e/32.png) [@ngmgithub](https://discuss.elastic.co/u/ngmgithub)\
**Post date:** [May 20, 2016, 9:14pm UTC](https://discuss.elastic.co/t/api-curl-get-document--id-by-query-match/50504/2 "2016-05-20T21:14:02Z")

</div>

Is `_id` a member of `resp.hits.hits` (aka `hits`). Can I access the `_id` as `hits._id`?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:50pm UTC](https://discuss.elastic.co/t/api-curl-get-document--id-by-query-match/50504/3 "2017-07-05T22:50:02Z")

</div>


