- Ignore javascript search calls if we don't have a query

- Make /search support html, text, or json responses via
  ?format=(html|json|txt|text) - default is html.
- If see "No query given" even after we have query, we'll update it to
  say "Loading query ...."
This commit is contained in:
Jordan Sissel 2010-12-22 18:55:45 -08:00
parent b0cdd55aa0
commit 4883264ec9
4 changed files with 55 additions and 6 deletions

View file

@ -7,6 +7,11 @@
},
search: function(query) {
if (query == undefined || query == "") {
return;
}
var display_query = query.replace("<", "&lt;").replace(">", "&gt;")
$("#querystatus").html("Loading query '" + display_query + "'")
logstash.params.q = query;
document.location.hash = escape(JSON.stringify(logstash.params));
$("#results").load("/search/ajax", logstash.params);

View file

@ -32,17 +32,51 @@ class LogStash::Web::Server < Sinatra::Base
end # '/'
aget '/search' do
result_callback = proc do
status 500 if @error
params[:format] ||= "html"
case params[:format]
when "html"
headers({"Content-Type" => "text/html" })
body haml :"search/results", :layout => !request.xhr?
when "text"
headers({"Content-Type" => "text/plain" })
body erb :"search/results.txt", :layout => false
when "txt"
headers({"Content-Type" => "text/plain" })
body erb :"search/results.txt", :layout => false
when "json"
headers({"Content-Type" => "text/plain" })
hits = @hits.collect { |h| h["_source"] }
response = {
"hits" => hits,
"facets" => (@results["facets"] rescue nil),
}
response["error"] = @error if @error
body response.to_json
end # case params[:format]
end # proc result_callback
# We'll still do a search query here even though most users
# have javascript enabled, we need to show the results in
# case a user doesn't have javascript.
if params[:q] and params[:q] != ""
elasticsearch.search(params) do |@results|
@hits = (@results["hits"]["hits"] rescue [])
body haml :"search/results", :layout => !request.xhr?
begin
result_callback.call
rescue => e
puts e
end
end
else
#@error = "No query given."
@hits = []
body haml :"search/results", :layout => !request.xhr?
end
result_callback.call
end
end # aget '/search'
apost '/search/ajax' do
headers({"Content-Type" => "text/html" })

View file

@ -31,9 +31,9 @@
last
- if @hits.length == 0
- if !params[:q]
No query given. How about <a href="?q=*" class="querychanger">this?</a>
%h3#querystatus No query given. How about <a href="?q=*" class="querychanger">this?</a>
- else
No results for query '#{params[:q]}'
%h3#querystatus No results for query '#{params[:q]}'
- else
%table.results
%tr

View file

@ -0,0 +1,10 @@
<%
# Sinatra currently doesn't do ERB with newline trimming, so we
# have to write this funky mishmosh that is hard to read.
if @error %>Error: <%= @error %><% else
@hits.each do |hit|
event = LogStash::Event.new(hit["_source"])
%><%= event.message || event.to_hash.to_json %>
<% end
end
%>