Skip to content

Instantly share code, notes, and snippets.

@gmassanek
Last active June 5, 2017 21:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmassanek/4ae00a5e6629fc983369234ddb50e4a1 to your computer and use it in GitHub Desktop.
Save gmassanek/4ae00a5e6629fc983369234ddb50e4a1 to your computer and use it in GitHub Desktop.
MLT vs OR query against prod
def converted_prices_for(price)
Currency.supported_for_selling.each_with_object({}) do |listing_currency, all_prices|
exchanged_amount = Reverb::ExchangeBanks
.exchange_with_inverse_paypal_rates(money: price, to_currency: listing_currency)
.cents
all_prices[listing_currency] = exchanged_amount
end
end
def mtl_query(product)
ReverbSearchV2::Listings
.paginate(page: 1, per_page: 24)
.filter_status(ListingState::LIVE)
.filter_category(product.product_type.root_category_uuid)
.filter_min_price(converted_prices_for(Monetize.parse(product.price.to_f * 0.5)))
.filter_max_price(converted_prices_for(Monetize.parse(product.price.to_f * 1.5)))
.filter_ships_to_and_local_pickup(shipping_region_codes: ["CON_US", "XX"])
.similar_to(
product.id,
minimum_should_match: '30%',
max_query_terms: 20
)
.filter_min_year(product.year_low - 3)
.filter_max_year(product.year_high - 3)
.boost_category(product.canonical_taxonomy_categories.map(&:uuid))
.boost_brand(product.brand.try(:uuid))
end
def or_query(product)
query = [
product.title,
product.year,
product.canonical_taxonomy_categories.map(&:slug).join(" "),
product.finish,
product.make,
product.model
].join(" ")
ReverbSearchV2::Listings
.paginate(page: 1, per_page: 24)
.filter_status(ListingState::LIVE)
.filter_category(product.product_type.root_category_uuid)
.filter_min_price(converted_prices_for(Monetize.parse(product.price.to_f * 0.5)))
.filter_max_price(converted_prices_for(Monetize.parse(product.price.to_f * 1.5)))
.filter_ships_to_and_local_pickup(shipping_region_codes: ["CON_US", "XX"])
.full_text_query(query, locale: 'en')
.full_text_query_operator(:OR, minimum_should_match: '30%')
.filter_min_year(product.year_low - 3)
.filter_max_year(product.year_high - 3)
.boost_category(product.canonical_taxonomy_categories.map(&:uuid))
.boost_brand(product.brand.try(:uuid))
end
roduct = Product.find(5520652)
puts "USING MLT"
Benchmark.measure do
100.times { mtl_query(product).search }
end
puts "USING OR QUERY"
Benchmark.measure do
100.times { or_query(product).search }
end
USING MLT
=> #<Benchmark::Tms:0x005566c68eb868 @label="", @real=21.44712279400119, @cstime=0.0, @cutime=0.0, @stime=0.020000000000000018, @utime=0.2900000000000009, @total=0.31000000000000094>
USING OR QUERY
=> #<Benchmark::Tms:0x005566bf0b0380 @label="", @real=27.092301193999447, @cstime=0.0, @cutime=0.0, @stime=0.020000000000000018, @utime=0.4399999999999995, @total=0.4599999999999995>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment