Kouhei Sutou
null+****@clear*****
Sun May 25 16:09:03 JST 2014
Kouhei Sutou 2014-05-25 16:09:03 +0900 (Sun, 25 May 2014) New Revision: 40b49e60391b4609be89f9e50dc22ff1df7e5580 https://github.com/groonga/heroku-sample-rroonga-blog/commit/40b49e60391b4609be89f9e50dc22ff1df7e5580 Message: Support index and search Added files: app/models/search_condition.rb config/initializers/groonga.rb lib/post_indexer.rb lib/post_searcher.rb Modified files: app/controllers/posts_controller.rb app/models/post.rb app/views/posts/index.html.erb config/application.rb groonga/init.rb Modified: app/controllers/posts_controller.rb (+11 -1) =================================================================== --- app/controllers/posts_controller.rb 2014-05-25 12:56:32 +0900 (d566eaf) +++ app/controllers/posts_controller.rb 2014-05-25 16:09:03 +0900 (f13d25b) @@ -4,7 +4,13 @@ class PostsController < ApplicationController # GET /posts # GET /posts.json def index - @posts = Post.all + @search_condition = SearchCondition.new(search_condition_params) + if @search_condition.valid? + searcher = PostSearcher.new(@search_condition.query) + @posts = searcher.search + else + @posts = Post.all + end end # GET /posts/1 @@ -71,4 +77,8 @@ class PostsController < ApplicationController def post_params params.require(:post).permit(:title, :content) end + + def search_condition_params + params.permit(:query) + end end Modified: app/models/post.rb (+9 -0) =================================================================== --- app/models/post.rb 2014-05-25 12:56:32 +0900 (791dcb5) +++ app/models/post.rb 2014-05-25 16:09:03 +0900 (35913aa) @@ -1,2 +1,11 @@ class Post < ActiveRecord::Base + after_save do |post| + indexer = PostIndexer.new + indexer.add(post) + end + + after_destroy do |post| + indexer = PostIndexer.new + indexer.remove(post) + end end Added: app/models/search_condition.rb (+8 -0) 100644 =================================================================== --- /dev/null +++ app/models/search_condition.rb 2014-05-25 16:09:03 +0900 (ee1a6ea) @@ -0,0 +1,8 @@ +class SearchCondition + include ActiveModel::Model + include ActiveModel::Validations + + attr_accessor :query + + validates_presence_of :query +end Modified: app/views/posts/index.html.erb (+5 -0) =================================================================== --- app/views/posts/index.html.erb 2014-05-25 12:56:32 +0900 (e29a7e3) +++ app/views/posts/index.html.erb 2014-05-25 16:09:03 +0900 (c5a5b5f) @@ -1,5 +1,10 @@ <h1>Listing posts</h1> +<%= form_tag(posts_path, method: "get") do %> + <%= search_field_tag :query, @search_condition.query %> + <%= submit_tag 'Search' %> +<% end %> + <table> <thead> <tr> Modified: config/application.rb (+2 -0) =================================================================== --- config/application.rb 2014-05-25 12:56:32 +0900 (c0d1a5a) +++ config/application.rb 2014-05-25 16:09:03 +0900 (dbc2bde) @@ -19,5 +19,7 @@ module HerokuBlog # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] # config.i18n.default_locale = :de + + config.autoload_paths += ["#{config.root}/lib"] end end Added: config/initializers/groonga.rb (+9 -0) 100644 =================================================================== --- /dev/null +++ config/initializers/groonga.rb 2014-05-25 16:09:03 +0900 (cdc3da4) @@ -0,0 +1,9 @@ +require 'fileutils' +require 'groonga' + +database_path = ENV['GROONGA_DATABASE_PATH'] || 'groonga/database' +if File.exist?(database_path) + Groonga::Database.open(database_path) +else + Groonga::Database.create(path: database_path) +end Modified: groonga/init.rb (+2 -13) =================================================================== --- groonga/init.rb 2014-05-25 12:56:32 +0900 (d50b20c) +++ groonga/init.rb 2014-05-25 16:09:03 +0900 (f8ba376) @@ -1,15 +1,5 @@ require_relative '../config/environment' -require 'fileutils' -require 'groonga' - -database_path = ENV['GROONGA_DATABASE_PATH'] || 'groonga/database' -if File.exist?(database_path) - Groonga::Database.open(database_path) -else - Groonga::Database.create(path: database_path) -end - Groonga::Schema.define do |schema| schema.create_table('Posts', type: :hash, @@ -21,11 +11,10 @@ Groonga::Schema.define do |schema| end end +indexer = PostIndexer.new posts = Groonga['Posts'] Post.all.each do |post| - attributes = post.attributes - id = attributes.delete("id") - posts.add(id, attributes) + indexer.add(post) end Groonga::Schema.define do |schema| Added: lib/post_indexer.rb (+15 -0) 100644 =================================================================== --- /dev/null +++ lib/post_indexer.rb 2014-05-25 16:09:03 +0900 (473752b) @@ -0,0 +1,15 @@ +class PostIndexer + def initialize + @posts = Groonga['Posts'] + end + + def add(post) + attributes = post.attributes + id = attributes.delete('id') + @posts.add(id, attributes) + end + + def remove(post) + @posts[post.id].delete + end +end Added: lib/post_searcher.rb (+16 -0) 100644 =================================================================== --- /dev/null +++ lib/post_searcher.rb 2014-05-25 16:09:03 +0900 (7a59af9) @@ -0,0 +1,16 @@ +class PostSearcher + def initialize(query) + @query = query + @posts = Groonga['Posts'] + end + + def search + matched_posts =****@posts***** do |record| + (record.title =~ @query) | (record.content =~ @query) + end + post_ids = matched_posts.sort(['_score']).collect do |matched_post| + matched_post._key + end + Post.where(id: post_ids) + end +end -------------- next part -------------- HTML����������������������������... 다운로드