myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [2571] trunk: added solr search feature to index


From: noreply
Subject: [myexperiment-hackers] [2571] trunk: added solr search feature to index views
Date: Tue, 8 Mar 2011 10:58:48 -0500 (EST)

Revision
2571
Author
dgc
Date
2011-03-08 10:58:47 -0500 (Tue, 08 Mar 2011)

Log Message

added solr search feature to index views

Modified Paths

Diff

Modified: trunk/app/controllers/application.rb (2570 => 2571)


--- trunk/app/controllers/application.rb	2011-03-02 17:02:29 UTC (rev 2570)
+++ trunk/app/controllers/application.rb	2011-03-08 15:58:47 UTC (rev 2571)
@@ -531,7 +531,8 @@
         :networks            => "INNER JOIN networks ON permissions.contributor_type = 'Network' AND permissions.contributor_id = networks.id",
         :credits             => "INNER JOIN creditations ON creditations.creditable_type = contributions.contributable_type AND creditations.creditable_id = contributions.contributable_id",
         :curation_events     => "INNER JOIN curation_events ON curation_events.object_type = contributions.contributable_type AND curation_events.object_id = contributions.contributable_id",
-        :workflow_processors => "INNER JOIN workflow_processors ON contributions.contributable_type = 'Workflow' AND workflow_processors.workflow_id = contributions.contributable_id"
+        :workflow_processors => "INNER JOIN workflow_processors ON contributions.contributable_type = 'Workflow' AND workflow_processors.workflow_id = contributions.contributable_id",
+        :search              => "RIGHT OUTER JOIN search_results ON search_results.result_type = contributions.contributable_type AND search_results.result_id = contributions.contributable_id"
       }
     }
   end
@@ -673,6 +674,7 @@
         end
       end
 
+      query["query"]        = params[:query]        if params[:query]
       query["order"]        = params[:order]        if parts.include?(:order)
       query["filter_query"] = params[:filter_query] if parts.include?(:filter_query)
 
@@ -688,7 +690,38 @@
         "#{lhs} IN ('#{rhs.map do |bit| escape_sql(bit) end.join("', '")}')"
       end
     end
-  
+
+    def create_search_results_table(search_query, models)
+
+      solr_results = User.multi_solr_search(search_query,
+          :models         => models,
+          :results_format => :ids,
+          :limit          => Conf.max_search_size)
+
+      conn =  ActiveRecord::Base.connection
+
+      conn.execute("CREATE TEMPORARY TABLE search_results (result_type VARCHAR(255), result_id INT)")
+
+      # This next part converts the search results to SQL values
+      #
+      # from:  { "id" => "Workflow:4" }, { "id" => "Pack:6" }, ...
+      # to:    "('Workflow', '4'), ('Pack', '6'), ..."
+
+      if solr_results.results.length > 0
+        insert_part = solr_results.results.map do |result|
+          "(" + result["id"].split(":").map do |bit|
+            "'#{bit}'"
+          end.join(", ") + ")"
+        end.join(", ")
+
+        conn.execute("INSERT INTO search_results VALUES #{insert_part}")
+      end
+    end
+
+    def drop_search_results_table
+      ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS search_results")
+    end
+
     def calculate_having_clause(filter, opts)
 
       having_bits = []
@@ -731,6 +764,8 @@
         end
       end
 
+      joins.push(:search) if params[:query]
+
       current = find_filter(opts[:filters], filter[:query_option]) ? find_filter(opts[:filters], filter[:query_option])[:expr][:terms] : []
 
       if opts[:ids].nil?
@@ -896,6 +931,14 @@
 
     joins += order_options[:joins] if order_options[:joins]
 
+    # perform search if requested
+
+    if params["query"]
+      drop_search_results_table
+      create_search_results_table(params["query"], [Workflow, Blob, Pack])
+      joins.push(:search)
+    end
+
     having_bits = []
 
 #   pivot_options[:filters].each do |filter|

Modified: trunk/app/views/content/_index.rhtml (2570 => 2571)


--- trunk/app/views/content/_index.rhtml	2011-03-02 17:02:29 UTC (rev 2570)
+++ trunk/app/views/content/_index.rhtml	2011-03-08 15:58:47 UTC (rev 2571)
@@ -61,12 +61,23 @@
     <div class="summary">
       <div class="result-count">
         Showing <%= pluralize(@pivot[:results].size, 'result') -%>.
