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 #699

Merged
merged 29 commits into from
May 25, 2022
Merged

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented May 16, 2022

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

These refactor PRs I've been making recently is to make the process of adding media types (like 3D models and video) smoother and more intuitive. It also helps to understand what the code does when you can confidently modify it.

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.

@openverse-bot openverse-bot added the 🚦 status: awaiting triage Has not been triaged & therefore, not ready for work label May 16, 2022
@github-actions
Copy link

github-actions bot commented May 16, 2022

API Developer Docs Preview: Ready

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

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.

@dhruvkb dhruvkb added 🟨 priority: medium Not blocking but should be addressed soon ✨ goal: improvement Improvement to an existing user-facing feature and removed 🚦 status: awaiting triage Has not been triaged & therefore, not ready for work labels May 16, 2022
) -> List[Hit]:
"""
After fetching the search results from the back end, iterate through the
results, perform image validation, and route certain thumbnails through our
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 not really in the scope of this PR, but where exactly is this done: route certain thumbnails thumbnails through our proxy in _post_process_results function?

Copy link
Member Author

Choose a reason for hiding this comment

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

This code is in api/utils/validate_images.py. It's not exactly what the comment describes though, it checks for broken images but it does not route any images through our proxy. I think we used to do that before for http:// images but I think we stopped doing sometime and didn't update the comment.

for arg in search_params[serializer_field].split(","):
filters.append(Q("term", **{es_field: arg}))
method = getattr(s, behaviour) # can be ``s.filter`` or ``s.exclude``
return method("bool", should=filters)
Copy link
Contributor

Choose a reason for hiding this comment

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

From my limited understanding of ES, I think we can replace the should with must here, because we don't need the results that do not contain the filter parameter (i.e., when we are filtering by some value, we don't need the score that should can bring).
This is not a suggestion for this PR, I'm just putting my thoughts in words here :)

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 thought so too, but I don't know enough ES to be confident enough to make the change. I'll update it if you're sure that it should be must instead of should.

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.

I really love how much clearer the code is now. It's great to have ES global with the Elasticsearch connection, too!

Copy link
Contributor

@AetherUnbound AetherUnbound left a comment

Choose a reason for hiding this comment

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

This is awesome! I have a few comments/potential improvements, but this is good to go as-is IMO.

api/catalog/api/controllers/elasticsearch/related.py Outdated Show resolved Hide resolved
api/catalog/api/controllers/elasticsearch/related.py Outdated Show resolved Hide resolved
api/catalog/api/controllers/elasticsearch/related.py Outdated Show resolved Hide resolved
api/catalog/api/controllers/elasticsearch/utils.py Outdated Show resolved Hide resolved
@dhruvkb dhruvkb merged commit 0e442a4 into main May 25, 2022
@dhruvkb dhruvkb deleted the es_rand branch May 25, 2022 15:07
sarayourfriend added a commit that referenced this pull request Jun 1, 2022
sarayourfriend added a commit that referenced this pull request Jun 1, 2022
Revert "Refactor search controller for consistency and clarity (#699)"

This reverts commit 0e442a4.
dhruvkb added a commit that referenced this pull request Jun 8, 2022
@AetherUnbound AetherUnbound mentioned this pull request Jun 14, 2022
1 task
@zackkrida
Copy link
Member

@WordPress/openverse can we explore re-merging this work and getting it in an upcoming API deploy? I'd hate to 1. lose this work or 2. have the API drift so far from the changes here that it becomes difficult or impossible to merge.

@AetherUnbound
Copy link
Contributor

Fully support that post-v2.5.6!

@dhruvkb dhruvkb restored the es_rand branch June 29, 2022 06:17
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: medium Not blocking but should be addressed soon
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants