Sha256: a86757543455d3d21da362aaf88860d64b79c3e052ce69085827b5481940c22b

Contents?: true

Size: 1.77 KB

Versions: 6

Compression:

Stored size: 1.77 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
          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?
          or_terms = terms.map do |v|
            '"' + Europeana::API::Search.escape(v) + '"'
          end.join(' OR ')
          "#{param}: (#{or_terms})^#{boost}"
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
europeana-blacklight-0.3.2 lib/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-0.3.1 lib/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-0.3.0 lib/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-0.2.7 lib/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-0.2.6 lib/europeana/blacklight/document/more_like_this.rb
europeana-blacklight-0.2.5 lib/europeana/blacklight/document/more_like_this.rb