Sha256: 4691770097ecfc2b5339b1012be25032676b451b54a94555c2fb79341eb5e923

Contents?: true

Size: 1.89 KB

Versions: 7

Compression:

Stored size: 1.89 KB

Contents

module Europeana
  module Blacklight
    class Document
      ##
      # Methods for obtaining "more like this" documents
      module MoreLikeThis
        # @param [String] param Name of API parameter to restrict query to
        # @return [String]
        def more_like_this_query(param = nil)
          queries = more_like_this_field_queries(param)
          return nil unless queries.size > 0
          field_queries = queries.join(' OR ')
          "(#{field_queries}) NOT europeana_id:\"#{self.id}\""
        end

        protected

        def more_like_this_logic
          [
            { param: 'what', fields: ['proxies.dcType', 'proxies.dcSubject'], boost: 0.8 },
            { param: 'who', fields: 'proxies.dcCreator', boost: 0.5 },
            { param: 'title', fields: 'title', boost: 0.3 },
            { param: 'DATA_PROVIDER', fields: 'aggregations.edmDataProvider', boost: 0.2 }
          ]
        end

        def more_like_this_field_terms(*fields)
          fields.flatten.map do |field|
            fetch(field, []).compact[0..9]
          end.flatten
        end

        def more_like_this_field_queries(param = nil)
          logic = more_like_this_logic.select do |component|
            param.nil? || component[:param] == param
          end
          logic.map do |component|
            field_terms = more_like_this_field_terms(component[:fields])
            more_like_this_param_query(component[:param], field_terms, component[:boost])
          end.compact
        end

        def more_like_this_param_query(param, terms, boost)
          return nil unless terms.present?

          string_terms = terms.select { |t| t.is_a?(String) }
          return nil unless string_terms.present?

          or_terms = string_terms.map do |v|
            '"' + Europeana::API.record.escape(v) + '"'
          end.join(' OR ')
          "#{param}: (#{or_terms})^#{boost}"
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
europeana-blacklight-1.2.2 app/models/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-1.2.1 app/models/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-1.2.0 app/models/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-1.1.0 app/models/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-1.0.0 app/models/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-0.5.2 app/models/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-0.5.1 app/models/europeana/blacklight/document/more_like_this.rb