Skip to content

Commit

Permalink
Use long for downloads count in OpenSearch index. (#5052)
Browse files Browse the repository at this point in the history
* Use long for downloads count in OpenSearch index.

* Scale the suggestion weight using log

---------

Co-authored-by: Colby Swandale <996377+colby-swandale@users.noreply.github.com>
  • Loading branch information
simi and colby-swandale committed Sep 24, 2024
1 parent bf3fa63 commit 2c9a573
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
8 changes: 6 additions & 2 deletions app/models/concerns/rubygem_searchable.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module RubygemSearchable
description: { type: "text", analyzer: "english", fields: { raw: { analyzer: "simple", type: "text" } } }, description: { type: "text", analyzer: "english", fields: { raw: { analyzer: "simple", type: "text" } } },
suggest: { type: "completion", contexts: { name: "yanked", type: "category" } }, suggest: { type: "completion", contexts: { name: "yanked", type: "category" } },
yanked: { type: "boolean" }, yanked: { type: "boolean" },
downloads: { type: "integer" }, downloads: { type: "long" },
updated: { type: "date" } updated: { type: "date" }
} }
} }
Expand Down Expand Up @@ -88,12 +88,16 @@ def suggest_json
{ {
suggest: { suggest: {
input: name, input: name,
weight: downloads, weight: suggest_weight_scale(downloads),
contexts: { contexts: {
yanked: versions.none?(&:indexed?) yanked: versions.none?(&:indexed?)
} }
} }
} }
end end

def suggest_weight_scale(downloads)
Math.log10(downloads + 1).to_i
end
end end
end end
42 changes: 42 additions & 0 deletions test/models/concerns/rubygem_searchable_test.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -79,6 +79,48 @@ class RubygemSearchableTest < ActiveSupport::TestCase
assert_equal v, json[k], "value doesn't match for key: #{k}" assert_equal v, json[k], "value doesn't match for key: #{k}"
end end
end end

should "set the suggest json" do
json = @rubygem.search_data

assert_equal "example_gem", json[:suggest][:input]
end

should "calculate the suggestion weight based on the number of downloads" do
weights = [
[0, 0],
[10, 1],
[100, 2],
[1_000, 3],
[10_000, 4],
[100_000, 5],
[1_000_000, 6],
[10_000_000, 7],
[100_000_000, 8],
[1_000_000_000, 9]
]

weights.each do |downloads, weight|
@rubygem.gem_download.update(count: downloads)
json = @rubygem.search_data

assert_equal weight, json[:suggest][:weight]
end
end

context "when the number of downloads exceeds a 32 bit integer" do
setup do
@rubygem = create(:rubygem, name: "large_downloads_example_gem", downloads: 10_000_000_000) # 10 Billion downloads
@version = create(:version, number: "1.0.0", rubygem: @rubygem)
import_and_refresh
end

should "allow the number of downloads to be stored as a 64 bit integer" do
json = @rubygem.search_data

assert_equal 10_000_000_000, json[:downloads]
end
end
end end


context "rubygems analyzer" do context "rubygems analyzer" do
Expand Down

0 comments on commit 2c9a573

Please sign in to comment.