简体   繁体   English

带有Elasticsearch和Tire的灵活模型

[英]Flexible model with Elasticsearch and Tire

In Rails application I want to be able to store schema-less objects -- JSON data structure, that could have different structure from object to object, or for the same object at different times. 在Rails应用程序中,我希望能够存储无模式的对象-JSON数据结构,对象与对象之间的结构可能不同,或者同一对象在不同时间可能具有不同的结构。

With ElasticSearch I can do this. 使用ElasticSearch,我可以做到这一点。 However I would like to be use some kind of ORM with Rails for this. 但是我想为此使用某种ORM与Rails。

Tried to do some testing with Tire tutorial, doing this in Rails console: 尝试使用Tire教程进行一些测试,在Rails控制台中执行以下操作:

>>> c = Article.new :title =>'New article', :content => { :a => 'a', :b => 'b'}

I can see that content data stored as serialized YAML: 我可以看到内容数据存储为序列化的YAML:

>> Article.last.content
=> "---\n:a: a\n:b: b\n"

This will require parsing search result to JSON, which is not a problem. 这将需要将搜索结果解析为JSON,这不是问题。 But main question: will I be able to search inside serialized data? 但主要问题是:我将能够搜索内部序列化数据吗? Is there a way to return attribute names from arbitrary structured data (like Object.keys)? 有没有办法从任意结构化数据(如Object.keys)中返回属性名称?

If not Tire, is there other solutions to do this? 如果不是Tyre,还有其他解决方案吗?

Not really sure what you're up to here, but Tire does come with a drop-in replacement for ActiveRecord integration, just include Tire::Model::Persistence in your class. 不太确定您要做什么,但是Tire确实提供了ActiveRecord集成的直接替代品,只需在类中include Tire::Model::Persistence You may also define properties (with mappings, type casting, default values, etc): 您还可以定义属性(具有映射,类型转换,默认值等):

class Article

  include Tire::Model::Persistence

  property :title
  property :published_on, type: 'date'
  property :tags,         analyzer: 'keyword', default: []

end

The Tire README and integration tests have all the info. Tire README和集成测试包含所有信息。

Of course you're able to search inside the content attribute -- it's just a matter of proper mapping. 当然,您可以在content属性中进行搜索-只是适当的映射即可。

If you're after some specific behaviour, please update your question... 如果您要遵循某些特定的行为,请更新您的问题...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM