【问题标题】:Active Storage: Query on Metadata column活动存储:元数据列查询
【发布时间】:2019-07-09 13:27:19
【问题描述】:

我在元数据中添加了“纬度、经度、geo_location_name、image_taken_time”属性。因此,当我分析它时,它将添加此属性以及高度和宽度。

在我的项目中,我想获取那些花费特定年份时间的记录。

require 'exifr/jpeg'
require 'geocoder'

module ActiveStorage
  class Analyzer::ImageAnalyzer < Analyzer
    def metadata
      read_image do |image|
        if rotated_image?(image)
          { width: image.height, height: image.width }
        else
          { width: image.width, height: image.height }
        end.merge(gps_from_exif(image) || {})
      end
    rescue LoadError
      logger.info "Skipping image analysis because the mini_magick gem isn't installed"
      {}
    end

    private

    def gps_from_exif(image)
      return unless image.type == 'JPEG'
      if exif = EXIFR::JPEG.new(image.path).exif
        if gps = exif.fields[:gps]
          result = {
            latitude:  gps.fields[:gps_latitude].to_f,
            longitude: gps.fields[:gps_longitude].to_f,
            altitude:  gps.fields[:gps_altitude].to_f
          }
          result[:image_taken_time] =  exif.date_time_original
          query        = "#{result[:latitude]},#{result[:longitude]}"
          geo_location = Geocoder.search(query).first
          result[:geo_location_name]       = geo_location.address if geo_location.present?
          return result
        end
      end
    rescue EXIFR::MalformedImage, EXIFR::MalformedJPEG
    end
  end
end

如何查询blob的元数据字段(文本数据类型)?

提前致谢

【问题讨论】:

    标签: mysql ruby-on-rails rails-activestorage


    【解决方案1】:

    几年后,我只是为自己做了以下事情。只需加入正确的表并在metadata 上执行序列化哈希查询:

    作用于特定记录 record.your_attachment.joins(:blob).where('active_storage_blobs.metadata LIKE ?', '%"your_attribute":"your_value"%')

    适用于所有附件 ActiveStorage::Attachment.joins(:blob).where('active_storage_blobs.metadata LIKE ?', '%"your_attribute":"your_value"%')

    如果您需要多个条件,则应添加更多 where 语句。

    【讨论】:

      【解决方案2】:

      如果您想查询元数据,您可以在活动存储表中添加索引

      class AddSearchToActiveStorageBlobs < ActiveRecord::Migration[5.2]
        def change
          add_index :active_storage_blobs,
            "(to_tsvector('english', coalesce(\"active_storage_blobs\".\"metadata\"::text, '')))",
            using: :gin,
            name: 'metadata_search_idx'
        end
      end
      

      【讨论】:

        猜你喜欢
        • 2021-08-12
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 2016-05-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多