<commit_before>module ElasticRecord
  module AsDocument
    def as_search_document(mapping_properties = elastic_index.mapping[:properties])
      mapping_properties.each_with_object({}) do |(field, mapping), result|
        value = value_for_elastic_search field, mapping, mapping_properties

        unless value.nil?
          result[field] = value
        end
      end
    end

    def as_partial_update_document(mapping_properties = elastic_index.mapping[:properties])
      changed_fields = respond_to?(:saved_changes) ? saved_changes.keys : changed

      changed_fields.each_with_object({}) do |field, result|
        if field_mapping = mapping_properties[field]
          result[field] = value_for_elastic_search(field, field_mapping, mapping_properties, partial: true)
        end
      end
    end

    def value_for_elastic_search(field, mapping, mapping_properties, partial: false)
      return if (value = try(field)).nil?

      case mapping[:type]&.to_sym
      when :object
        object_mapping_properties = mapping_properties.dig(field, :properties)
        value_for_elastic_search_object(value, object_mapping_properties, partial: partial)
      when :nested
        return nil if value.empty?

        object_mapping_properties = mapping_properties.dig(field, :properties)
        value.map { |entry| value_for_elastic_search_object(entry, object_mapping_properties) }
      when :integer_range, :float_range, :long_range, :double_range, :date_range
        value_for_elastic_search_range(value)
      else
        value if value.present? || value == false
      end
    end

    def value_for_elastic_search_object(object, nested_mapping, partial: false)
      method = partial ? :as_partial_update_document : :as_search_document
      object.respond_to?(method) ? object.public_send(method, nested_mapping) : object
    end

    def value_for_elastic_search_range(range)
      gte = range.begin unless range.begin == -Float::INFINITY
      lte = range.end unless range.end == Float::INFINITY

      {'gte' => gte, 'lte' => lte}
    end
  end
end<commit_msg>Remove partial updates for inner objects. Too unreliable (#107)<commit_after>module ElasticRecord
  module AsDocument
    def as_search_document(mapping_properties = elastic_index.mapping[:properties])
      mapping_properties.each_with_object({}) do |(field, mapping), result|
        value = value_for_elastic_search field, mapping, mapping_properties

        unless value.nil?
          result[field] = value
        end
      end
    end

    def as_partial_update_document(mapping_properties = elastic_index.mapping[:properties])
      changed_fields = respond_to?(:saved_changes) ? saved_changes.keys : changed

      changed_fields.each_with_object({}) do |field, result|
        if field_mapping = mapping_properties[field]
          result[field] = value_for_elastic_search(field, field_mapping, mapping_properties)
        end
      end
    end

    def value_for_elastic_search(field, mapping, mapping_properties)
      return if (value = try(field)).nil?

      case mapping[:type]&.to_sym
      when :object
        object_mapping_properties = mapping_properties.dig(field, :properties)
        value_for_elastic_search_object(value, object_mapping_properties)
      when :nested
        return if value.empty?

        object_mapping_properties = mapping_properties.dig(field, :properties)
        value.map { |entry| value_for_elastic_search_object(entry, object_mapping_properties) }
      when :integer_range, :float_range, :long_range, :double_range, :date_range
        value_for_elastic_search_range(value)
      else
        value if value.present? || value == false
      end
    end

    def value_for_elastic_search_object(object, nested_mapping)
      object.respond_to?(:as_search_document) ? object.as_search_document(nested_mapping) : object
    end

    def value_for_elastic_search_range(range)
      gte = range.begin unless range.begin == -Float::INFINITY
      lte = range.end unless range.end == Float::INFINITY

      {'gte' => gte, 'lte' => lte}
    end
  end
end
{'fraction_non_alphanumeric': 0.08402743753062224, 'fraction_numerical': 0.0007349338559529642, 'mean_word_length': 3.645051194539249, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '501985', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1152, 'n_words': 251}