<commit_before>require 'active_support'
require 'elasticsearch/model'
require 'elasticsearch/model/callbacks'

module Ladder::Searchable
  extend ActiveSupport::Concern

  included do
    include Elasticsearch::Model
    include Elasticsearch::Model::Callbacks
  end

  ##
  # Generate a qname-based JSON representation
  #
  def as_qname
    qname_hash = type.empty? ? {} : {rdf: {type: type.first.pname }}

    resource_class.properties.each do |field_name, property|
      ns, name = property.predicate.qname
      qname_hash[ns] ||= Hash.new

      if embedded_relations.keys.include? field_name
        qname_hash[ns][name] = self.send(field_name).map(&:as_qname)
      elsif fields.keys.include? field_name
        qname_hash[ns][name] = read_attribute(field_name)
      end
    end

    qname_hash.to_json
  end

  module ClassMethods

    ##
    # Specify type of serialization to use for indexing
    #
    def index(opts={})
      case opts[:as]
      when :jsonld
        define_method(:as_indexed_json) { |opts = {}| as_jsonld opts.except(:as) }
      when :qname
        define_method(:as_indexed_json) { |opts = {}| as_qname }
      else
        define_method(:as_indexed_json) { |opts = {}| as_document.as_json(except: ['id', '_id']) }
      end
    end

  end
    
end<commit_msg>Fix relations in #as_qname, default #index method<commit_after>require 'active_support'
require 'elasticsearch/model'
require 'elasticsearch/model/callbacks'

module Ladder::Searchable
  extend ActiveSupport::Concern

  included do
    include Elasticsearch::Model
    include Elasticsearch::Model::Callbacks
  end

  ##
  # Generate a qname-based JSON representation
  #
  def as_qname
    qname_hash = type.empty? ? {} : {rdf: {type: type.first.pname }}

    resource_class.properties.each do |field_name, property|
      ns, name = property.predicate.qname
      qname_hash[ns] ||= Hash.new

      object = self.send(field_name)

      if relations.keys.include? field_name
        qname_hash[ns][name] = object.to_a.map { |obj| "#{obj.class.name.underscore.pluralize}:#{obj.id}" }
      elsif fields.keys.include? field_name
        qname_hash[ns][name] = read_attribute(field_name)
      end
    end

    qname_hash.to_json
  end

  module ClassMethods

    ##
    # Specify type of serialization to use for indexing
    #
    def index(opts={})
      case opts[:as]
      when :jsonld
        define_method(:as_indexed_json) { |opts = {}| as_jsonld opts.except(:as) }
      when :qname
        define_method(:as_indexed_json) { |opts = {}| as_qname }
      else
        define_method(:as_indexed_json) { |opts = {}| as_json(except: ['id', '_id']) }
      end
    end

  end
    
end
{'fraction_non_alphanumeric': 0.11326378539493294, 'fraction_numerical': 0.0, 'mean_word_length': 3.1307692307692307, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '888597', 'n_tokens_mistral': 894, 'n_tokens_neox': 879, 'n_words': 183}