Commit 53427b55 authored by Felipe Wu's avatar Felipe Wu

[005] finalizado

Signed-off-by: 's avatarFelipe Wu <felipeshiwu@gmail.com>
parent c39d572d
Pipeline #20105 failed with stages
in 33 seconds
......@@ -52,28 +52,11 @@ export class ElasticsearchAdapter extends Adapter {
}
/** Aggregation is used to define the dimention and the aggregation */
const func = this.getAggregateFunction(view.metrics[0].aggregation, view.origin);
let aggrName = func + "_" + view.metrics[0].name.toLowerCase();
let aggregation = {
group_by: {
terms: {
field: view.dimensions[0].name + ".keyword",
size: 10000,
order: {
[aggrName]: "asc"
}
},
aggs: {}
}
};
Object.assign(aggregation.group_by.aggs, {[aggrName]: {[func]: {field: view.metrics[0].name}}});
Object.assign(query, {aggs: aggregation});
let partialQuery = this.translateAggs(view, 0);
Object.assign(query, {aggs: partialQuery});
console.log(JSON.stringify(query, null, 2));
//console.log(JSON.stringify(view, null, 2));
return query;
}
......@@ -87,8 +70,11 @@ export class ElasticsearchAdapter extends Adapter {
*/
public getDataFromView(view: View, cb: (error: Error, result?: any[]) => void): void {
const query = this.getQueryFromView(view);
this.executeQuery(query, cb);
//this.executeFixQuery(query, cb);
if (view.clauses.length == 0 || view.dimensions.length > 1) {
this.executeQuery(view.operation.values[0].name, query, cb);
} else {
this.executeQuery(view.operation.values[0].operation.values[0].name, query, cb);
}
}
/**
......@@ -98,56 +84,15 @@ export class ElasticsearchAdapter extends Adapter {
* @param cb.error - Error information when the method fails.
* @param cb.result - Query result.
*/
private executeQuery(query: object, cb: (error: Error, result?: any[]) => void): void {
private executeQuery(index: string, query: object, cb: (error: Error, result?: any[]) => void): void {
this.client.search({
index: 'teste',
index: index.replace(/\"/g, ''),
body: query
},(err, result) => {
cb(err, [result]);
})
}
private executeFixQuery(query: object, cb: (error: Error, result?: any[]) => void): void {
this.client.search({
index: 'teste',
body: {
"size": 0,
"query": {
"bool": {
"must": [
{"match": { "TP_SEXO": 2 }}
]
}
},
"aggs": {
"group_by_estado": {
"terms": {
"field": "CO_UF_NASC.keyword",
"size": 10000,
"order": {
"average_idade": "asc"
}
},
"aggs": {
"average_idade": {
"avg": {
"field": "NU_IDADE"
}
},
"sum_idade": {
"sum": {
"field": "NU_IDADE"
}
}
}
}
}
}
},(err, result) => {
cb(err, [result]);
})
}
/**
* Asynchronously insert one register into a given Source.
......@@ -177,8 +122,7 @@ export class ElasticsearchAdapter extends Adapter {
}
private translateAggs(view: View, query: object,
numDimensions: number): object {
private translateAggs(view: View, numDimensions: number): object {
if (numDimensions == view.dimensions.length) {
let func = "";
let aggrName = "";
......@@ -190,24 +134,23 @@ export class ElasticsearchAdapter extends Adapter {
}
return partialQuery;
}
let returnedQuery = this.translateAggs(view, numDimensions+1);
if (view.dimensions.length > 0) {
let group_by = "group_by_" + view.dimensions[numDimensions].name;
let aggregation = {
[group_by]: {
terms: {
field: view.dimensions[0].name + ".keyword",
size: 10000
field: view.dimensions[numDimensions].name + ".keyword",
size: 100000
},
aggs: {}
}
};
let aggregate =
for (let i=0; i<view.dimensions.length; i++) {
Object.assign(aggregation.[group_by].aggs, {[aggrName]: {[func]: {field: view.metrics[0].name}}});
Object.assign(query, {aggs: aggregation});
Object.assign(aggregation[group_by], {aggs: returnedQuery});
return aggregation;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment