Commit e19e3210 authored by Felipe Wu's avatar Felipe Wu

[001] Separei a consulta elastic

Signed-off-by: 's avatarFelipe Wu <felipeshiwu@gmail.com>
parent 997d0248
Pipeline #20083 failed with stages
in 40 seconds
......@@ -8,6 +8,10 @@ import { Client, ConfigOptions } from "elasticsearch";
/** @hidden */
const elasticsearch = require('elasticsearch');
/**
* Adapter which connects with a Elasticsearch database.
*/
......@@ -26,12 +30,49 @@ export class ElasticsearchAdapter extends Adapter {
}
public getQueryFromView(view: View): string {
public getQueryFromView(view: View): object {
/**
* Translate a view to a Elasticsearch query.
* @param view - View to be translated.
*/
/** A Elasticsearch query is JSON format, the header always begin with size:0 */
let initial = {
size: 0,
query: {
bool: {}
}
};
/** Query is used to define the filters */
let filter = {"match": { "TP_SEXO": "2" }};
let match = [];
match.push(filter);
Object.assign(initial.query.bool, {must: match});
//console.log(JSON.stringify(initial));
console.log(JSON.stringify(view));
/** Aggregation is used to define the dimention and the aggregation */
let aggregation = {
group_by_estado: {
terms: {
field: "CO_UF_NASC.keyword",
size: 1000,
order: {
average_idade: "asc"
}
},
aggs: {
average_idade: {avg: {field: "NU_IDADE"}},
sum_idade: {sum: {field: "NU_IDADE" }}
}
}
};
Object.assign(initial, {aggs: aggregation});
return "oi";
return initial;
}
/**
......@@ -43,12 +84,12 @@ export class ElasticsearchAdapter extends Adapter {
* @param cb.result - Data got from view.
*/
public getDataFromView(view: View, cb: (error: Error, result?: any[]) => void): void {
//const query = this.getQueryFromView(view);
const query = this.getQueryFromView(view);
//console.log(JSON.stringify(query));
//console.log(JSON.stringify(view));
//cb(null, []);
const query = "";
this.executeQuery(query, cb);
//this.executeQuery(query, cb);
this.teste(query, cb);
}
/**
......@@ -58,7 +99,17 @@ export class ElasticsearchAdapter extends Adapter {
* @param cb.error - Error information when the method fails.
* @param cb.result - Query result.
*/
private executeQuery(query: string, cb: (error: Error, result?: any[]) => void): void {
private teste(query: object, cb: (error: Error, result?: any[]) => void): void {
this.client.search({
index: 'teste',
body: query
},(err, result) => {
cb(err, [result]);
})
}
private executeQuery(query: object, cb: (error: Error, result?: any[]) => void): void {
this.client.search({
index: 'teste',
body: {
......@@ -94,11 +145,9 @@ export class ElasticsearchAdapter extends Adapter {
}
}
}
}).then(function(resp) {
cb(null, [resp]);
}, function(err) {
cb(err);
});
},(err, result) => {
cb(err, [result]);
})
}
/**
......@@ -110,7 +159,7 @@ export class ElasticsearchAdapter extends Adapter {
*/
public insertIntoSource(source: Source, data: any[], cb: (err: Error, result: any[]) => void): void {
// const query = this.getQueryFromSource(source, data);
const query = "";
const query = {};
this.executeQuery(query, cb);
}
......
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