Skip to content

Commit

Permalink
feat: add eshop active/inactive facets builder
Browse files Browse the repository at this point in the history
  • Loading branch information
Niki committed Aug 24, 2021
1 parent 00acb51 commit 61f59a0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 12 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.1.5",
"version": "1.1.7",
"description": "Node.js ElasticSearch search query builder",
"main": "./",
"scripts": {
Expand Down
19 changes: 9 additions & 10 deletions src/Builders/Aggregation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,40 @@ import { AggregationSchema } from './types';

export class Aggregation<SCHEMA extends AggregationSchema> extends AbstractBulder {
private _data = {};

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

this._data[name] = {
[aggType]: { ...all, ...((d.opts as object) || {}) },
...sub
[aggType]: {...all, ...((d.opts as object) || {})},
...sub,
};
return this;
}

public addCustom(name: string, cutsomAgg: object) {
this._data[name] = cutsomAgg;
}


public build(): object {
return this._data;
}
Expand Down
75 changes: 75 additions & 0 deletions src/Builders/EshopFacets/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import {AggregationSchema} from 'Builders/Aggregation/types';
import {Bool} from 'Builders/Bool';

interface IFacet {
active: object;
inactive: InactiveFacet;
}

type FacetType = keyof IFacet;

type InactiveFacet = Record<'filter' | 'aggs', object>;

export class EshopFacets {
private _facets: IFacet = {
active: {},
inactive: {
aggs: {},
filter: {},
},
};

public setupInactiveFacets(filter: Bool): void {
this._facets.inactive!.filter = filter;
}

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

this._facets[facetType][name] = {
[aggType]: {...all, ...((d.opts as object) || {})},
...sub,
};
return this;
}

public addCustom(facetType: FacetType, name: string, cutsomAgg: object) {
this._facets[facetType][name] = cutsomAgg;
}

public build(): object {
if (Object.keys(this._facets.inactive.aggs).length > 0 && Object.keys(this._facets.inactive.filter).length < 1) {
throw new Error('Please setup inactive facets');
}
return this._facets;
}

public isNotEmty(): boolean {
return Object.keys({...this._facets.active, ...this._facets.inactive.aggs}).length > 0;
}
}
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export * from './Query';
export * from './Query/types';

// abstract
// Abstract
export * from './Abstract/AbstractBuilder';
export * from './Abstract/Schema';
// Bool
Expand All @@ -16,3 +16,8 @@ export * from './Builders/Nested/types';
// Aggregation
export * from './Builders/Aggregation/index';
export * from './Builders/Aggregation/types';
// Text
export * from './Builders/Text/index';
export * from './Builders/Text/types';
// Eshop facets
export * from './Builders/EshopFacets';

0 comments on commit 61f59a0

Please sign in to comment.