Получите мою группу Варианты продукта по цвету сasticsearch - PullRequest
0 голосов
/ 04 февраля 2020

Я получил следующую схему Variant

 create_table "spree_variants", force: :cascade do |t|
   t.string   "sku",                                        default: "",    null: false
   t.decimal  "weight",            precision: 8,  scale: 2, default: 0.0
   t.decimal  "height",            precision: 8,  scale: 2
   t.decimal  "width",             precision: 8,  scale: 2
   t.decimal  "depth",             precision: 8,  scale: 2
   t.datetime "deleted_at"
   t.boolean  "is_master",                                  default: false
   t.integer  "product_id"
   t.decimal  "cost_price",        precision: 10, scale: 2
   t.integer  "position"
   t.string   "cost_currency"
   t.boolean  "track_inventory",                            default: true
   t.integer  "tax_category_id"
   t.datetime "updated_at"
   t.integer  "stock_items_count",                          default: 0,     null: false
   t.boolean  "ready_to_post"
 end

Моя модель Variant

 searchkick unless respond_to?(:searchkick_index)
  def search_data
    {
      name: name,
      price: price,
      created_at: created_at,
      posted_at: posted_at,
      updated_at: updated_at,
      discount_percentage: discount_percentage,
      ready_to_post: ready_to_post,
      taxon_names: taxon_names,
      brand: brand,
      sport: sport,
      business: business,
      gender: gender,
      kind: kind,
      size: size,
      color: color,
      landing: landing,
      count_on_hand: count_on_hand
    }
  end
  delegate :created_at, :posted_at, :discount_percentage,
           :ready_to_post, :taxon_names, :brand, :sport, :business, :gender,
           :kind, :landing, :updated_at, to: :product
  def count_on_hand
    if self.stock_items.first
      self.stock_items.first.count_on_hand
   else
     0
   end
 end
 def size
   self.option_values.detect { |o| o.option_type.name == 'SIZE' }&.id
 end
 def color
   self.option_values.detect { |o| o.option_type.name == 'COLOR' }&.id
 end

Я пытаюсь получить следующий результат

было бы привести все варианты и группы по цвету, независимо от размера. выберите этот метод, чтобы фильтры по размеру приносили только варианты с наличием на складе

мой базовый запрос выглядит примерно так

query = {ready_to_post: true, count_on_hand: {gt : 0}}

И мой последний поиск

Spree::Variant.search("#{keywords}", :where => query, order: sort_by, page: params[:page], per_page: ENV['ES_PER_PAGE'].to_i, aggs: main_aggs)
...