[Groonga-commit] ranguba/groonga-client-rails at 66c1ebc [master] Add search feature

Back to archive index

Kouhei Sutou null+****@clear*****
Wed Dec 21 17:33:09 JST 2016


Kouhei Sutou	2016-12-21 17:33:09 +0900 (Wed, 21 Dec 2016)

  New Revision: 66c1ebc3bd2f4c2c1e1dea98283f7c649a443579
  https://github.com/ranguba/groonga-client-rails/commit/66c1ebc3bd2f4c2c1e1dea98283f7c649a443579

  Message:
    Add search feature

  Modified files:
    test/apps/rails5-activerecord/Gemfile.lock
    test/apps/rails5-activerecord/app/assets/stylesheets/posts.scss
    test/apps/rails5-activerecord/app/controllers/posts_controller.rb
    test/apps/rails5-activerecord/app/views/posts/index.html.erb

  Modified: test/apps/rails5-activerecord/Gemfile.lock (+6 -1)
===================================================================
--- test/apps/rails5-activerecord/Gemfile.lock    2016-12-21 17:13:24 +0900 (c7bff8a)
+++ test/apps/rails5-activerecord/Gemfile.lock    2016-12-21 17:33:09 +0900 (c199266)
@@ -1,9 +1,10 @@
 PATH
   remote: ../../../../groonga-client
   specs:
-    groonga-client (0.3.4)
+    groonga-client (0.3.9)
       gqtp (>= 1.0.4)
       groonga-command (>= 1.2.8)
+      groonga-command-parser (>= 1.0.7)
       hashie
 
 PATH
@@ -73,6 +74,9 @@ GEM
     gqtp (1.0.6)
     groonga-command (1.3.1)
       json
+    groonga-command-parser (1.0.7)
+      groonga-command (>= 1.0.9)
+      json-stream
     hashie (3.4.6)
     i18n (0.7.0)
     jbuilder (2.6.1)
@@ -83,6 +87,7 @@ GEM
       railties (>= 4.2.0)
       thor (>= 0.14, < 2.0)
     json (2.0.2)
+    json-stream (0.2.1)
     listen (3.0.8)
       rb-fsevent (~> 0.9, >= 0.9.4)
       rb-inotify (~> 0.9, >= 0.9.7)

  Modified: test/apps/rails5-activerecord/app/assets/stylesheets/posts.scss (+4 -0)
===================================================================
--- test/apps/rails5-activerecord/app/assets/stylesheets/posts.scss    2016-12-21 17:13:24 +0900 (1a7e153)
+++ test/apps/rails5-activerecord/app/assets/stylesheets/posts.scss    2016-12-21 17:33:09 +0900 (eab42b8)
@@ -1,3 +1,7 @@
 // Place all the styles related to the posts controller here.
 // They will automatically be included in application.css.
 // You can use Sass (SCSS) here: http://sass-lang.com/
+
+.keyword {
+  color: red;
+}

  Modified: test/apps/rails5-activerecord/app/controllers/posts_controller.rb (+19 -1)
===================================================================
--- test/apps/rails5-activerecord/app/controllers/posts_controller.rb    2016-12-21 17:13:24 +0900 (16bba99)
+++ test/apps/rails5-activerecord/app/controllers/posts_controller.rb    2016-12-21 17:33:09 +0900 (eae7d4e)
@@ -4,7 +4,25 @@ class PostsController < ApplicationController
   # GET /posts
   # GET /posts.json
   def index
-    @posts = Post.all
+    search = PostsSearcher.new.search
+    query = params[:query]
+    if query.blank?
+      search = search.sort_keys("-updated_at")
+    else
+      search = search.
+        query(query).
+        sort_keys("-_score")
+    end
+    search = search.
+      output_columns([
+                       "_key",
+                       "_score",
+                       "*",
+                       "highlight_html(title)",
+                       "snippet_html(body)",
+                     ]).
+      paginate(params[:page])
+    @result_set = search.result_set
   end
 
   # GET /posts/1

  Modified: test/apps/rails5-activerecord/app/views/posts/index.html.erb (+17 -3)
===================================================================
--- test/apps/rails5-activerecord/app/views/posts/index.html.erb    2016-12-21 17:13:24 +0900 (dd12307)
+++ test/apps/rails5-activerecord/app/views/posts/index.html.erb    2016-12-21 17:33:09 +0900 (6575b58)
@@ -2,6 +2,13 @@
 
 <h1>Posts</h1>
 
+<p><%= @result_set.n_hits %> records</p>
+
+<%= form_tag(posts_path, method: "get") do %>
+  <%= search_field_tag "query", params["query"] %>
+  <%= submit_tag "Search" %>
+<% end %>
+
 <table>
   <thead>
     <tr>
@@ -12,10 +19,17 @@
   </thead>
 
   <tbody>
-    <% @posts.each do |post| %>
+    <% @result_set.records.each do |record| %>
+      <% post = record.source %>
       <tr>
-        <td><%= post.title %></td>
-        <td><%= post.body %></td>
+        <td><%= record.highlight_html.html_safe %></td>
+        <td>
+          <% if record.snippet_html.blank? %>
+            <%= post.body %>
+          <% else %>
+            <%= record.snippet_html.join("<br>").html_safe %>
+          <% end %>
+        </td>
         <td><%= link_to 'Show', post %></td>
         <td><%= link_to 'Edit', edit_post_path(post) %></td>
         <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
-------------- next part --------------
HTML����������������������������...
Download 



More information about the Groonga-commit mailing list
Back to archive index