# Best practice creating ES API using NodeJS

**URL:** <https://discuss.elastic.co/t/best-practice-creating-es-api-using-nodejs/20577>\
**Category:** Elasticsearch\
**Created:** [November 5, 2014, 12:13pm UTC](https://discuss.elastic.co/t/best-practice-creating-es-api-using-nodejs/20577 "2014-11-05T12:13:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rayman\_2](https://avatars.discourse-cdn.com/v4/letter/r/3bc359/32.png) [@rayman\_2](https://discuss.elastic.co/u/rayman_2)\
**Post date:** [November 5, 2014, 12:13pm UTC](https://discuss.elastic.co/t/best-practice-creating-es-api-using-nodejs/20577/1 "2014-11-05T12:13:30Z")

</div>

Hi,  
I developed simple nodejs project using ES as our search engine and  
ElasticSearchClient for node

Node is exposing api to the user(using expressJS for that)

I have few search categories (search by username, search by firstname,  
search by lastname, etc...)

This is the function (using promises) which returning search by username:

- please ignore the inline params. this is just a POC

function performSearch(termToSearch) {

```
var deferred = Q.defer();
console.log("Request handler 'search' was called.");
var qryObj =
{
    "query": {
        "match": {
            "username": termToSearch
        }
    }
};
elasticSearchClient.search('my_index", "users", qryObj).
    on('data', function (data) {
        console.log(data)
        deferred.resolve(JSON.parse(data));
    })
    .on('error', function (err) {
        console.log(err);
        return deferred.resolve(err);
    })
    .exec();
return deferred.promise;

```

}

My question is what if I want to implement the next fields.. I need to  
duplicate this code with different queries? any best practical example to  
upgrade that code.

thanks.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/c79fe14d-3d9d-4558-a6a8-711a90054d57%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/c79fe14d-3d9d-4558-a6a8-711a90054d57%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Ravi\_Shanker\_Reddy](https://avatars.discourse-cdn.com/v4/letter/r/a5b964/32.png) [@Ravi\_Shanker\_Reddy](https://discuss.elastic.co/u/Ravi_Shanker_Reddy)\
**Post date:** [July 21, 2016, 5:45am UTC](https://discuss.elastic.co/t/best-practice-creating-es-api-using-nodejs/20577/2 "2016-07-21T05:45:56Z")

</div>

Store your query into a variable (json) and pass to the same function

---

<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:33pm UTC](https://discuss.elastic.co/t/best-practice-creating-es-api-using-nodejs/20577/3 "2017-07-05T22:33:50Z")

</div>


