Skip to content

poc(related) alerts custom score query#214017

Draft
kdelemme wants to merge 6 commits into
elastic:mainfrom
kdelemme:poc/related-alerts-custom-score-query
Draft

poc(related) alerts custom score query#214017
kdelemme wants to merge 6 commits into
elastic:mainfrom
kdelemme:poc/related-alerts-custom-score-query

Conversation

@kdelemme
Copy link
Copy Markdown
Contributor

@kdelemme kdelemme commented Mar 11, 2025

This PoC reuses the alert query hook and a custom table to display the results.
The query is currently not configurable by the user, but ideally we would offer some weight configuration on the different heuristics we use, as well as some more "on/off" filter, like on the alert status, alert start date, etc...

Ideally we could add to existing/new cases, and go to the alert details (or open in flyout)

image

@kdelemme kdelemme changed the title poc/related alerts custom score query poc(related) alerts custom score query Mar 11, 2025
@elasticmachine
Copy link
Copy Markdown
Contributor

elasticmachine commented Mar 11, 2025

🤖 Jobs for this PR can be triggered through checkboxes. 🚧

ℹ️ To trigger the CI, please tick the checkbox below 👇

  • Click to trigger kibana-pull-request for this PR!
  • Click to trigger kibana-deploy-project-from-pr for this PR!
  • Click to trigger kibana-deploy-cloud-from-pr for this PR!

