mirror of
https://github.com/elastic/logstash.git
synced 2025-04-24 22:57:16 -04:00
- 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:
parent
b0cdd55aa0
commit
4883264ec9
4 changed files with 55 additions and 6 deletions
|
@ -7,6 +7,11 @@
|
||||||
},
|
},
|
||||||
|
|
||||||
search: function(query) {
|
search: function(query) {
|
||||||
|
if (query == undefined || query == "") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var display_query = query.replace("<", "<").replace(">", ">")
|
||||||
|
$("#querystatus").html("Loading query '" + display_query + "'")
|
||||||
logstash.params.q = query;
|
logstash.params.q = query;
|
||||||
document.location.hash = escape(JSON.stringify(logstash.params));
|
document.location.hash = escape(JSON.stringify(logstash.params));
|
||||||
$("#results").load("/search/ajax", logstash.params);
|
$("#results").load("/search/ajax", logstash.params);
|
||||||
|
|
|
@ -32,17 +32,51 @@ class LogStash::Web::Server < Sinatra::Base
|
||||||
end # '/'
|
end # '/'
|
||||||
|
|
||||||
aget '/search' do
|
aget '/search' do
|
||||||
headers({"Content-Type" => "text/html" })
|
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] != ""
|
if params[:q] and params[:q] != ""
|
||||||
elasticsearch.search(params) do |@results|
|
elasticsearch.search(params) do |@results|
|
||||||
@hits = (@results["hits"]["hits"] rescue [])
|
@hits = (@results["hits"]["hits"] rescue [])
|
||||||
body haml :"search/results", :layout => !request.xhr?
|
begin
|
||||||
|
result_callback.call
|
||||||
|
rescue => e
|
||||||
|
puts e
|
||||||
|
end
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
#@error = "No query given."
|
||||||
@hits = []
|
@hits = []
|
||||||
body haml :"search/results", :layout => !request.xhr?
|
result_callback.call
|
||||||
end
|
end
|
||||||
end
|
end # aget '/search'
|
||||||
|
|
||||||
apost '/search/ajax' do
|
apost '/search/ajax' do
|
||||||
headers({"Content-Type" => "text/html" })
|
headers({"Content-Type" => "text/html" })
|
||||||
|
|
|
@ -31,9 +31,9 @@
|
||||||
last
|
last
|
||||||
- if @hits.length == 0
|
- if @hits.length == 0
|
||||||
- if !params[:q]
|
- 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
|
- else
|
||||||
No results for query '#{params[:q]}'
|
%h3#querystatus No results for query '#{params[:q]}'
|
||||||
- else
|
- else
|
||||||
%table.results
|
%table.results
|
||||||
%tr
|
%tr
|
||||||
|
|
10
lib/logstash/web/views/search/results.txt.erb
Normal file
10
lib/logstash/web/views/search/results.txt.erb
Normal 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
|
||||||
|
%>
|
Loading…
Add table
Add a link
Reference in a new issue