Skip to content

Commit

Permalink
refactor aggs
Browse files Browse the repository at this point in the history
  • Loading branch information
ksuess committed Nov 15, 2022
1 parent bb8bf2f commit 00100d4
Showing 1 changed file with 69 additions and 68 deletions.
137 changes: 69 additions & 68 deletions src/components/Searchkit/CustomESRequestSerializer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,14 @@ export class CustomESRequestSerializer {
// }
// },

const aggFieldsMapping = {
// 'kompasscomponent_agg.inner.kompasscomponent_token': 'kompasscomponent',
'informationtype_agg.inner.informationtype_token': 'informationtype',
};
// TODO Make filter configurable
// const aggFieldsMapping = {
// // 'kompasscomponent_agg.inner.kompasscomponent_token': 'kompasscomponent',
// 'informationtype_agg.inner.informationtype_token': 'informationtype',
// };
const getFieldnameFromAgg = (agg) => {
return agg.split(".")[0].replace("_agg", "")
}

let terms = [];
terms.push({
Expand All @@ -292,7 +296,7 @@ export class CustomESRequestSerializer {
const additionalterms = Object.keys(aggValueObj).reduce(
(accumulator, aggName) => {
const obj = {};
const fieldName = aggFieldsMapping[aggName];
const fieldName = getFieldnameFromAgg(aggName);
obj[fieldName] = aggValueObj[aggName];
if (listFilterFields.includes(fieldName)) {
accumulator.push({ terms: obj });
Expand All @@ -305,7 +309,7 @@ export class CustomESRequestSerializer {

filter = Object.keys(aggValueObj).reduce((accumulator, aggName) => {
const obj = {};
const fieldName = aggFieldsMapping[aggName];
const fieldName = getFieldnameFromAgg(aggName);
obj[fieldName] = aggValueObj[aggName];
if (this.nestedFilterFields.includes(fieldName)) {
accumulator.push({
Expand Down Expand Up @@ -354,78 +358,75 @@ export class CustomESRequestSerializer {
});

// 2. aggregations of nestedFilterFields
Object.keys(aggFieldsMapping).map((aggName) => {
const myaggs = aggName.split('.');
const fieldName = aggFieldsMapping[aggName];
if (this.nestedFilterFields.includes(fieldName)) {
// const filter_debug = {
// nested: {
// path: 'informationtype',
// query: {
// bool: {
// must: [
// {
// terms: {
// 'informationtype.token': ['Anleitung', 'FAQ'],
// },
// },
// ],
// },
// },
// },
// };

function aggregation_filter(agg) {
// agg is a key of aggFieldsMapping.
// something like 'kompasscomponent_agg.inner.kompasscomponent_token'
// return filter_debug;
return isEmpty(filter)
? { match_all: {} }
: {
bool: {
filter: filter.filter(
(el) => !agg[0].startsWith(el.nested.path),
),
},
};
}
this.nestedFilterFields.map((fieldName) => {
const myaggs = [`${fieldName}_agg`, 'inner', `${fieldName}_token`];
// const filter_debug = {
// nested: {
// path: 'informationtype',
// query: {
// bool: {
// must: [
// {
// terms: {
// 'informationtype.token': ['Anleitung', 'FAQ'],
// },
// },
// ],
// },
// },
// },
// };

function aggregation_filter(agg) {
// agg is a key of aggFieldsMapping.
// something like 'kompasscomponent_agg.inner.kompasscomponent_token'
// return filter_debug;
return isEmpty(filter)
? { match_all: {} }
: {
bool: {
filter: filter.filter(
(el) => !agg[0].startsWith(el.nested.path),
),
},
};
}

const aggBucketTermsComponent = {
[myaggs[0]]: {
aggs: {
inner: {
nested: {
path: fieldName,
},
aggs: {
[myaggs[2]]: {
terms: {
field: fieldName + '.token',
order: {
_key: 'asc',
},
size: 30, // number of buckets
const aggBucketTermsComponent = {
[myaggs[0]]: {
aggs: {
inner: {
nested: {
path: fieldName,
},
aggs: {
[myaggs[2]]: {
terms: {
field: fieldName + '.token',
order: {
_key: 'asc',
},
aggs: {
somemoredatafromelasticsearch: {
top_hits: {
size: 1,
_source: { includes: [fieldName] },
},
size: 30, // number of buckets
},
aggs: {
somemoredatafromelasticsearch: {
top_hits: {
size: 1,
_source: { includes: [fieldName] },
},
},
},
},
},
},
},
};
const flt = aggregation_filter(myaggs);
if (!isEmpty(flt)) {
aggBucketTermsComponent[myaggs[0]].filter = flt;
}
extend(bodyParams['aggs'], aggBucketTermsComponent);
},
};
const flt = aggregation_filter(myaggs);
if (!isEmpty(flt)) {
aggBucketTermsComponent[myaggs[0]].filter = flt;
}
extend(bodyParams['aggs'], aggBucketTermsComponent);
});
// console.debug('CustomESRequestSerializer bodyParams', bodyParams);
return bodyParams;
Expand Down

0 comments on commit 00100d4

Please sign in to comment.