export function RelatedAlertsView({ alert }: Props) {
const { services } = useKibana();

const esQuery = useBuildRelatedAlertsQuery({ alert });
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 Add the user's specified weights & filters in the mix

Copy link
Copy Markdown
Contributor

@tveasey tveasey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall I like the approach (and it is really crafty to do it with custom scoring function 👍. I proposed some potential small tweaks. Happy to discuss these offline.

script: {
source: dedent(`
double jaccardSimilarity(Set a, Set b) {
if (a.size() == 0 || b.size() == 0) return 0.0;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If either set is empty or the intersect is empty we'll never get a match (the score is forced to zero). I'm not sure this is desirable.

For example, if $A$ is empty I would argue the instance on the candidate alert carries no additional information, i.e. the multiplier should be 1. Although I'm not sure under what circumstances this happens. Then if there is a match there is a positive boost, a boost of $1 + w \frac{A \cap B}{A \cup B}$ could be an option.

If both alerts have instances and there is no match I'm not sure, but I would we be inclined to think it should still be possible to match on other attributes. Perhaps though this should actively count against matching, i.e. multiplicative boost should be < 1.

One option which achieves all this is the following function:

$$1 + w \frac{A \cup B}{A \cap B} - 0.5 \times 1[A\neq \emptyset \text{ AND } B\neq \emptyset]$$

Here, $1[\cdot]$ denotes the indicator function. So the possible scores would be $0.5$, $1$, $1 + w \frac{m}{n}$ for $m \leq n$. Here, $w$ can be adjusted based on how much weight you want to assign uniquely matching the instance.

script_score: {
script: {
source: dedent(`
double jaccardSimilarity(Set a, Set b) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A similar comment applies regarding no matching tags to no matching instances.

Comment on lines +93 to +117
exp: {
[ALERT_START]: {
origin: startDate.toISOString(),
scale: '10m',
offset: '10m',
decay: 0.5,
},
},
weight: 10,
},
...(endDate
? [
{
exp: {
[ALERT_END]: {
origin: endDate.toISOString(),
scale: '10m',
offset: '10m',
decay: 0.5,
},
},
weight: 10,
},
]
: []),
Copy link
Copy Markdown
Contributor

@tveasey tveasey Mar 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like exponential decay is reasonable. I think this should be based on the interval distance though (so might need script score). Specifically, if alerts have intervals $[a_1, b_1]$ and $[a_2, b_2]$ I would use something like

$$score = \exp(-(\max(a_1 - b_2, 0) - \max(a_2 - b_1, 0)) / T)$$

This calculates the distance between the intervals if they don't overlap and is otherwise 0.

On efficiency grounds one should probably add some filter conditions as well, i.e. must not "end time of one alert" << "start time of other". This will allow Lucene to skip many candidate matches and only run the script scoring on the remainder.

dominiqueclarke added a commit that referenced this pull request Apr 14, 2025
## Summary

Copying most of #214017 !!

Fixes #214372

### Implementation
We are now using response ops alerts table with custom score querying
based on tags/groups matches and Jaccard similarity on documents !!


<img width="1728" alt="image"
src="https://github.com/user-attachments/assets/b3a69280-c05d-4100-be6a-2c8dadcc051d"
/>

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
Co-authored-by: Dominique Clarke <dominique.clarke@elastic.co>
Co-authored-by: Kevin Delemme <kevin.delemme@elastic.co>
shahzad31 added a commit that referenced this pull request Apr 22, 2025
…218664)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Observability] Related alerts based on scoring !!
(#215673)](#215673)

<!--- Backport version: 9.6.6 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sorenlouv/backport)

<!--BACKPORT
[{"author":{"name":"Shahzad","email":"shahzad31comp@gmail.com"},"sourceCommit":{"committedDate":"2025-04-14T15:35:40Z","message":"[Observability]
Related alerts based on scoring !! (#215673)\n\n## Summary\n\nCopying
most of #214017 !!\n\nFixes
https://github.com/elastic/kibana/issues/214372\n\n###
Implementation\nWe are now using response ops alerts table with custom
score querying\nbased on tags/groups matches and Jaccard similarity on
documents !!\n\n\n<img width=\"1728\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/b3a69280-c05d-4100-be6a-2c8dadcc051d\"\n/>\n\n---------\n\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>\nCo-authored-by:
Dominique Clarke <dominique.clarke@elastic.co>\nCo-authored-by: Kevin
Delemme
<kevin.delemme@elastic.co>","sha":"760106eb8611a934d10157fb021979637d49dae4","branchLabelMapping":{"^v9.1.0$":"main","^v8.19.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:obs-ux-management","backport:version","v9.1.0","v8.19.0"],"title":"[Observability]
Related alerts based on scoring
!!","number":215673,"url":"https://github.com/elastic/kibana/pull/215673","mergeCommit":{"message":"[Observability]
Related alerts based on scoring !! (#215673)\n\n## Summary\n\nCopying
most of #214017 !!\n\nFixes
https://github.com/elastic/kibana/issues/214372\n\n###
Implementation\nWe are now using response ops alerts table with custom
score querying\nbased on tags/groups matches and Jaccard similarity on
documents !!\n\n\n<img width=\"1728\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/b3a69280-c05d-4100-be6a-2c8dadcc051d\"\n/>\n\n---------\n\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>\nCo-authored-by:
Dominique Clarke <dominique.clarke@elastic.co>\nCo-authored-by: Kevin
Delemme
<kevin.delemme@elastic.co>","sha":"760106eb8611a934d10157fb021979637d49dae4"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.1.0","branchLabelMappingKey":"^v9.1.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/215673","number":215673,"mergeCommit":{"message":"[Observability]
Related alerts based on scoring !! (#215673)\n\n## Summary\n\nCopying
most of #214017 !!\n\nFixes
https://github.com/elastic/kibana/issues/214372\n\n###
Implementation\nWe are now using response ops alerts table with custom
score querying\nbased on tags/groups matches and Jaccard similarity on
documents !!\n\n\n<img width=\"1728\"
alt=\"image\"\nsrc=\"https://github.com/user-attachments/assets/b3a69280-c05d-4100-be6a-2c8dadcc051d\"\n/>\n\n---------\n\nCo-authored-by:
kibanamachine
<42973632+kibanamachine@users.noreply.github.com>\nCo-authored-by:
Dominique Clarke <dominique.clarke@elastic.co>\nCo-authored-by: Kevin
Delemme
<kevin.delemme@elastic.co>","sha":"760106eb8611a934d10157fb021979637d49dae4"}},{"branch":"8.x","label":"v8.19.0","branchLabelMappingKey":"^v8.19.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

---------

Co-authored-by: Shahzad <shahzad31comp@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants