Skip to content

Commit

Permalink
Merge pull request #8 from Blynskyniki/sub-aggs
Browse files Browse the repository at this point in the history
feat: add post filter in query, add sub aggs
  • Loading branch information
Blynskyniki authored Feb 4, 2021
2 parents 56f1a97 + e7b3804 commit de4dea6
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 54 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "elastic-dsl-typescript",
"version": "1.0.7",
"version": "1.1.1",
"description": "Node.js ElasticSearch search query builder",
"main": "./",
"scripts": {
Expand Down
16 changes: 13 additions & 3 deletions src/Builders/Aggregation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,31 @@ export class Aggregation<SCHEMA extends AggregationSchema> extends AbstractBulde
private _data = {};

public add<Type extends keyof SCHEMA>(aggType: Type, name: string, d: SCHEMA[Type]) {
const { filter, ...all } = d.params as { filter?: any };
const { filter, subAgg, ...all } = d.params as { filter?: any; subAgg?: any };
let sub = {};
if (subAgg) {
sub = {
aggs: {
...subAgg
}
};
}
if (filter) {
this._data[name] = {
filter,
aggs: {
[`${name}_filtered`]: {
[aggType]: { ...(all as object), ...((d.opts as object) || {}) }
[aggType]: { ...all, ...((d.opts as object) || {}) },
...sub
}
}
};
return this;
}

this._data[name] = {
[aggType]: { ...(d.params as object), ...((d.opts as object) || {}) }
[aggType]: { ...all, ...((d.opts as object) || {}) },
...sub
};
return this;
}
Expand Down
26 changes: 17 additions & 9 deletions src/Builders/Aggregation/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { AggSchema } from "../../Abstract/Schema";
import { Range, RangeAggregation, PainLessScript } from "../../Types";
import { RangeAggregation, PainLessScript } from "../../Types";


export interface SubAggregation {
subAgg?: FilterAggregation;
}

export interface AggregationSchema extends AggSchema {
/**
Expand All @@ -12,7 +17,10 @@ export interface AggregationSchema extends AggSchema {
*/
field: string;
filter?: FilterAggregation;
};
/**
* @property subAgg Вложенные sub аггрегации
*/
} & SubAggregation;
opts?: {
/**
* @property Return basket size
Expand All @@ -32,50 +40,50 @@ export interface AggregationSchema extends AggSchema {
range: {
params: {
field: string;

filter?: FilterAggregation;
ranges: RangeAggregation;
};
};

avg: {
params: {
field: string;

filter?: FilterAggregation;
script?: PainLessScript;
};
};
max: {
params: {
field: string;

filter?: FilterAggregation;
script?: PainLessScript;
};
};
min: {
params: {
field: string;

filter?: FilterAggregation;
script?: PainLessScript;
};
};
sum: {
params: {
field: string;

filter?: FilterAggregation;
script?: PainLessScript;
};
};
percentiles: {
params: {
field: string;

filter?: FilterAggregation;
percents?: [number, number, number];
};
};
value_count: {
params: {
field: string;

filter?: FilterAggregation;
script?: PainLessScript;
};
};
Expand Down
25 changes: 19 additions & 6 deletions src/Query/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import { RawQuery } from './types';
* @Classdesc Generator queries for Elastic search
*/
export class Query<BOOL_SCHEMA extends BoolSchema> extends AbstractBulder {
private _props: Omit<RawQuery, 'query'> = {};
private _query: RawQuery['query'] = {};
private _props: Omit<RawQuery, "query"> = {};
private _query: RawQuery["query"] = {};
private _post_filter: Bool = new Bool<BoolSchema>();

/**
* Add basic props to Query (size,from,_source,etc...)
* @param prop
* @param data
* @returns {this<BOOL_SCHEMA>}
*/
public addProps<K extends keyof Omit<RawQuery, 'query'>>(prop: K, data: RawQuery[K]) {
public addProps<K extends keyof Omit<RawQuery, "query">>(prop: K, data: RawQuery[K]) {
this._props[prop] = data;
return this;
}
Expand All @@ -38,13 +39,22 @@ export class Query<BOOL_SCHEMA extends BoolSchema> extends AbstractBulder {
* @returns {Bool<BoolSchema>}
*/
get bool(): Bool<BOOL_SCHEMA> {
if (this.isNotExistInQuery('bool')) {
if (this.isNotExistInQuery("bool")) {
this._query.bool = new Bool();
}
return this._query.bool!;
}

private isNotExistInQuery(prop: keyof RawQuery['query']) {
get postFilter(): Bool<BOOL_SCHEMA> {
return this._post_filter;
}

public addPostFilter(bool: Bool<BOOL_SCHEMA>) {
this._post_filter = bool;
return this;
}

private isNotExistInQuery(prop: keyof RawQuery["query"]) {
return !(prop in this._query);
}

Expand Down Expand Up @@ -76,7 +86,10 @@ export class Query<BOOL_SCHEMA extends BoolSchema> extends AbstractBulder {
}
query[prop] = val;
}
obj['query'] = query;
obj["query"] = query;
if (this._post_filter.isNotEmty()) {
obj["post_filter"] = this._post_filter.build();
}
return obj;
}

Expand Down
12 changes: 8 additions & 4 deletions src/Query/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AggregationSchema } from '..';
import { Bool } from '../Builders/Bool';
import { BoolSchema } from '../Builders/Bool/types';
import { Sort, Range } from "../Types";
import { AggregationSchema } from "..";
import { Aggregation } from "../Builders/Aggregation";
import { Bool } from "../Builders/Bool";
import { BoolSchema } from "../Builders/Bool/types";
import { Range, Sort } from "../Types";

/**
*
Expand Down Expand Up @@ -35,6 +35,10 @@ export interface RawQuery {
boost?: number;
};
};
/**
* Post aggregations filters
*/
post_filter?: Bool<BoolSchema>;
sort?: Sort[];
aggs?: Aggregation<AggregationSchema>;
_source?: string[];
Expand Down
84 changes: 83 additions & 1 deletion src/__test__/Aggs/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Aggregation } from '../../Builders/Aggregation';
import { Aggregation } from "../../Builders/Aggregation";
import { Bool } from "../../Builders/Bool";

describe('Check aggs builder', () => {
beforeEach(() => {
Expand Down Expand Up @@ -173,4 +174,85 @@ describe('Check aggs builder', () => {
}
});
});

test("Create range with sub aggs", async () => {
const a = new Aggregation().add("range", "my_range", {
params: {
field: "price",
filter: new Bool()
.Must("exists", {
params: {
fieldName: "testfield"
}
})
.build(),
ranges: [
{
from: 1
}
]
}
});

expect(a.build()).toEqual({
my_range: {
aggs: {
my_range_filtered: {
range: {
field: "price",
ranges: [
{
from: 1
}
]
}
}
},
filter: {
bool: {
must: [
{
exists: {
field: "testfield"
}
}
]
}
}
}
});
});
test("Create term sub aggs", async () => {
const a = new Aggregation().add("terms", "testField", {
params: {
field: "testField",
subAgg: {
stores: {
terms: {
field: "availStores",
size: 5,
include: ["124214"]
}
}
}
}
});

expect(a.build()).toEqual({
testField: {
aggs: {
stores: {
terms: {
field: "availStores",
include: ["124214"],
size: 5
}
}
},
terms: {
field: "testField"
}
}
});
});
});
Loading

0 comments on commit de4dea6

Please sign in to comment.