0.0.0 • Published 1 year ago

elastic-table v0.0.0

Weekly downloads
1
License
-
Repository
-
Last release
1 year ago

The elastic-table module is useful and powerful module for those who are trying to build meaningful elasticsearch aggregations and transform it into Tabular format like either HTML table or csv.

Getting Started

Install elastic-table using npm:

$ npm install --save elastic-table

How to use package?

You can use it by:

  • create request.aggs body
  • transform response into requested format

Aggs:

Aggs is an aggregations builder for this package.

const { Aggs } = require('elastic-table');
var aggs = Aggs();

Every table is divided into columns and rows, so; you need to add your aggs as either column or row.

  • addColumn(type, field, options={}):

call it for each column you wanna add. All columns will be converted into composite aggregation, it takes 3 params:

- type: aggregation type, it could be: 'terms', 'histogram' or 'date_histogram'
- field: name of required field, notice that keyword fields needs: '.keyword' to be added
- options: based on aggregation type, like: 
    - order: should be 'asc' or 'desc', like composite.sources order.
    - size: it will be for all columns, cause composite.sources doesn't accept size*.
    - format: for both; terms and date_histogram**.
    - interval: required for 'histogram' and 'date_histogram'.

*size can be used only with 'terms'.

** terms inside composite doesn't accept format, but it will generate a script to handle it.

  • addRow(type, field, options={}):

call it for each row you wanna add. All rows will be converted into nested aggregations, it takes 3 params:

- type: aggregation type, it could be: 'terms', 'histogram', 'date_histogram', 'range' or 'date_range'
- field: name of required field, notice that keyword fields needs: '.keyword' to be added
- options: based on aggregation type, like: 
    - order: should be object like: { '_count': 'desc' }, like aggs order*.
    - size: it will be for all columns, cause composite.sources doesn't accept size**.
    - format: for both; terms and date_histogram.
    - interval: required for 'histogram' and 'date_histogram'.
    - range: required for 'range' and 'date_range'.

*order cannot be used with 'range' or 'date_range'.

**size can be used only with 'terms'.

  • doc_count(type, field, options={}):

call it to use metrics instead of default counting*. it takes 3 params:

- type: metrics aggregation type, it could be: 'value_count', 'min', 'max', 'sum' or 'avg'
- field: name of required field, notice that it must be numeric field.
- options: metrics options like: 
    - order: should be 'asc' or 'desc', in matter of fact; it doesn't allowed in elastic, but it will be converted into bucket_sort aggregation
    - missing: missing option for metrics aggs.

*can be used once only.

  • addMetrics(type, field, options={}):

same as doc_count, but to add more metrics.

  • build():

it called to convert Aggs object into elasticsearch aggs object.

const aggs = Aggs()
    .addColumn('terms', 'name.keyword', { order: 'asc' })
    .addRow('terms', 'company.keyword')
    .addColumn('date_histogram', 'birthdate', { interval: '1y', format: 'MM/yyyy' })
    .addRow('terms', 'department')
    .doc_count('avg', 'salary', { order: 'asc' })
    .addMetrix('sum', 'sales')

HTMLTable:

after you get elasticsearch response, that you built with Aggs; use HTMLTable to transform it into HTML table.

const { HTMLTable } = require('elastic-table');

let table = HTMLTable(response)
    .build()
  • formatter(callback):

You can format each cell into any format you want, by passing callback into formatter before build.

- callback: with 1 params and should return value.
by default it is: num => num, it will be useful to add postfix like: 
    .formatter(function(value) { return value + '$'; })
so, it will add $ for each cell.
  • style(obj):

Also; you add inline-style to your table using this function before build.

- obj: it should be like:
    {
        table: "inline-style inside table tag",
        thead: {
            style: 'inline-style inside thead tag',
            tr: 'inline-style inside each tr tag in thead',
            th: 'inline-style inside each th tag in tr'
        },
        tbody: {
            style: 'inline-style inside thead tag',
            tr: 'inline-style inside each tr tag in tbody',
            td: 'inline-style inside each td tag in tr'
        },
        tfoot: {
            style: 'inline-style inside thead tag',
            tr: 'inline-style inside each tr tag in tfoot',
            th: 'inline-style inside each th tag in tr'
        },
    }
0.0.0

1 year ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago