define_type ::Car.active.with_online_dealer.includes(::Marketplace::Car.default_include) do field :id, type: 'integer' field :type, value: ->(c) { 'car' } field :number, type: 'long', value: ->(c) { c.random_bigint } field :cache_key, type: 'string' field :url, type: 'string', value: ->(c) { UrlService.url_for_car c } field :created_at, type: 'date', value: ->(c) { c.created_at.to_date.to_s :db } field :tia, type: 'date', value: ->(c) { c.tia ? c.tia.to_s(:db) : nil } field :name, type: 'string', value: ->(c) { c.headline(show_original: true) } field :text_variant, type: 'string', analyzer: 'car_variant' field :mileage, type: 'integer' field :seats, type: 'integer' field :ancestors, type: 'integer', value: ->(c) { c.ancestor || 0 } field :doors, type: 'integer', value: ->(c) { door_enum = ::Doors.include? c.doors case door_enum when ::Doors::NoPreference, ::Doors::AllPossible, nil (1..7).to_a else door_enum.door_range.to_a end } field :initial_registration, value: ->(c) { c.initial_registration } do field :months, type: 'integer', value: ->(ir) { ir ? ir.year * 12 + ir.month : nil } field :year, type: 'integer', value: ->(ir) { ir ? ir.year : nil } field :date, type: 'date', value: ->(ir) { ir.to_s :db } end [:brand, :model, :bodytype, :agetype, :fueltype, :geartype].each do |f| field f do field :id, type: 'integer' field :name, type: 'string' field :id_and_name, index: 'not_analyzed', type: 'string', value: ->(obj) { [obj.id, obj.name].join('|') } end end field :owner do field :id, type: 'integer' field :name, type: 'string' field :type, type: 'string', value: ->(o) { o.dealer? ? 'dealer' : 'customer' } end field :location, type: 'object', value: ->(c) { c } do field :city, type: 'string', analyzer: 'city', value: ->(c) { c.owner ? c.owner.city : nil } field :zip, type: 'string', value: ->(c) { c.zipcode ? c.zipcode.zip : nil } field :coordinates, type: 'geo_point', value: ->(c) { c.owner ? c.owner.coordinates.to_a : [] } end field :color, value: ->(c) { c } do field :exterior, value: ->(c) { c } do field :id, type: 'integer', value: ->(c) { c.color_exterieur ? c.color_exterieur.id : nil } field :name, type: 'string', value: ->(c) { c.color_exterieur ? c.color_exterieur.name : nil } field :origin, type: 'string', value: ->(c) { c.color_original } field :id_and_name, index: 'not_analyzed', type: 'string', value: ->(c) { id = c.color_exterieur ? c.color_exterieur.id : nil name = c.color_exterieur ? c.color_exterieur.name : nil [id, name].join('|') } end field :interior, value: ->(c) { c } do field :id, type: 'integer', value: ->(c) { c.color_interieur ? c.color_interieur.id : nil } field :name, type: 'string', value: ->(c) { c.color_interieur ? c.color_interieur.name : nil } end end field :extras, value: ->(c) { c.extras.map{|name| extra = ::Extras[name] {id: extra.bitfield_index, name: extra.to_s, name_breakdown: extra.name_breakdown} } } do field :name_breakdown, type: 'string' field :id, type: 'integer' field :name, type: 'string' end field :price, value: ->(c) { c } do field :initial_price, type: 'float', value: ->(c) { c.price_customer_first } field :customer, type: 'integer', value: ->(c) { c.price_customer } field :predicted, type: 'float', value: ->(c) { c.predicted_price ? c.predicted_price.price : 0 } field :overprice_amount, type: 'float', value: ->(c) { c.predicted_price ? c.predicted_price.overprice_amount : c.price_customer } field :prediction_class, type: 'integer', value: ->(c) { c.predicted_price ? c.predicted_price.advantage_class : PredictedPrice::NO_PREDICTION } end field :power, value: ->(c) { c } do field :kw_raw, type: 'integer', value: ->(c) { c.kw_raw } field :kw, type: 'integer', value: ->(c) { c.kw_raw ? c.kw : nil } field :hp, type: 'integer', value: ->(c) { c.kw_raw ? c.hp : nil } end field :emission, value: ->(c) { c } do field :co2_emission, type: 'float', value: ->(c) { c.co2_emission } field :consumption, value: ->(c) { c } do field :intown, type: 'float', value: ->(c) { c.consumption_intown } field :outtown, type: 'float', value: ->(c) { c.consumption_outtown } field :average, type: 'float', value: ->(c) { c.consumption_average } end end field :main_image, value: ->(c) { c.main_image } do field :id, type: 'integer', value: ->(main_image) { main_image.id } field :seo_url_part, type: 'string', value: ->(main_image) { main_image.seo_url_part } end field :co2_emission_class, value: ->(c) { c.co2_emission_class } do field :name, type: 'string', value: ->(n) { ::Co2EmissionClasses.for_class_number(n).to_s } field :class_number, type: 'integer', value: ->(n) { n } end field :parked_by, type: 'integer', value: ->(c) { c.user_ids }