Sha256: ff35d487e88fd0fcd36a4e0e1f3fcd79e8658525ce403c74ff0f8a7b2c26c465

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

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 if queries.empty?
          field_queries = queries.join(' OR ')
          "(#{field_queries}) NOT europeana_id:\"#{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

2 entries across 2 versions & 1 rubygems

Version Path
europeana-blacklight-1.3.1 app/models/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-1.3.0 app/models/europeana/blacklight/document/more_like_this.rb