-
Notifications
You must be signed in to change notification settings - Fork 212
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
Simplify related query to remove nesting and make more performant #3307
Conversation
b6ed3e7
to
a5bb1a5
Compare
f88b556
to
bc8934c
Compare
bc8934c
to
b1e2e52
Compare
Based on the high urgency of this PR, the following reviewers are being gently reminded to review this PR: @sarayourfriend Excluding weekend1 days, this PR was ready for review 3 day(s) ago. PRs labelled with high urgency are expected to be reviewed within 2 weekday(s)2. @obulat, if this PR is not ready for a review, please draft it to prevent reviewers from getting further unnecessary pings. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@obulat could you add some new unit tests, preferrably ones that run against the previous implementation and then still pass with the new one? The only test for related I can find is that it returns a 200 (https://github.com//WordPress/openverse/blob/bc7b2a7f6ee32fc3ca367baae7681abc41ef7f79/api/test/media_integration.py#L152-L155) but it doesn't confirm anything about the query actually doing what we want it to do (for example, by testing that the query result is the expected one).
As it stands, this might work fine (I haven't tested it locally yet, the code looks fine) but I'm uncomfortable approving it without any tests that would verify the behaviour of the query is the same (or at least, changing in an expected way).
Also: I'd like to have a deployment plan for this that includes setting up a dashboard to monitor the changes in response times for the related endpoint. I've saved a Logs Insights query in CloudWatch called "parsed requests" (something along those lines) that exposes an isRelated
flag, which we could use to create a query like this:
# ... saved query with parsing and creation if `isRelated` field
| filter isRelated
| stats avg(upstream_response_time) by bin(5m)
I've started this work for https://github.com/WordPress/openverse-infrastructure/issues/651 but likely won't be able to get to it until my Friday, but would seem to be a prerequisite to me for any significant changes we make to the related endpoint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed with @sarayourfriend - I like the code changes and they make sense, having tests for this would be useful and having a dashboard for monitoring production when deployed feels necessary. I appreciate how much more readable the search building code is with all the changes you've been making @obulat!
related_query["should"].append(Q("terms", tags__name=tags)) | ||
|
||
# Exclude the dynamically disabled sources. | ||
if excluded_providers_query := get_excluded_providers_query(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Love that we have this now!
Regarding the dashboard: I actually have a few minutes now I think I can have the basic thing up and we can iterate it after deployment if needed. The data will be there before/after because it's just derived from the logs anyway, so the dashboard doesn't need to be perfect at deployment time, just comprehensible enough that we can monitor the deployment and make sure this change doesn't somehow negatively effect the response times (which, to be clear, I don't think will be the case, just want to make sure we can move forward with confidence!). |
Update on the dashboard: I implemented it earlier today, and it's available at a link in the issue: https://github.com/WordPress/openverse-infrastructure/issues/651 With unit tests this will be good-to-go from my perspective 😁 |
Signed-off-by: Olga Bulat <[email protected]>
c5394b2
to
91ae7fe
Compare
Thank you so much for setting up the dashboard, @sarayourfriend! I've updated this PR so that the first commit is adding the test for I've added assertions for the ES query to check that the correct query is set. It is not very consistent with other tests, because instead of using I wanted to add a check that the results' title and tags have common words, but realized that it's not possible with this implementation since we use mocked results instead of the "related" results. I will see if I can use the integrated tests for this. |
Signed-off-by: Olga Bulat <[email protected]>
91ae7fe
to
6d91d59
Compare
Signed-off-by: Olga Bulat <[email protected]>
Signed-off-by: Olga Bulat <[email protected]>
6d91d59
to
568f754
Compare
Signed-off-by: Olga Bulat <[email protected]>
For what it's worth, I think that's fine, there are other ES tests that do this:
You're right, I think it's only at the integration test level that we can actually test this in a meaningful way. |
I think this example is a little bit different from what I did in the test here. In the linked example, there's only 1
|
Sure, but the example I showed just uses a regex to test the body of the request. There's no real difference, the end result is pook either compares the body as strings using Anyway, it's all good, both are perfectly fine approaches as far as I'm concerned and well suited for different testing needs. |
Signed-off-by: Olga Bulat <[email protected]>
Signed-off-by: Olga Bulat <[email protected]>
I added the check that there are at least 1 word in common in the related results with the main item (in the title and tags) in 8a5e3e4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Excited to see how/if the endpoint timings respond 🤞
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the new tests, I'm excited to try this out!
# Only use the first 10 tags | ||
if tags: | ||
tags = [tag["name"] for tag in tags[:10]] | ||
related_query["should"].append(Q("terms", tags__name=tags)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note for follow up @obulat:
related_query["should"].append(Q("terms", tags__name=tags)) | |
related_query["should"].append(Q("terms", tags__name__keyword=tags)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #3346
Just wanted to share an update. We deployed this an hour ago and the related image response times are dramaticly improved: Nice work @obulat! |
Fixes
Fixes #3306 by @obulat
Description
This PR refactors the way related query is created, using more low-level queries to make them less nested.
The main changes for this PR are within the
elasticsearch/related.py
file. All other changes are just moving the functions to a different file. Therelated
function is moved fromsearch_controller
module to a separate file withinelsticsearch
folder, and some other common functions for pagination toelasticsearch/helpers.py
file to make the files easier to read.Converting the simple search query to the terms query
After opening this PR, I also checked the queries logged by slowlog: all of the queries logged in the recent hours were related queries.
Using the
simple_query_string
for the list of tags is not performant. This PR converts the query to use the terms query for tags. It will mean that the tags will not match if the form is different (so,cat
will not matchcats
), but since we are trying to match all of the tags, I think we should still have enough matches.Testing Instructions
Go to the
/admin
endpoint and sethide content
for either Stocksnap or Flickr to true (in ContentModel).Add logging to the related endpoint (
s.to_dict()
to view the ES query), and check that the query sent to ES when the related endpoint is requested does not have the nesting described in the issue.Checklist
Update index.md
).main
) or a parent feature branch.Developer Certificate of Origin
Developer Certificate of Origin