Skip to content

Instantly share code, notes, and snippets.

@KitaitiMakoto
Last active October 11, 2015 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KitaitiMakoto/22ba49bee76a178fd816 to your computer and use it in GitHub Desktop.
Save KitaitiMakoto/22ba49bee76a178fd816 to your computer and use it in GitHub Desktop.
RailsでActiveGroongaを使う ref: http://qiita.com/KitaitiMakoto/items/e518a51a804896f9f062
class CreateTerms < ActiveGroonga::Migration
def up
create_table(:terms, :type => :patricia_trie, :key_type => "ShortText", :key_normalize => true, :default_tokenizer => "TokenMecab") do |table|
table.index("articles.body", :with_section => true, :with_weight => true, :with_position => true)
table.timestamps
end
end
def down
remove_table(:terms)
end
end
class AddAuthorsToArticles < ActiveGroonga::Migration
def up
change_table(:articles) do |table|
table.reference :author
end
end
def down
change_table(:articles) do |table|
table.remove_column :author
end
end
end
# :
# :
require "active_groonga/railtie"
require "active_model/railtie"
# :
# :
class Article < ActiveGroonga::Base
validates :title, presence: true
before_validation :ensure_title
private
def ensure_title
return if title
self.title = 'article ' + Time.now.to_s
end
end
class Article < ActiveGroonga::Base
end
$ bundle install
-o, --orm=NAME # Orm to be invoked
# Default: active_groonga
$ ./bin/rails g model -h
(snip)
Usage:
rails generate model NAME [field[:type][:index] field[:type][:index]] [options]
Options:
[--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications)
[--force-plural], [--no-force-plural] # Forces the use of the given model name
-o, --orm=NAME # Orm to be invoked
# Default: active_record
ActiveRecord options:
[--migration], [--no-migration] # Indicates when to generate migration
# Default: true
[--timestamps], [--no-timestamps] # Indicates when to generate timestamps
# Default: true
[--parent=PARENT] # The parent class for the generated model
[--indexes], [--no-indexes] # Add indexes for references and belongs_to columns
# Default: true
-t, [--test-framework=NAME] # Test framework to be invoked
# Default: test_unit
(snip)
$ ./bin/rails g model article key:int:hash title:short_text body:long_text
(snip)
invoke active_groonga
create db/groonga/migrate/20151003192809_create_articles.rb
create app/models/article.rb
invoke test_unit
create test/models/article_test.rb
create test/fixtures/articles.yml
(snip)
field[:type][:index]
$ ./bin/rake groonga:migrate
== 20151003192809 CreateArticles (db/groonga/migrate/20151003192809_create_articles.rb): migrating
== 20151003192809 CreateArticles (db/groonga/migrate/20151003192809_create_articles.rb): migrated (0.0001s)
$ groonga db/groonga/development/db table_list | jq .
(snip)
[
258,
"articles",
"db/groonga/development/db.0000102",
"TABLE_NO_KEY|PERSISTENT",
"Int32",
null,
null,
null
],
[
256,
"schema_migrations",
"db/groonga/development/db.0000100",
"TABLE_HASH_KEY|PERSISTENT",
"UInt64",
null,
null,
null
]
(snip)
$ ./bin/rails g model term key:short_text:patricia_trie:normalize:tokenizer:token_mecab articles.body:index:with_section:with_weight:with_position
(snip)
invoke active_groonga
create db/groonga/migrate/20151003211724_create_terms.rb
create app/models/term.rb
invoke test_unit
create test/models/term_test.rb
create test/fixtures/terms.yml
(snip)
Groonga::Context.default.register_plugin 'path/to/mecab'
$ ./bin/rake groonga:migrate
== 20151004143850 CreateTerms (db/groonga/migrate/20151004143850_create_terms.rb): migrating
== 20151004143850 CreateTerms (db/groonga/migrate/20151004143850_create_terms.rb): migrated (0.0002s)
$ ./bin/rails c
>> Article.all.records.size
=> 0
>> a1 = Article.new(key: 1, title: 'article1', body: 'body')
=> #<Article key: 1, author: nil, body: "body", created_at: nil, title: "article1", updated_at: nil>
>> Article.all.records.size
=> 0
>> a1.save
=> true
>> Article.all.records.size
=> 1
>> a2 = Article.create(key: 2, title: 'article2', body: 'body')
=> #<Article key: 2, author: nil, body: "body", created_at: 2015-10-08 06:08:27 +0900, title: "article2", updated_at: 2015-10-08 06:08:27 +0900>
>> Article.all.records.size
=> 2
>> a1.body = 'first article'
=> "first article"
>> a1.save
=> true
>> Article.all.records.first
=> #<Groonga::Record:0x007f043c0824f0 @table=#<Groonga::Hash id: <258>, name: <articles>, path: <db/groonga/development/db.0000102>, domain: <Int32>, range: (nil), flags: <>, size: <2>, encoding: <:utf8>, default_tokenizer: (nil), token_filters: [], normalizer: (nil)>, @id=1, @added=false, attributes: {"_id"=>1, "_key"=>2, "author"=>nil, "body"=>"body", "created_at"=>2015-10-08 06:08:27 +0900, "title"=>"article2", "updated_at"=>2015-10-08 06:08:27 +0900}>
>> a3 = Article.create(key: 3, body: 'body')
=> #<Article key: 3, author: nil, body: "body", created_at: 2015-10-08 06:09:36 +0900, title: "article 2015-10-08 06:09:36 +0900", updated_at: 2015-10-08 06:09:36 +0900>
>> a3.valid?
=> false
>> a3.save!
ActiveGroonga::RecordInvalid: translation missing: en.activegroonga.errors.messages.record_invalid
:
:
>> a4 = Article.create(key: 4, body: 'body')
=> #<Article key: 4, author: nil, body: "body", created_at: 2015-10-08 06:10:18 +0900, title: "article 2015-10-08 06:10:18 +0900", updated_at: 2015-10-08 06:10:18 +0900>
>> a4.title
=> "article 2015-10-08 06:10:18 +0900"
$ ./bin/rails g model author name:short_text
invoke active_groonga
create db/groonga/migrate/20151006234946_create_authors.rb
create app/models/author.rb
invoke test_unit
create test/models/author_test.rb
create test/fixtures/authors.yml
$ ./bin/rails g migration AddAuthorsToArticles
invoke active_groonga
create db/groonga/migrate/20151007003328_add_authors_to_articles.rb
$ dpkg -L groonga-tokenizer-mecab
$ ./bin/rake groonga:migrate
== 20151006234946 CreateAuthors (db/groonga/migrate/20151006234946_create_authors.rb): migrating
== 20151006234946 CreateAuthors (db/groonga/migrate/20151006234946_create_authors.rb): migrated (0.0001s)
== 20151007003328 AddAuthorsToArticles (db/groonga/migrate/20151007003328_add_authors_to_articles.rb): migrating
== 20151007003328 AddAuthorsToArticles (db/groonga/migrate/20151007003328_add_authors_to_articles.rb): migrated (0.0001s)
>> author1 = Author.create(name: 'author1')
=> #<Author id: 2, created_at: 2015-10-08 04:44:52 +0900, name: "author1", updated_at: 2015-10-08 04:44:52 +0900>
>> a4.author = author1
=> #<Author id: 2, created_at: 2015-10-08 04:44:52 +0900, name: "author1", updated_at: 2015-10-08 04:44:52 +0900>
>> a4.save!
=> true
>> Article.all.to_a.last.author
=> #<Author id: 2, created_at: 2015-10-08 04:44:52 +0900, name: "author1", updated_at: 2015-10-08 04:44:52 +0900>
>> Article.find(1)
=> #<Article key: 1, author: nil, body: "first article", created_at: 2015-10-08 06:09:00 +0900, title: "article1", updated_at: 2015-10-08 06:09:00 +0900>
>> articles = Article.select {|article| article.title == 'article1'}
=> #<ActiveGroonga::ResultSet:0x007f043c003e70 @records=#<Groonga::Hash id: <2147483649>, name: (anonymous), path: (temporary), domain: <articles>, range: (nil), flags: <WITH_SUBREC>, size: <1>, encoding: <:utf8>, default_tokenizer: (nil), token_filters: [], normalizer: (nil)>, @klass=Article(author: authors, body: LongText, created_at: Time, title: ShortText, updated_at: Time), @groups={}, @expression=#<Groonga::Expression
:
:
>> articles.first.title
=> "article1"
>> ['こんにちはGroonga', 'こんにちはElasticsearch', 'こんにちはよいお日柄で'].each_with_index do |body, index|
?> Article.create(key: index + 4, body: body)
>> end
=> ["こんにちはGroonga", "こんにちはElasticsearch", "こんにちはよいお日柄で"]
>> Article.select {|article| article.body =~ 'こんにちは'}.to_a
=> [#<Article key: 4, author: nil, body: "こんにちはGroonga", created_at: 2015-10-08 06:21:32 +0900, title: "article 2015-10-08 06:21:32 +0900", updated_at: 2015-10-08 06:21:32 +0900>, #<Article key: 5, author: nil, body: "こんにちはElasticsearch", created_at: 2015-10-08 06:21:32 +0900, title: "article 2015-10-08 06:21:32 +0900", updated_at: 2015-10-08 06:21:32 +0900>, #<Article key: 6, author: nil, body: "こんにちはよいお日柄で", created_at: 2015-10-08 06:21:32 +0900, title: "article 2015-10-08 06:21:32 +0900", updated_at: 2015-10-08 06:21:32 +0900>]
>> Article.select {|article| article.body =~ '日柄'}.to_a
=> [#<Article key: 6, author: nil, body: "こんにちはよいお日柄で", created_at: 2015-10-08 06:21:32 +0900, title: "article 2015-10-08 06:21:32 +0900", updated_at: 2015-10-08 06:21:32 +0900>]
>> Article.select {|article| article.body.similar_search 'こんにちはActiveGroonga'}.to_a
=> [#<Article key: 4, author: nil, body: "こんにちはGroonga", created_at: 2015-10-08 06:21:32 +0900, title: "article 2015-10-08 06:21:32 +0900", updated_at: 2015-10-08 06:21:32 +0900>, #<Article key: 5, author: nil, body: "こんにちはElasticsearch", created_at: 2015-10-08 06:21:32 +0900, title: "article 2015-10-08 06:21:32 +0900", updated_at: 2015-10-08 06:21:32 +0900>, #<Article key: 6, author: nil, body: "こんにちはよいお日柄で", created_at: 2015-10-08 06:21:32 +0900, title: "article 2015-10-08 06:21:32 +0900", updated_at: 2015-10-08 06:21:32 +0900>]
$ ./bin/rake -T groonga
rake groonga:create # Create the database
rake groonga:drop # Drops the database
rake groonga:migrate # Migrate the database (options: VERSION=x)
rake groonga:migrate:down # Migrate the schema down to the version (options: VERSION=x)
rake groonga:migrate:redo # Rolls the schema back and migrate the schema again
rake groonga:migrate:status # Display status of migration
rake groonga:migrate:up # Migrate the schema up to the version (options: VERSION=x)
rake groonga:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
rake groonga:schema:dump # Dump the schema
rake groonga:schema:load # Load the schema
rake groonga:seed # Load the seed data from db/groonga/seeds/#{RAILS_ENV}.grn, db/groonga/seeds/#{RAILS_ENV}.rb, db/groonga/seeds.grn or db/groonga/seeds.rb
rake groonga:setup # Create the database and load the schema
rake groonga:test:env # Set Rails.env = 'test'
rake groonga:test:prepare # Prepare groonga database for testing
$ ./bin/rails g -h
(snip)
ActiveGroonga:
active_groonga:migration
active_groonga:model
(snip)
$ ./bin/rails g active_groonga:migration -h
(snip)
Usage:
rails generate active_groonga:migration NAME [name:type[:option:option] name:type[:option:option]] [options]
Options:
[--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications)
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend], [--no-pretend] # Run but do not make any changes
-q, [--quiet], [--no-quiet] # Suppress status output
-s, [--skip], [--no-skip] # Skip files that already exist
Description:
Create active groonga files for migration generator.
(snip)
$ ./bin/rails g active_groonga:model -h
(snip)
Usage:
rails generate active_groonga:model NAME [name:type[:option:option] name:type[:option:option]] [options]
Options:
[--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications)
[--migration], [--no-migration] # Indicates when to generate migration
# Default: true
[--timestamps], [--no-timestamps] # Indicates when to generate timestamps
# Default: true
[--parent=PARENT] # The parent class for the generated model
-t, [--test-framework=NAME] # Test framework to be invoked
# Default: test_unit
TestUnit options:
[--fixture], [--no-fixture] # Indicates when to generate fixture
# Default: true
-r, [--fixture-replacement=NAME] # Fixture replacement to be invoked
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend], [--no-pretend] # Run but do not make any changes
-q, [--quiet], [--no-quiet] # Suppress status output
-s, [--skip], [--no-skip] # Skip files that already exist
Description:
Create active groonga files for model generator.
(snip)
$ ./bin/rails g model -h
(snip)
Usage:
rails generate model NAME [field[:type][:index] field[:type][:index]] [options]
Options:
[--skip-namespace], [--no-skip-namespace] # Skip namespace (affects only isolated applications)
[--force-plural], [--no-force-plural] # Forces the use of the given model name
-o, --orm=NAME # Orm to be invoked
# Default: active_groonga
ActiveGroonga options:
[--migration], [--no-migration] # Indicates when to generate migration
# Default: true
[--timestamps], [--no-timestamps] # Indicates when to generate timestamps
# Default: true
[--parent=PARENT] # The parent class for the generated model
-t, [--test-framework=NAME] # Test framework to be invoked
# Default: test_unit
(snip)
gem 'activegroonga', require: 'active_groonga'
class CreateArticles < ActiveGroonga::Migration
def up
create_table(:articles, :type => :hash, :key_type => "Int32") do |table|
table.short_text(:title)
table.long_text(:body)
table.timestamps
end
end
def down
remove_table(:articles)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment