Sha256: 13aad2d5ef0230ce343fda4cdf65f31b510e9eea045d59188ba95f1ce0116733

Contents?: true

Size: 1.58 KB

Versions: 5

Compression:

Stored size: 1.58 KB

Contents

class Artist
  include Elasticsearch::Persistence::Model

  index_name [Rails.application.engine_name, Rails.env].join('-')

  analyzed_and_raw = { fields: {
    name: { type: 'text', analyzer: 'snowball' },
    raw:  { type: 'keyword' }
  } }

  attribute :name, String, mapping: analyzed_and_raw

  attribute :profile
  attribute :date, Date

  attribute :members, String, default: [], mapping: analyzed_and_raw
  attribute :members_combined, String, default: [], mapping: { analyzer: 'snowball' }

  attribute :urls, String, default: []
  attribute :album_count, Integer, default: 0

  attribute :suggest, Hashie::Mash, mapping: {
    type: 'object',
    properties: {
      name: {
        type: 'object',
        properties: {
          input:   { type: 'completion' },
          output:  { type: 'keyword', index: false },
          payload: { type: 'object', enabled: false }
        }
      },
      member: {
        type: 'object',
        properties: {
          input:   { type: 'completion' },
          output:  { type: 'keyword', index: false },
          payload: { type: 'object', enabled: false }
        }
      }
    }
  }

  validates :name, presence: true

  def albums
    Album.search(
      { query: {
          has_parent: {
            type: 'artist',
            query: {
              bool: {
                filter: {
                  ids: { values: [ self.id ] }
                }
              }
            }
          }
        },
        sort: 'released',
        size: 100
      },
      { type: 'album' }
  )
  end

  def to_param
    [id, name.parameterize].join('-')
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
elasticsearch-persistence-6.0.0.pre examples/music/artist.rb
elasticsearch-persistence-5.1.0 examples/music/artist.rb
elasticsearch-persistence-5.0.2 examples/music/artist.rb
elasticsearch-persistence-5.0.1 examples/music/artist.rb
elasticsearch-persistence-5.0.0 examples/music/artist.rb