cirandas.net

ref: master

plugins/solr/lib/ext/article.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
require_dependency 'article'

ActiveSupport.run_load_hooks :solr_article

class Article

  SEARCH_FILTERS[:order] = []

  # use for internationalizable human type names in search facets
  # reimplement on subclasses
  def self.type_name
    c_('Content')
  end

  def solr_comments_updated
    solr_save
  end

  def add_category_with_solr_save(c, reload=false)
    add_category_without_solr_save(c, reload)
    if !new_record?
      self.solr_save
    end
  end
  alias_method_chain :add_category, :solr_save

  def create_pending_categorizations_with_solr_save
    create_pending_categorizations_without_solr_save
    self.solr_save
  end
  alias_method_chain :create_pending_categorizations, :solr_save

  protected

  def self.solr_f_type_proc(facet, id_count_arr)
    id_count_arr.map do |type, count|
      [type, type.constantize.type_name, count]
    end
  end

  def self.solr_f_profile_type_proc facet, id_count_arr
    id_count_arr.map do |type, count|
      [type, type.constantize.type_name, count]
    end
  end

  def solr_f_type
    self.class.name
  end

  def solr_f_profile_type
    self.profile.class.name
  end

  def solr_f_published_at
    self.published_at
  end

  def solr_f_category
    self.categories.collect(&:name)
  end

  def solr_is_public
    self.public?
  end

  def solr_category_filter
    categories_including_virtual_ids
  end

  def solr_name_sortable
    name
  end

  extend SolrPlugin::ActsAsSearchable
  extend SolrPlugin::ActsAsFaceted

  acts_as_faceted fields: {
      solr_f_type: {label: c_('Type'), proc: method(:solr_f_type_proc).to_proc},
      solr_f_published_at: {type: :date, label: _('Published date'), queries: {'[* TO NOW-1YEARS/DAY]' => _("Older than one year"),
        '[NOW-1YEARS TO NOW/DAY]' => _("In the last year"), '[NOW-1MONTHS TO NOW/DAY]' => _("In the last month"), '[NOW-7DAYS TO NOW/DAY]' => _("In the last week"), '[NOW-1DAYS TO NOW/DAY]' => _("In the last day")},
        queries_order: ['[NOW-1DAYS TO NOW/DAY]', '[NOW-7DAYS TO NOW/DAY]', '[NOW-1MONTHS TO NOW/DAY]', '[NOW-1YEARS TO NOW/DAY]', '[* TO NOW-1YEARS/DAY]']},
      solr_f_profile_type: {label: c_('Profile'), proc: method(:solr_f_profile_type_proc).to_proc},
      solr_f_category: {label: c_('Categories')},
    }, category_query: -> (c) { "solr_category_filter:\"#{c.id}\"" },
    order: [:solr_f_type, :solr_f_published_at, :solr_f_profile_type, :solr_f_category]

  acts_as_searchable fields: [
      # searched fields
      {name: {type: :text, boost: 2.0}},
      {slug: :text}, {body: :text},
      {abstract: :text}, {filename: :text},
      # filtered fields
      {solr_is_public: :boolean}, {published: :boolean},
      {environment_id: :integer},
      {profile_id: :integer}, :language,
      {solr_category_filter: :integer},
      # ordered/query-boosted fields
      {lat: :float}, {lng: :float},
      {solr_name_sortable: :string}, :last_changed_by_id, :published_at, :is_image,
      :updated_at, :created_at,
    ],
    include: [
      {profile: {fields: [:name, :identifier, :address, :nickname, :region_id, :lat, :lng]}},
      {comments: {fields: [:title, :body, :author_name, :author_email]}},
      {categories: {fields: [:name, :path, :slug, :lat, :lng, :acronym, :abbreviation]}},
    ], facets: self.solr_facets_options,
    boost: -> (a) { 10 if a.profile && a.profile.enabled },
    if: -> (a) { not a.class.name.in? ['RssFeed'] }

end