- Support prev and next links in search

This commit is contained in:
Jordan Sissel 2010-11-16 09:50:10 +00:00
parent 2e416f34cb
commit a8ce21e753
3 changed files with 44 additions and 12 deletions

View file

@ -81,6 +81,21 @@
}
});
$("a.pager").live("click", function() {
var href = $(this).attr("href");
var params = href.replace(/^[^?]*\?/, "").split("&")
for (var p in params) {
var a = params[p].split("=");
console.log(a)
var key = a[0]
var value = a[1]
logstash.params[key] = value
console.log("Setting " + key + " => " + value)
}
logstash.search(logstash.params.q)
return false;
});
$("a.querychanger").live("click", function() {
var href = $(this).attr("href");
var re = new RegExp("[&?]q=([^&]+)");

View file

@ -46,8 +46,8 @@ class LogStash::Web::Server < Sinatra::Base
apost '/search/ajax' do
headers({"Content-Type" => "text/html" })
params[:count] = (params[:count] or 50).to_i
params[:offset] = (params[:offset] or 0).to_i
count = params["count"] = (params["count"] or 50).to_i
offset = params["offset"] = (params["offset"] or 0).to_i
elasticsearch.search(params) do |@results|
@hits = (@results["hits"]["hits"] rescue [])
@total = (@results["hits"]["total"] rescue 0)
@ -56,17 +56,30 @@ class LogStash::Web::Server < Sinatra::Base
@graphpoints << [entry["key"], entry["count"]]
end
if params[:count] and params[:offset]
if @total > (params[:count] + params[:offset])
@result_end = params[:count] + params[:offset]
if count and offset
if @total > (count + offset)
@result_end = (count + offset)
else
@result_end = @total
end
@result_start = params[:offset]
@result_start = offset
end
if count + offset < @total
next_params = params.clone
next_params["offset"] = [offset + count, @total - count].min
@next_href = "?" + next_params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&")
end
if offset - count > 0
prev_params = params.clone
prev_params["offset"] = [offset - count, 0].max
@prev_href = "?" + prev_params.collect { |k,v| [URI.escape(k.to_s), URI.escape(v.to_s)].join("=") }.join("&")
end
body haml :"search/ajax", :layout => !request.xhr?
end
end
end # elasticsearch.search
end # apost '/search/ajax'
end # class LogStashWeb

View file

@ -14,9 +14,13 @@
%strong
Results #{@result_start} - #{@result_end} of #{@total}
|
%a
- if @prev_href
%a.pager{ :href => @prev_href }
prev
%a
- if @next_href
|
- if @next_href
%a.pager{ :href => @next_href }
next
%ul.results
- @hits.reverse.each do |hit|