Skip to content

Add search result ordering by download count #1182

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tests/unit/cli/search/test_reindex.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def test_project_docs(db_session):
"name": p.name,
"normalized_name": p.normalized_name,
"version": [r.version for r in prs],
"downloads": p.downloads,
},
}
for p, prs in sorted(releases.items(), key=lambda x: x[0].name.lower())
Expand Down
1 change: 1 addition & 0 deletions tests/unit/packaging/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def test_build_search():
pretend.stub(version="3.0"),
pretend.stub(version="4.0"),
],
downloads=9001,
),
summary="This is my summary",
description="This is my description",
Expand Down
11 changes: 11 additions & 0 deletions warehouse/packaging/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import enum

from collections import OrderedDict
from datetime import datetime

from citext import CIText
from pyramid.security import Allow
Expand Down Expand Up @@ -153,6 +154,12 @@ def documentation_url(self):

return request.route_url("legacy.docs", project=self.name)

@property
def downloads(self):
release, *_ = self.releases
delta = (datetime.now() - release.created).days or 1
return release.downloads / delta


class DependencyKind(enum.IntEnum):

Expand Down Expand Up @@ -339,6 +346,10 @@ def urls(self):
def has_meta(self):
return any([self.keywords])

@property
def downloads(self):
return sum([file_.downloads for file_ in self.files])


class File(db.Model):

Expand Down
6 changes: 5 additions & 1 deletion warehouse/packaging/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from elasticsearch_dsl import DocType, String, analyzer, MetaField, Date
from elasticsearch_dsl import (
DocType, String, analyzer, MetaField, Date, Float
)

from warehouse.search import doc_type

Expand Down Expand Up @@ -47,6 +49,7 @@ class Project(DocType):
platform = String(index="not_analyzed")
created = Date()
classifiers = String(index="not_analyzed", multi=True)
downloads = Float()

class Meta:
# disable the _all field to save some space
Expand All @@ -70,5 +73,6 @@ def from_db(cls, release):
obj["platform"] = release.platform
obj["created"] = release.created
obj["classifiers"] = list(release.classifiers)
obj["downloads"] = release.project.downloads

return obj
1 change: 1 addition & 0 deletions warehouse/templates/search/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ <h2 class="no-top-padding">Filter Projects</h2>
<select class="-js-form-submit-trigger" name="o">
{{ search_option("Relevance", "") }}
{{ search_option("Date Last Updated", "-created") }}
{{ search_option("Downloads", "-downloads", "downloads") }}
</select>
</div>
</section>
Expand Down