# Finding the query in agg\_table.js context

**URL:** <https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336>\
**Category:** Kibana\
**Created:** [September 4, 2017, 4:46pm UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336 "2017-09-04T16:46:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leofer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leofer/32/71260_2.png) [@leofer](https://discuss.elastic.co/u/leofer)\
**Post date:** [September 4, 2017, 4:46pm UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336/1 "2017-09-04T16:46:06Z")

</div>

Hi,

I'm trying to develop server side query analysis tool, and I'm modifying the src/ui/public/agg\_table/agg\_table.js

I was able to add some stuff, but what I would really like to to, is to get the query text (just like spy mode -\> request) in this context.

Any help?

thanks!!! this is really important for me...

---

<div class="post-metadata">

**Author:** ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)\
**Post date:** [September 5, 2017, 8:08am UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336/2 "2017-09-05T08:08:22Z")

</div>

Hi @leofer,

I want to prefix this by recommending to not modify the Kibana source itself, but to instead implement things lika that as a plugin that provides a new visualization type.

That being said, what you are probably looking for is to get hold of the `SearchSource` and use the `body` property of the [`SearchSource._flatten()`](https://github.com/elastic/kibana/blob/dc9a2ee3ea34018c00d90496e55b2e3d1e5ae01c/src/ui/public/courier/data_source/_abstract.js#L264-L379) call's return value.

---

<div class="post-metadata">

**Author:** ![leofer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leofer/32/71260_2.png) [@leofer](https://discuss.elastic.co/u/leofer)\
**Post date:** [September 5, 2017, 9:52pm UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336/3 "2017-09-05T21:52:32Z")

</div>

Thanks Felix for the prompt reply.

Doing this as a plugin is an interesting idea - working on plugin should improve maintenance. Can you please point me to an example plugin which uses SearchSource ?

---

<div class="post-metadata">

**Author:** ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)\
**Post date:** [September 6, 2017, 8:50am UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336/4 "2017-09-06T08:50:59Z")

</div>

The visualization type infrastructure provides an extensive abstraction over data retrieval and dashboard embedding. Since all built-in visualization are implemented as plugins as well, I would recommend to look at one of them (e.g. the [data table visualization](https://github.com/elastic/kibana/tree/4cc98fc3ad593bc30daf8cbc4317df01652ba4bb/src/core_plugins/table_vis)). Maybe you could copy the visualization, rename it and adapt it to your use-case. Unfortunately there is no authoritative documentation on writing visualization plugins, but there are several community-authored tutorials such as [https://www.timroes.de/2015/12/06/writing-kibana-4-plugins-visualizations-using-data/](https://www.timroes.de/2015/12/06/writing-kibana-4-plugins-visualizations-using-data/). The official documentation also contains an [overview of the basic structure of a plugin](https://www.elastic.co/guide/en/kibana/current/plugin-development.html).

---

<div class="post-metadata">

**Author:** ![leofer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leofer/32/71260_2.png) [@leofer](https://discuss.elastic.co/u/leofer)\
**Post date:** [September 7, 2017, 7:19am UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336/5 "2017-09-07T07:19:29Z")

</div>

I read the official documentation of plugin development and everything I could find online, and SearchSource does look like what need - but I'm still not sure how to use it. Could you point me to an example of how to "get a hold of SearchSource" ?

thanks in advance,  
Ofer

---

<div class="post-metadata">

**Author:** ![leofer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leofer/32/71260_2.png) [@leofer](https://discuss.elastic.co/u/leofer)\
**Post date:** [September 12, 2017, 8:36pm UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336/6 "2017-09-12T20:36:53Z")

</div>

So...I added the following line:

```
    const p = Private(SearchSourceProvider).prototype._flatten();

```

inside the file:  
$KIBANA\_HOME/src/ui/public/agg\_table/agg\_table.js

within self.exportAsCsv (line 41).

I'm getting a Promise which seems to be some flashy concept of ES6, not sure what's that but found some ref here:

> **[Promise.resolve()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve)**
>
> The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; if the value was a...

So I added a breakpoint in developer mode (chrome) and in the console I added

p.then(function(value) {  
console.log('value: ' + value);  
});

expecting finally to see my query logged in the console, whereas instead I'm getting the following error...help..please

angular.js:12477 TypeError: Cannot read property 'index' of undefined  
at SourceAbstract.AbstractDataSourceProvider.SourceAbstract.get (\_abstract.js:90)  
at \_root\_search\_source.js:17  
at SourceAbstract.SearchSourceProvider.SearchSource.\_mergeProp (search\_source.js:172)  
at ittr (\_abstract.js:316)  
at index.js:2275  
at index.js:3073  
at baseForOwn (index.js:2046)  
at index.js:3043  
at baseMap (index.js:2274)  
at Function.map (index.js:6710)  
at ittr (\_abstract.js:309)  
at \_abstract.js:324  
at processQueue (angular.js:14745)  
at angular.js:14761  
at Scope.$eval (angular.js:15989)  
at Scope.$digest (angular.js:15800)

---

<div class="post-metadata">

**Author:** ![weltenwort](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/weltenwort/32/53885_2.png) [@weltenwort](https://discuss.elastic.co/u/weltenwort)\
**Post date:** [September 13, 2017, 9:00am UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336/7 "2017-09-13T09:00:36Z")

</div>

You're trying to call the instance method `_flatten()` without and instance by accessing the `prototype` directly. In order to use the search source you have to get hold of an instance. You can either create your own using something like

```
const SearchSource = Private(SearchSourceProvider);
const searchSource = new SearchSource();

```

or use the `searchSource` of the `savedObj` (similar to how it is done in [https://github.com/elastic/kibana/blob/b5527aebdc66c87e1e0c18f6537dc639ec261618/src/ui/public/visualize/visualize.js#L58](https://github.com/elastic/kibana/blob/b5527aebdc66c87e1e0c18f6537dc639ec261618/src/ui/public/visualize/visualize.js#L58)). Alternatively you could use the query abstraction provided by Kibana's visualization layer. Which one is appropriate would depend on what you want to accomplish. If your code is available on GitHub, I could take a look and give more specific advice.

---

<div class="post-metadata">

**Author:** ![leofer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leofer/32/71260_2.png) [@leofer](https://discuss.elastic.co/u/leofer)\
**Post date:** [September 13, 2017, 3:17pm UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336/8 "2017-09-13T15:17:25Z")

</div>

THANKS, but now I'm getting another error (below).

I have uploaded it to git, here: [https://github.com/gorefbitim/kibana/commit/aa03e495a1c8d3858e13e8a94f5a6ef17c229075](https://github.com/gorefbitim/kibana/commit/aa03e495a1c8d3858e13e8a94f5a6ef17c229075)

The error ,

I put BreakPoint at line 64  
console.log("AFTER");

Then I copy the following lines to the console on development mode (client side, chrome) and hit F8

```
    searchSource._flatten().then(function(value) {
        console.log('value: ' + value);
    });

```

I'm getting:

AFTER  
TypeError: Cannot read property 'timeFieldName' of undefined  
at Timefilter.get (timefilter.js:87)  
at \_root\_search\_source.js?422d:17  
at SearchSourceProvider.SearchSource.\_mergeProp (search\_source.js?5034:172)  
at ittr (\_abstract.js?851d:316)  
at index.js:2275  
at index.js:3073  
at baseForOwn (index.js:2046)  
at index.js:3043  
at baseMap (index.js:2274)  
at Function.map (index.js:6710)  
at ittr (\_abstract.js?851d:309)  
at \_abstract.js?851d:324  
at processQueue (angular.js:14745)  
at angular.js:14761  
at Scope.$eval (angular.js:15989)  
at Scope.$digest (angular.js:15800)  
(anonymous) @ angular.js:12477  
(anonymous) @ angular.js:9246  
processQueue @ angular.js:14753  
(anonymous) @ angular.js:14761  
$eval @ angular.js:15989  
$digest @ angular.js:15800  
$apply @ angular.js:16097  
(anonymous) @ angular.js:23554  
dispatch @ jquery.js:4737  
elemData.handle @ jquery.js:4549

---

<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:** [October 11, 2017, 3:17pm UTC](https://discuss.elastic.co/t/finding-the-query-in-agg-table-js-context/99336/9 "2017-10-11T15:17:48Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