-        <% if @pivot[:summary].length > 0 %>
-          The following filters are in effect:
-        <% end %>
+        <% if @pivot[:results].size > 1 %>Use the filters on the left and the
+        search box below to refine the results.<% end %>
       </div>
+      <div>
+        <form action="" url_for(request.query_parameters) -%>" method="GET">
+          <div class="search_box">
+            <input class="query" name="query" value="<%= params[:query] -%>" />
+            <% if request.query_parameters["filter"] %>
+              <input name="filter" type="hidden" value="<%= request.query_parameters["filter"].gsub('"', '&quot;') -%>" />
+            <% end %>
+            <input class="submit" type="button" name="submit" value="Search" />
+          </div>
+        </form>
+      </div>
       <% if @pivot[:summary].length > 0 %>
-        <div class="crumbs"><%= @pivot[:summary] -%></div>
+        <div class="crumbs">
+          <%= @pivot[:summary] -%></div>
       <% end %>
       <% if @pivot[:reset_filters_url] %>
         <div class="reset_filters"><%= link_to("Remove all filters", @pivot[:reset_filters_url]) -%></div>

Modified: trunk/lib/authorization.rb (2570 => 2571)


--- trunk/lib/authorization.rb	2011-03-02 17:02:29 UTC (rev 2570)
+++ trunk/lib/authorization.rb	2011-03-08 15:58:47 UTC (rev 2571)
@@ -851,7 +851,7 @@
     end
 
     if model != Contribution
-      joins.push("INNER JOIN contributions ON contributions.contributable_id = #{auth_id} AND contributions.contributable_type = #{auth_type}")
+      joins.push("LEFT OUTER JOIN contributions ON contributions.contributable_id = #{auth_id} AND contributions.contributable_type = #{auth_type}")
     end
 
     # selection
@@ -860,7 +860,7 @@
 
     # add in the extra joins needed for the authorisation checks
 
-    joins.push("INNER JOIN policies ON contributions.policy_id = policies.id")
+    joins.push("LEFT OUTER JOIN policies ON contributions.policy_id = policies.id")
     joins.push("LEFT OUTER JOIN permissions ON policies.id = permissions.policy_id") if user_id || opts[:include_permissions]
 
     # include the effective permissions in the result?
@@ -869,7 +869,7 @@
 
       opts[:select] << ", BIT_OR(#{view_conditions(user_id, friends, networks)})     AS view_permission"
       opts[:select] << ", BIT_OR(#{download_conditions(user_id, friends, networks)}) AS download_permission"
-      opts[:select] << ", BIT_OR(#{edit_conditions(user_id, friends, networks)})           AS edit_permission"
+      opts[:select] << ", BIT_OR(#{edit_conditions(user_id, friends, networks)})     AS edit_permission"
     end
 
     # merge the joins

Modified: trunk/public/stylesheets/styles.css (2570 => 2571)


--- trunk/public/stylesheets/styles.css	2011-03-02 17:02:29 UTC (rev 2570)
+++ trunk/public/stylesheets/styles.css	2011-03-08 15:58:47 UTC (rev 2571)
@@ -2062,12 +2062,11 @@
   clear: right;
   background: #f0f0f0;
   border: 1px solid #d8d8d8;
-  padding: 8px;
+  padding: 6px;
 }
 
-.pivot .summary DIV {
-  padding-top: 0.2em;
-  padding-bottom: 0.2em;
+.pivot .summary DIV+DIV {
+  padding-top: 0.4em;
 }
 
 .pivot .sort {
@@ -2181,7 +2180,8 @@
 }
 
 .pivot .filter_search_box INPUT.query {
-  width: 127px;
+  width: 123px;
+  padding: 2px;
   border: none;
   outline: none;
 }
@@ -2198,9 +2198,6 @@
   top: -1px;
 }
 
-.result-count {
-}
-
 .truncate {
   white-space: nowrap;
 }
@@ -2216,3 +2213,8 @@
 	font-style: italic;
 }
 
+.pivot .search_box .query {
+  padding: 2px;
+  width: 450px;
+}
+

reply via email to

[Prev in Thread] Current Thread [Next in Thread]