mongoid_search icon indicating copy to clipboard operation
mongoid_search copied to clipboard

MongoID Search not working

Open Loschcode opened this issue 9 years ago • 1 comments

It doesn't seem to work and as soon as I add a search_in :name to my models it shows this error

message:
  Invalid index specification on Product: _keywords, {:background=>true}
summary:
  Indexes in Mongoid are defined as a hash of field name and direction/2d pairs, with a hash for any additional options.
resolution:
  Ensure that the index conforms to the correct syntax and has the correct options.

 Valid options are:
   background: true|false
   database: 'database_name'
   drop_dups: true|false
   name: 'index_name'
   sparse: true|false
   unique: true|false
   min: 1
   max: 1
   bits: 26
   bucket_size : 1
   weights: { content: 1, title: 2 }
   expire_after_seconds: number_of_seconds
 Valid types are: 1, -1, '2d', '2dsphere', 'geoHaystack', 'text', 'hashed'

 Example:
   class Band
     include Mongoid::Document
     index({ name: 1, label: -1 }, { sparse: true })
     index({ location: '2d' }, { background: true })
   end

My model looks like so

class Product
  include MongoidBase
  include Mongoid::Search

  strip_attributes

  field :name,        type: String,   localize: true
  field :brand,       type: String,   localize: true
  field :cover,       type: String # deprecated ?
  field :desc,        type: String,   localize: true
  field :tags,        type: Array,    default: Array.new(Rails.configuration.max_num_tags)
  field :status,      type: Boolean,  default: true
  field :data,        type: String,   localize: true

  embeds_many :options,   inverse_of: :product,   cascade_callbacks: true,  class_name: 'VariantOption'
  embeds_many :skus,      inverse_of: :product,   cascade_callbacks: true

  has_and_belongs_to_many :collections,   inverse_of: :products
  has_and_belongs_to_many :categories,    inverse_of: :products

  has_many :order_items,  inverse_of: :product

  belongs_to :shop,           inverse_of: :products
  belongs_to :duty_category,  inverse_of: :products,  counter_cache: true

  accepts_nested_attributes_for :skus
  accepts_nested_attributes_for :options

  search_in :name

  mount_uploader :cover,   ProductImageUploader # deprecated ?

  scope :without_detail, -> { only(:_id, :name, :brand, :shop_id, :cover) }

  validates :name,        presence: true,   length: {maximum: (Rails.configuration.max_short_text_length * 1.25).round}
  validates :brand ,      presence: true,   length: {maximum: (Rails.configuration.max_short_text_length * 1.25).round}
  validates :shop,        presence: true
  validates :status,      presence: true

  validates :desc,        length: { maximum: (Rails.configuration.max_long_text_length * 1.25).round}
  validates :tags,        length: { maximum: Rails.configuration.max_num_tags }

  scope :has_tag,         ->(value) { where( :tags => value ) }
  scope :is_active,       ->        { where( :status => true ) }
  scope :has_sku,         ->()      { where( :skus.ne => [] ) }

  index({name: 1},          {unique: false, name: :idx_product_name})
  index({brand: 1},         {unique: false, name: :idx_product_brand})
  index({shop: 1},          {unique: false, name: :idx_product_shop})
  index({tags: 1},          {unique: false, name: :idx_product_tags,        sparse: true})
  index({users: 1},         {unique: false, name: :idx_product_users,       sparse: true})
  index({collections: 1},   {unique: false, name: :idx_product_collections, sparse: true})
  index({categories: 1},    {unique: false, name: :idx_product_categories,  sparse: true})
end

Is there a bug or am I doing something wrong ?

Loschcode avatar May 17 '16 09:05 Loschcode

same as me

fake-or-dead avatar Jan 08 '17 12:01 fake-or-dead