Skip to content
This repository has been archived by the owner on Feb 22, 2023. It is now read-only.

Refactor search controller for consistency and clarity #778

Merged
merged 2 commits into from
Jul 5, 2022

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented Jun 29, 2022

This is a revision of PR #699 formed by reverting commit 7e009fd and then rebasing against main.

Original description

This PR refactors the search controller, splitting it up into smaller, easier to understand chunks with proper documentation and type hinting.

This PR

  • removes frivolous arguments to search, using the MediaSearchRequestSerializer as the single source of truth for search params
  • applies the excluded provider check only when there are non-zero excluded providers
  • updates the get_sources function to use DSL instead of raw query to ES
  • updates usage of Search to be idiomatic with Elasticsearch DSL conventions
  • moves globally accessible ES connection to settings.ES
  • reduces code duplication

Testing Instructions

This is a refactor, passing CI should be sufficient proof that no functionality is impacted.

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dhruvkb dhruvkb added 🟧 priority: high Stalls work on the project or its dependents ✨ goal: improvement Improvement to an existing user-facing feature 💻 aspect: code Concerns the software code in the repository labels Jun 29, 2022
@dhruvkb dhruvkb requested a review from a team as a code owner June 29, 2022 06:45
@dhruvkb dhruvkb requested review from krysal and sarayourfriend June 29, 2022 06:45
@github-actions
Copy link

github-actions bot commented Jun 29, 2022

API Developer Docs Preview: Ready

https://wordpress.github.io/openverse-api/_preview/778

Please note that GitHub pages takes a little time to deploy newly pushed code, if the links above don't work or you see old versions, wait 5 minutes and try again.

You can check the GitHub pages deployment action list to see the current status of the deployments.

Copy link
Contributor

@obulat obulat left a comment

Choose a reason for hiding this comment

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

Approving as we have already approved this PR before :) Really hope it works now that the API is throttled for unauthorized requests.

Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

As I was last time, I am still uncomfortable with the size and scope of this refactor. Even though I do agree it seems to make things much more uniform and consistent. I'm not sure that for me it makes anything clearer; perhaps for someone already familiar with the code it does though!

I left one suggestion that might help clarify at least a small part of the code. I think a lot of the code is pretty cryptic to begin with and I hope we can refactor/add tests/add comments to help with that in the future.

If there is absolutely any way to break up this refactor and to deploy it in steps, I think that would be a benefit, but I can appreciate that that is not always possible.

api/catalog/api/controllers/elasticsearch/search.py Outdated Show resolved Hide resolved

:param query_serializer: the ``MediaSearchRequestSerializer`` object with the query
:param index: The Elasticsearch index to search (e.g. 'image')
:param ip: the users' hashed IP to consistently route to the same ES shard
Copy link
Contributor

Choose a reason for hiding this comment

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

This is interesting, I didn't know we did this. I wonder if circumventing ES's built-in load balancing could be an issue for us.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know enough to comment if it can be a problem, but I can see how redirecting all requests from a particularly active consumer to a single shard can be a bottleneck. I haven't modified it in this PR though, it's been that way from before.

api/catalog/api/controllers/elasticsearch/search.py Outdated Show resolved Hide resolved
@dhruvkb
Copy link
Member Author

dhruvkb commented Jul 5, 2022

I'm going to merge this so that we can unblock #776. It's very huge and unwieldy as-is so it's better for any changes to this to be addressed in faster, smaller, easier-to-review PRs.

@dhruvkb dhruvkb merged commit d26df92 into main Jul 5, 2022
@dhruvkb dhruvkb deleted the search_ctrl_refac branch July 5, 2022 09:36
sarayourfriend added a commit that referenced this pull request Aug 5, 2022
AetherUnbound pushed a commit that referenced this pull request Aug 5, 2022
sarayourfriend added a commit that referenced this pull request Aug 5, 2022
* Revert "Fix related media endpoint (#808)"

This reverts commit d7962f1.

* Selectively revert "Add logging and fix all logging contexts (#790)"

This reverts commit ce33572.

* Revert "Refactor search controller for consistency and clarity (#778)"

Preserves #730

This reverts commit d26df92.

* Fix divergent Elasticsearch configurations

In #712 Elasticsearch configuration was moved out of the search controller area into the Django settings module. This unifies access to ES from across the application and is part of the refactoring being done to support the ES versioned index, multi-stage deployment. This commit keeps those changes and applies them to the old search_controller.py module that did not exist at the time the configuration change was applied.

* Lint

* Prevent circular import

We can replace the Any back with the serializer type in the future.

* Add back related media endpoint integration tests

* Lint
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
💻 aspect: code Concerns the software code in the repository ✨ goal: improvement Improvement to an existing user-facing feature 🟧 priority: high Stalls work on the project or its dependents
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants