我使用 attr_encrypted 来加密我的一些模型字段,我使用 Tire 和 Elasticsearch 进行全文搜索。我只使用一个简单的搜索表单。 这是我模型的一部分:
class Student < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
attr_accessible :name, :surname
attr_encrypted :name, :key => 'f98gd9regre9gr9gre9gerh'
attr_encrypted :surname, :key => 'f98gd9regre9gr9gre9gerh'
def self.search(params)
tire.search(load: true) do
query { string Student.encrypt_name(params[:search]) } if params[:search].present?
end
end
end
例如,如果我在数据库中有名字“John”,当我搜索“John”时,查询在查询数据库之前被加密 (Student.encrypt_name(params[:search])),并且结果被返回。 Elasticsearch允许通配符搜索,比如我搜索“Joh*”,应该返回匹配的结果,但是加密的关键字“Joh”与加密的“John”不同,db没有返回结果。对此的任何解决方案将不胜感激。
问候, 拉多斯拉夫
最佳答案
简答 - 全文搜索和客户端加密在当前最先进的技术中是相互排斥的。
更长的答案:
关于ruby-on-rails - 如何对数据库中的加密元素进行通配符搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12474788/