# Kibana4 -\> Elasticsearch API using Javascript

**URL:** <https://discuss.elastic.co/t/kibana4-elasticsearch-api-using-javascript/26028>\
**Category:** Kibana\
**Created:** [July 21, 2015, 10:14pm UTC](https://discuss.elastic.co/t/kibana4-elasticsearch-api-using-javascript/26028 "2015-07-21T22:14:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SNAFUdowser](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@SNAFUdowser](https://discuss.elastic.co/u/SNAFUdowser)\
**Post date:** [July 21, 2015, 10:14pm UTC](https://discuss.elastic.co/t/kibana4-elasticsearch-api-using-javascript/26028/1 "2015-07-21T22:14:57Z")

</div>

### What I want:

In Kibana, make a custom query in Javascript to produce a JSON response containing information like this:

```
  "hits": {
    "total": 8888229,
    "max_score": null,
    "hits": [
      {
        "_index": "logstash-2015.07.20",
        "_type": "logs",
        "_id": "AU6s1kvTAluRVDz068io",
        "_score": null,
        "_source": {
          "message": "",
          "@version": "1",
          "@timestamp": "2015-07-20T19:00:01.595Z",
          "type": "logs",
          "file": "/home/karl/sys_info.log",
          "host": "dood",
          "offset": "0",
          "tags": [
            "_grokparsefailure"
          ]
        }, //...etc...//

```

### What I've tried:

1. Sending POST requests using Javascript to /elasticsearch/\_msearch?timeout=0&ignore\_unavailable=true&preference=1437514278665 that look very much like the POST requests that are sent when you search in Kibana's query bar.
2. Attempt to use the **_add()_** function which in my tacked on Javascript file I seem to have access to.
3. Attempt to use **_require('components/courier/data\_source/search\_source')_** to get the SearchSource object. In doing this I wasn't able to figure out how to use Private and the other parameters to \*\*\*SearchSourceFactory()\*\*\*.

**Relevant links which I've viewed:** [https://github.com/elastic/kibana/issues/3580](https://github.com/elastic/kibana/issues/3580)

###What should I do?

---

<div class="post-metadata">

**Author:** ![tbragin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbragin/32/45166_2.png) [@tbragin](https://discuss.elastic.co/u/tbragin)\
**Post date:** [July 22, 2015, 5:45pm UTC](https://discuss.elastic.co/t/kibana4-elasticsearch-api-using-javascript/26028/2 "2015-07-22T17:45:26Z")

</div>

Ultimately, what are you trying to achieve with this? What's your use case?

---

<div class="post-metadata">

**Author:** ![SNAFUdowser](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@SNAFUdowser](https://discuss.elastic.co/u/SNAFUdowser)\
**Post date:** [July 22, 2015, 8:55pm UTC](https://discuss.elastic.co/t/kibana4-elasticsearch-api-using-javascript/26028/3 "2015-07-22T20:55:09Z")

</div>

I'm attempting to create a button which will fire off multiple queries to Elasticsearch, each query using the previous query result in the next.

**More Specific Info:** I am working collecting logs from multiple machines. Each machine handles many orders which travel from one machine to the next. Each machine also has a pointer to the previous machine in the chain and the next machine in this chain. I want to trace that order. So to do this I want to get one order\_id from one of the machines, and then search the parent machine and child machine for that order. If I find the order and it too has a parent, repeat the process.

**Another option on how to implement the queries:** I realize that sending multiple requests on the client side and doing calculations there isn't the best way to go about it as round trip time goes way up. Instead I realize I should send one request, which then on the backend fires of a bunch of queries. I know Kibana4 uses Nodejs, is extending the Nodejs backend what I should be looking into doing?

---

<div class="post-metadata">

**Author:** ![tbragin](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tbragin/32/45166_2.png) [@tbragin](https://discuss.elastic.co/u/tbragin)\
**Post date:** [July 23, 2015, 3:40pm UTC](https://discuss.elastic.co/t/kibana4-elasticsearch-api-using-javascript/26028/4 "2015-07-23T15:40:28Z")

</div>

For this type of use case, have you considered using entity-centric indexing?

Mark Harwood, one of our Engineers, has given several talks on the subject. Here are some pointers:

Slides: [https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes1](https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes1)  
Recent video: [https://www.elastic.co/videos/entity-centric-indexing-mark-harwood?q=entity](https://www.elastic.co/videos/entity-centric-indexing-mark-harwood?q=entity)  
Video from Meetup: [https://www.elastic.co/videos/entity-centric-indexing-london-meetup-sep-2014](https://www.elastic.co/videos/entity-centric-indexing-london-meetup-sep-2014)

---

<div class="post-metadata">

**Author:** ![SNAFUdowser](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@SNAFUdowser](https://discuss.elastic.co/u/SNAFUdowser)\
**Post date:** [July 23, 2015, 4:52pm UTC](https://discuss.elastic.co/t/kibana4-elasticsearch-api-using-javascript/26028/5 "2015-07-23T16:52:42Z")

</div>

My gut response is that this may be difficult because to construct each entity I would have to build a long chain of order IDs. Each node in the chain represents one machine, each with a previous and next node pointer. These chains can potentially be added to over a span of days. I believe because of this I would have to store each node in the ES, then when I add a new one, I check to see if there is an entity that contains that order ID. If there is, then I add this new node to that entity.

However, with that solution, there is a problem that if I have 3 nodes. Each part of the chain as such: A -\> B -\> C. A and C were first added to ES, my preprocessing would look at the data like this: ? -\> A -\> ?, and ? -\> C -\> ? which would cause me to store the two nodes in different indices, even if they are actually from the same chain.

Once C was added, I would have to, after the fact, grab C, delete the entity, and then put C into A's entity. This seems overly complex when my data will likely never grow to a size which I would run into a speed bottleneck from doing all these queries at request time, instead of being preprocessed.

**After thinking about it** : I think that entity-centric indices could be an OK solution. Is the implementation I wrote about above is accurate (with the ABC stuff)? Is it correct that I would have to continually shift around data inside ES to handle the case I specified above? Also, on a more technical note, he is speaking about grouping these entities by Type or by Index. Wouldn't there be too many indices?

While this option could be good, I'm not sure if it is the most time-efficient considering the added development time vs what benefits we would gain. Our data will only scale up to a few hundred GB at most, so I don't think our the queries will be too slow considering each will be made separately and against indexed fields.

---

<div class="post-metadata">

**Author:** ![SNAFUdowser](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@SNAFUdowser](https://discuss.elastic.co/u/SNAFUdowser)\
**Post date:** [July 28, 2015, 9:03pm UTC](https://discuss.elastic.co/t/kibana4-elasticsearch-api-using-javascript/26028/6 "2015-07-28T21:03:30Z")

</div>

##My Solution  
I finally realized that I should be extending the backend of Kibana4 written in Nodejs. I then edited:

```
/kibana/src/routes/index.js

```

and included a new URL. This one would be:

```
router.get('/trace-order')

```

From there I used the Nodejs Elasticsearch API documented here: [https://github.com/elastic/elasticsearch-js](https://github.com/elastic/elasticsearch-js) to perform the queries that I needed. Then when they completed and I did my calculations, I returned all my values in JSON.

---

<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 6, 2017, 2:15pm UTC](https://discuss.elastic.co/t/kibana4-elasticsearch-api-using-javascript/26028/7 "2017-07-06T14:15:45Z")

</div>


