Skip to content

Commit

Permalink
[EEM] Use top_metrics for identity fields lifting (#188315)
Browse files Browse the repository at this point in the history
This PR converts the identity fields in the latest transform from a
`terms` aggregation to a `top_metric` aggregation in an effort to
simplify the implementation since we have to convert a terms aggregation
from keys to an array of strings. With the `top_metrics` implementation,
we just need to use the `dot_expander` processors along with a `set`
processor to get a single (non-array) field.

---------

Co-authored-by: Chris Cowan <[email protected]>
  • Loading branch information
miltonhultgren and simianhacker authored Jul 22, 2024
1 parent a68f812 commit dd126c4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 31 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,30 @@ function createMetadataPainlessScript(definition: EntityDefinition) {
}

function liftIdentityFieldsToDocumentRoot(definition: EntityDefinition) {
return definition.identityFields.map((identityField) => {
const optionalFieldPath = identityField.field.replaceAll('.', '?.');
const assignValue = `ctx.${identityField.field} = ctx.entity.identity.${identityField.field}.keySet().toArray()[0];`;
return {
script: {
if: `ctx.entity.identity.${optionalFieldPath} != null && ctx.entity.identity.${identityField.field}.size() != 0`,
source: cleanScript(`${initializePathScript(identityField.field)}\n${assignValue}`),
},
};
});
return definition.identityFields
.map((identityField) => {
const setProcessor = {
set: {
field: identityField.field,
value: `{{entity.identity.${identityField.field}.top_metric.${identityField.field}}}`,
},
};

if (!identityField.field.includes('.')) {
return [setProcessor];
}

return [
{
dot_expander: {
field: identityField.field,
path: `entity.identity.${identityField.field}.top_metric`,
},
},
setProcessor,
];
})
.flat();
}

export function generateLatestProcessors(definition: EntityDefinition) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ export function generateIdentityAggregations(definition: EntityDefinition) {
(aggs, identityField) => ({
...aggs,
[`entity.identity.${identityField.field}`]: {
terms: {
field: identityField.field,
size: 1,
filter: {
exists: {
field: identityField.field,
},
},
aggs: {
top_metric: {
top_metrics: {
metrics: {
field: identityField.field,
},
sort: '_score',
},
},
},
},
}),
Expand Down

0 comments on commit dd126c4

Please sign in to comment.