Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get any value of a document in a field formatter #6416

Closed
scampi opened this issue Mar 4, 2016 · 7 comments
Closed

Get any value of a document in a field formatter #6416

scampi opened this issue Mar 4, 2016 · 7 comments

Comments

@scampi
Copy link
Contributor

scampi commented Mar 4, 2016

When formatting a field, I only have access to the value of that field. But I have a scenario where I need to display the value of another field.


To illustrate, let's say I have the following data. I have two people that have the same name, but are uniquely identifiable with the id field.

Document Field Value
d1 id 1
d1 name John
d2 id 2
d2 name John

Now, if I want to create an aggregation on my people data, I need to aggregate on the id field to have correct values.


The problem is I don't want to display the value of the field id, but the name.
In my case, I also want to format the field as a URL.

It should be possible to specify which field to take the value from in the formatter, rather than simply proposing the parameters value and rawValue of the current field.
Also, this would be useful for any future formatter that makes some operation on the value.

@rashidkpc
Copy link
Contributor

Seems reasonable to pass the document in along with the value. Of course you'd be getting the unformatted value of the document, but you could apply whatever formatter you wanted at that point.

@scampi
Copy link
Contributor Author

scampi commented Apr 14, 2016

@rashidkpc there is an issue in the case of a visualization that aggregates on the field id from the example in the description.
Since the results of an aggregation query only consists of the aggregated field and its associated metric, I would need to query for the document in order to get the label (name in the example) associated to the id.

To achieve this, I would need the _convert method in the field converters to be asynchronous. Returning a Promise would be great.

@scampi
Copy link
Contributor Author

scampi commented Apr 14, 2016

I found a solution, but it works only with the data table visualization.

In rows.js the return of the html formatter can be an object with which I can specify an HTML template that is then compiled. In order to overcome the issue, I then created a directive that takes care of retrieving the field I want.

However, for other visualizations such as bar chart this object is not expected by the tooltip function https://github.com/elastic/kibana/blob/4c535f615500f78294726b4d087722bfa655c61e/src/ui/public/agg_response/point_series/_tooltip_formatter.js

@scampi
Copy link
Contributor Author

scampi commented Apr 19, 2016

So I tried to add this feature to kibana but it is difficult to do because field formatters are not async.

In some places I managed to add this feature:

  • in the data table, thanks to the html field formatter that can return a template that is then compiled. Therefore, I created a small directive that would retrieve the desired field, and the template would use it.
  • in the vertical bar chart, but here I needed to refactor the code:
    • _tooltip_formatter.js is expected to return HTML code which is then added to the Tooltip.
    • in order to allow for the async call, the logic above needed to be changed so that the HTML returned by the tooltip formatter is compiled in the tooltip.

However, I didn't manage to support this feature for the pie chart in the time I allocated myself for this issue.


I think it would be better if the API of a field formatter expects it to be asynchronous, regardless of the content type (text or html). More fancy field formatters could be created then as well.
This would require some code refactoring but would be worth it I believe.
What do you think @rashidkpc ?

@rashidkpc
Copy link
Contributor

Yeah, I totally agree, field formatter should be able to handle Promises. Ideally you'd be able to return either a promise or a literal, and do so without breaking backwards compat here.

@bmcconaghy
Copy link
Contributor

@scampi I don't think it makes sense for a field formatter to access other fields. Seems like you could do what you want in a scripted field, which has access to the entire document. I'm going to close this for now. Feel free to reopen if you disagree.

@JackRyanson
Copy link

@bmcconaghy i dont think what scampi wants can be done in a scripted field and its a perfectly valid and real world situation "who are the top people who did this" "john doe" "john doe" .. you want separate slices in the pie chart , ie. use the personID (not the mame) to aggregate. But you'd like a smarter tooltip that shows you the name instead of the ID.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants