Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed transformation of document field names before running percolator search #845

Merged

Conversation

petardz
Copy link
Contributor

@petardz petardz commented Mar 31, 2023

Issue #, if available: #844

Description of changes:

Fixed transformation of document field names before running percolator search.

Documents which are ingested with "nested fields" weren't handled properly. Example:

"field1": {
   "field2": 12345
}

CheckList:
[ ] Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@petardz petardz requested a review from a team March 31, 2023 00:11
petardz added 3 commits March 31, 2023 02:13
Signed-off-by: Petar Dzepina <[email protected]>
… of outputing flatten json

Signed-off-by: Petar Dzepina <[email protected]>
Signed-off-by: Petar Dzepina <[email protected]>
@eirsep
Copy link
Member

eirsep commented Mar 31, 2023

#844


val sourceRef = BytesReference.bytes(xContentBuilder)

Pair(hit.id, sourceRef)
}
}

/**
* Traverses document fields in leaves recursively and appends [fieldNameSuffix] to field names.
Copy link
Member

@eirsep eirsep Mar 31, 2023

Choose a reason for hiding this comment

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

mention that we are trying to flatten the object type fields for percolate query

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, we can't flatten it, because of type=nested. You can't insert document with flatten fields if type is nested. For example, let's say field "rule" is type=nested, then this is invalid:

{
     "rule.category": "windows"
}

but this is valid:

{
     "rule" : {
          "category": "windows"
     }
}

Copy link
Member

Choose a reason for hiding this comment

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

i am not talking about type=nested

* @param jsonAsMap Input JSON (as Map)
* @param fieldNameSuffix Field suffix which is appended to existing field name
*/
private fun transformDocumentFieldNames(
Copy link
Member

Choose a reason for hiding this comment

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

what happens if customer has a nested type mapping for the index.

plz add a test case to verify we are handling that and fix it if required.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a test case with document with nested fields: "test execute monitor with custom query index and nested mappings"

if (entry.value is Map<*, *>) {
transformDocumentFieldNames(entry.value as MutableMap<String, Any>, fieldNameSuffix)
} else if (entry.key.endsWith(fieldNameSuffix) == false) {
tempMap["${entry.key}_$fieldNameSuffix"] = entry.value
Copy link
Member

Choose a reason for hiding this comment

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

add debug logs with field name generated and mention percolate query stage, monitor id

we are lacking insights into what is going wrong
this might help

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added trace log here printing whole document

Copy link
Member

@eirsep eirsep left a comment

Choose a reason for hiding this comment

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

Thanks for this important fix Petar,
added a few comments reg. testing, logging and verbose comments/documentation

Signed-off-by: Petar Dzepina <[email protected]>
@codecov-commenter
Copy link

Codecov Report

Merging #845 (a5aa476) into main (3f2e651) will increase coverage by 0.09%.
The diff coverage is 88.88%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@             Coverage Diff              @@
##               main     #845      +/-   ##
============================================
+ Coverage     75.46%   75.55%   +0.09%     
+ Complexity      111      110       -1     
============================================
  Files           125      125              
  Lines          6885     6886       +1     
  Branches       1034     1036       +2     
============================================
+ Hits           5196     5203       +7     
+ Misses         1158     1154       -4     
+ Partials        531      529       -2     
Impacted Files Coverage Δ
.../opensearch/alerting/DocumentLevelMonitorRunner.kt 83.38% <88.88%> (+3.11%) ⬆️

... and 8 files with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

*
* Example for index name is my_log_index and Monitor ID is TReewWdsf2gdJFV:
* { {
* "a": { "a": {
Copy link
Member

Choose a reason for hiding this comment

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

We do not want a to be appended with my_log_index_TReewWdsf2gdJFV?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't, since we transform mappings in the same way

while (it.hasNext()) {
val entry = it.next()
if (entry.value is Map<*, *>) {
transformDocumentFieldNames(entry.value as MutableMap<String, Any>, fieldNameSuffix)
Copy link
Collaborator

Choose a reason for hiding this comment

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

unchecked type cast happens here. please use @Suppress("UNCHECKED_CAST")

val entry = it.next()
if (entry.value is Map<*, *>) {
transformDocumentFieldNames(entry.value as MutableMap<String, Any>, fieldNameSuffix)
} else if (entry.key.endsWith(fieldNameSuffix) == false) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

you can also make it !entry.key.endsWith(fieldNameSuffix)

@sbcd90
Copy link
Collaborator

sbcd90 commented Apr 6, 2023

the pr looks good.

@lezzago lezzago merged commit 702da92 into opensearch-project:main Apr 6, 2023
@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.x 2.x
# Navigate to the new working tree
cd .worktrees/backport-2.x
# Create a new branch
git switch --create backport/backport-845-to-2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 702da92495481bebfc18dbee2de9544a4245759a
# Push it to GitHub
git push --set-upstream origin backport/backport-845-to-2.x
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.x

Then, create a pull request where the base branch is 2.x and the compare/head branch is backport/backport-845-to-2.x.

@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.3 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.3 2.3
# Navigate to the new working tree
cd .worktrees/backport-2.3
# Create a new branch
git switch --create backport/backport-845-to-2.3
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 702da92495481bebfc18dbee2de9544a4245759a
# Push it to GitHub
git push --set-upstream origin backport/backport-845-to-2.3
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.3

Then, create a pull request where the base branch is 2.3 and the compare/head branch is backport/backport-845-to-2.3.

@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.4 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.4 2.4
# Navigate to the new working tree
cd .worktrees/backport-2.4
# Create a new branch
git switch --create backport/backport-845-to-2.4
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 702da92495481bebfc18dbee2de9544a4245759a
# Push it to GitHub
git push --set-upstream origin backport/backport-845-to-2.4
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.4

Then, create a pull request where the base branch is 2.4 and the compare/head branch is backport/backport-845-to-2.4.

@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.5 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.5 2.5
# Navigate to the new working tree
cd .worktrees/backport-2.5
# Create a new branch
git switch --create backport/backport-845-to-2.5
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 702da92495481bebfc18dbee2de9544a4245759a
# Push it to GitHub
git push --set-upstream origin backport/backport-845-to-2.5
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.5

Then, create a pull request where the base branch is 2.5 and the compare/head branch is backport/backport-845-to-2.5.

@opensearch-trigger-bot
Copy link
Contributor

The backport to 2.6 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-2.6 2.6
# Navigate to the new working tree
cd .worktrees/backport-2.6
# Create a new branch
git switch --create backport/backport-845-to-2.6
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 702da92495481bebfc18dbee2de9544a4245759a
# Push it to GitHub
git push --set-upstream origin backport/backport-845-to-2.6
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-2.6

Then, create a pull request where the base branch is 2.6 and the compare/head branch is backport/backport-845-to-2.6.

petardz added a commit to petardz/alerting that referenced this pull request Apr 12, 2023
…r search (opensearch-project#845)

* Fixed transformation of document field names before running percolator search

Signed-off-by: Petar Dzepina <[email protected]>
(cherry picked from commit 702da92)
lezzago pushed a commit that referenced this pull request May 17, 2023
* Fixed transformation of document field names before running percolator search (#845)

* Fixed transformation of document field names before running percolator search

Signed-off-by: Petar Dzepina <[email protected]>
(cherry picked from commit 702da92)

* fixed module class names

Signed-off-by: Petar Dzepina <[email protected]>

---------

Signed-off-by: Petar Dzepina <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants