mirror of https://github.com/apache/lucene.git
Add more features to the UI: inverting/removing of filters and queries, Ajax suggestion of terms.
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@500769 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
8ad1ec2f87
commit
7e877bc49f
|
@ -5,35 +5,58 @@ class BrowseController < ApplicationController
|
|||
before_filter :setup_session
|
||||
|
||||
def index
|
||||
request = Solr::Request::Standard.new :query => session[:queries].join(' AND '), :filter_queries => session[:filters]
|
||||
@results = SOLR.send(request).data
|
||||
@info = SOLR.send(Solr::Request::IndexInfo.new) # TODO move this call to only have it called when the index may have changed
|
||||
@facet_fields = @info.field_names.find_all {|v| v =~ /_facet$/}
|
||||
|
||||
request = Solr::Request::Standard.new :query => query,
|
||||
:filter_queries => filters,
|
||||
:facets => {:fields => @facet_fields, :limit => 20 , :mincount => 1, :sort => :count, :debug_query=>true}
|
||||
@response = SOLR.send(request)
|
||||
|
||||
#TODO: call response.field_facets(??) - maybe field_facets should be return a higher level?
|
||||
end
|
||||
|
||||
def facet
|
||||
@field = "#{params[:field]}_facet"
|
||||
req = Solr::Request::Standard.new(:query => session[:queries].join(' AND '),
|
||||
:filter_queries => session[:filters],
|
||||
:facets => {:fields => [@field],
|
||||
:mincount => 1, :sort => :count
|
||||
},
|
||||
:rows => 0
|
||||
)
|
||||
|
||||
results = SOLR.send(req)
|
||||
|
||||
@facets = results.data['facet_counts']['facet_fields'][@field]
|
||||
@facets = retrieve_field_facets("#{params[:field]}")
|
||||
end
|
||||
|
||||
def auto_complete_for_search_query
|
||||
# TODO instead of "text", default to the default search field configured in schema.xml
|
||||
@values = retrieve_field_facets("text", 5, params['search']['query'].downcase)
|
||||
|
||||
render :partial => 'suggest'
|
||||
end
|
||||
|
||||
|
||||
def add_query
|
||||
if session[:queries].size == 1
|
||||
session[:queries] = [] if session[:queries][0] == "[* TO *]"
|
||||
session[:queries] << {:query => params[:search][:query]}
|
||||
redirect_to :action => 'index'
|
||||
end
|
||||
session[:queries] << params[:query]
|
||||
|
||||
def invert_query
|
||||
q = session[:queries][params[:index].to_i]
|
||||
q[:negative] = !q[:negative]
|
||||
redirect_to :action => 'index'
|
||||
end
|
||||
|
||||
def remove_query
|
||||
session[:queries].delete_at(params[:index].to_i)
|
||||
redirect_to :action => 'index'
|
||||
end
|
||||
|
||||
def invert_filter
|
||||
f = session[:filters][params[:index].to_i]
|
||||
f[:negative] = !f[:negative]
|
||||
redirect_to :action => 'index'
|
||||
end
|
||||
|
||||
def remove_filter
|
||||
session[:filters].delete_at(params[:index].to_i)
|
||||
redirect_to :action => 'index'
|
||||
end
|
||||
|
||||
def add_filter
|
||||
session[:filters] << "#{params[:field]}:#{params[:value]}"
|
||||
session[:filters] << {:field => params[:field], :value => params[:value]}
|
||||
redirect_to :action => 'index'
|
||||
end
|
||||
|
||||
|
@ -46,7 +69,36 @@ class BrowseController < ApplicationController
|
|||
|
||||
private
|
||||
def setup_session
|
||||
session[:queries] = ["[* TO *]"] if session[:queries] == nil || session[:queries] .empty?
|
||||
session[:queries] ||= []
|
||||
session[:filters] ||= []
|
||||
end
|
||||
|
||||
def retrieve_field_facets(field, limit=-1, prefix=nil)
|
||||
req = Solr::Request::Standard.new(:query => query,
|
||||
:filter_queries => filters,
|
||||
:facets => {:fields => [field],
|
||||
:mincount => 1, :limit => limit, :prefix => prefix
|
||||
},
|
||||
:rows => 0
|
||||
)
|
||||
|
||||
results = SOLR.send(req)
|
||||
|
||||
results.field_facets(field)
|
||||
end
|
||||
|
||||
def query
|
||||
queries = session[:queries]
|
||||
if queries.nil? || queries.empty?
|
||||
query = "[* TO *]"
|
||||
else
|
||||
query = session[:queries].collect{|q| "#{q[:negative] ? '-' : ''}(#{q[:query]})"}.join(' AND ')
|
||||
end
|
||||
|
||||
query
|
||||
end
|
||||
|
||||
def filters
|
||||
session[:filters].collect {|filter| "#{filter[:negative] ? '-' : ''}#{filter[:field]}:\"#{filter[:value]}\""}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<ul class="cookies">
|
||||
<%
|
||||
@values.each do |value|
|
||||
%>
|
||||
<li class="cookie"><span class="informal"> </span><%=value[0]%><span class="informal"> (<%=value[1]%>)<br/></span></li>
|
||||
<%
|
||||
end
|
||||
%>
|
||||
</ul>
|
|
@ -1,14 +1,47 @@
|
|||
<% form_tag(:action=>'add_query') do %>
|
||||
Query: <%=text_field_tag 'query'%>
|
||||
Query: <%= text_field_with_auto_complete :search, :query %>
|
||||
<% end %>
|
||||
<%=link_to 'clear constraints', :action => 'clear'%>
|
||||
Queries: <%=session[:queries].join(' AND ')%>
|
||||
Filters: <%=session[:filters].join(' >>> ')%>
|
||||
|
||||
Found: <%=@results['response']['numFound']%>
|
||||
<div>
|
||||
Queries:
|
||||
<ul>
|
||||
<% @results['response']['docs'].each do |doc| %>
|
||||
<li><%=doc.inspect%></li>
|
||||
<% session[:queries].each_with_index do |q,i| %>
|
||||
<li><%=link_to q[:negative] ? "-" : '+', :action => :invert_query, :index => i%> <%=q[:query]%> <%=link_to "remove", :action => :remove_query, :index => i %></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
<%=debug @results['responseHeader']%>
|
||||
</div>
|
||||
<div>
|
||||
Filters:
|
||||
<ul>
|
||||
<% session[:filters].each_with_index do |filter, i| %>
|
||||
<li>
|
||||
<%=link_to filter[:negative] ? "-" : "+", :action => :invert_filter, :index => i%>
|
||||
<%=filter[:field]%>:<%=filter[:value]%>
|
||||
<%=link_to "remove", :action => :remove_filter, :index => i %>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<h2>facets</h2>
|
||||
<div>
|
||||
<% @facet_fields.each do |field|%>
|
||||
<div>
|
||||
<%=field%>:
|
||||
<% @response.field_facets(field).each do |k,v| %>
|
||||
<%= link_to "#{k} (#{v})", :action => 'add_filter', :field=>field, :value=>k%>
|
||||
<% end %> <%=link_to "more...", :action => 'facet', :field => field%>
|
||||
</div>
|
||||
<% end %>
|
||||
</div>
|
||||
<h2>results</h2>
|
||||
<div>
|
||||
Found: <%=@response.total_hits%>
|
||||
<ul>
|
||||
<% @response.each do |doc| %>
|
||||
<li><%= debug doc%></li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<h3>index info</h3><%=debug @info %>
|
||||
|
||||
<h3>search/facet response header</h3><%=debug @response.data['responseHeader']%>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Flare: <%=controller.action_name%></title>>
|
||||
<title>Flare: <%=controller.action_name%></title>
|
||||
<%= javascript_include_tag :defaults %>
|
||||
</head>
|
||||
<body>
|
||||
<%= yield %>
|
||||
|
|
|
@ -54,7 +54,7 @@ end
|
|||
# end
|
||||
|
||||
# Include your application configuration below
|
||||
$KCODE = 'UTF8'
|
||||
# $KCODE = 'UTF8' # Rails 1.2 supposedly sets this automatically
|
||||
|
||||
require 'solr'
|
||||
SOLR = Solr::Connection.new("http://localhost:8983/solr")
|
||||
|
|
Loading…
Reference in New Issue