Skip to content

Commit d4e474d

Browse files
author
Anusha Ranganathan
committed
Added an advanced search controller
1 parent fcb6ecc commit d4e474d

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed

rdfdatabank/controllers/search.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,3 +352,85 @@ def detailed(self, query=None, additional_fields=[]):
352352
c.returned_facets[facet].append((keys[index],values[index]))
353353

354354
return render('/search.html')
355+
356+
357+
def advanced(self):
358+
359+
c.q = "*:*"
360+
c.typ = 'all'
361+
362+
# Search controls
363+
format = 'html'
364+
c.sort = 'score desc'
365+
c.sort_text = c.sort_options[c.sort]
366+
367+
c.chosen_fields = []
368+
c.chosen_fields.extend(c.search_fields)
369+
370+
c.fields_to_facet = []
371+
c.fields_to_facet.extend(c.facetable_fields)
372+
373+
c.facet_limit = 10
374+
375+
c.chosen_facets = {}
376+
377+
query_filter = ""
378+
379+
#Setup to capture all the url parameters needed to regenerate this search
380+
c.search = {}
381+
filter_url = ""
382+
383+
c.truncate = 450
384+
c.start = 0
385+
c.rows = 25
386+
c.search['truncate'] = c.truncate
387+
c.search['type'] = c.typ
388+
389+
solr_params = {}
390+
391+
if c.q:
392+
solr_params['q'] = c.q.encode('utf-8')+query_filter
393+
solr_params['wt'] = 'json'
394+
solr_params['fl'] = ','.join(c.chosen_fields)
395+
solr_params['rows'] = c.rows
396+
solr_params['start'] = c.start
397+
if c.sort:
398+
solr_params['sort'] = c.sort
399+
if c.fields_to_facet:
400+
solr_params['facet'] = 'true'
401+
solr_params['facet.limit'] = c.facet_limit
402+
solr_params['facet.mincount'] = 1
403+
solr_params['facet.field'] = []
404+
for facet in c.fields_to_facet:
405+
solr_params['facet.field'].append(facet)
406+
407+
solr_response = ag.solr.raw_query(**solr_params)
408+
409+
c.add_facet = u"%ssearch/detailed?q=%s&" % (ag.root, c.q.encode('utf-8'))
410+
c.add_facet = c.add_facet + urlencode(c.search) + filter_url
411+
412+
if not solr_response:
413+
# FAIL - do something here:
414+
c.message = 'Sorry, either that search "%s" resulted in no matches, or the search service is not functional.' % c.q
415+
h.redirect_to(controller='/search', action='index')
416+
417+
search = json.loads(solr_response)
418+
419+
numFound = search['response'].get('numFound',None)
420+
try:
421+
c.numFound = int(numFound)
422+
except:
423+
c.numFound = 0
424+
c.docs = search['response'].get('docs',None)
425+
426+
if c.fields_to_facet:
427+
c.returned_facets = {}
428+
for facet in search['facet_counts']['facet_fields']:
429+
facet_list = search['facet_counts']['facet_fields'][facet]
430+
keys = facet_list[::2]
431+
values = facet_list[1::2]
432+
c.returned_facets[facet] = []
433+
for index in range(len(keys)):
434+
c.returned_facets[facet].append((keys[index],values[index]))
435+
436+
return render('/search_advanced.html')

rdfdatabank/lib/search_term.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,9 @@ def get_all_facet_fields(self):
197197
"f_source",
198198
"f_subject"
199199
]
200+
201+
def get_range_facet_fields(self):
202+
return [
203+
"f_embargoedUntilDate",
204+
"f_publicationDate"
205+
]
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# -*- coding: utf-8 -*-
2+
<%inherit file="/base.html" />
3+
4+
<%def name="head_tags()">
5+
<title> Advanced Search</title>
6+
7+
% if c.search and c.q:
8+
<link rel="alternate" type="application/atom+xml" href="${c.add_facet}&format=atom&rows=c.rows&sort=score+desc" title="Search results for \"${c.q}\""/>
9+
% endif
10+
11+
</%def>
12+
<%def name="header()">
13+
</%def>
14+
<%def name="footer()">
15+
</%def>
16+
17+
<div id="facet_wrapper">
18+
<% count = 0 %>
19+
<div id="facet_container_adv">
20+
% if c.returned_facets:
21+
% for facet in c.returned_facets:
22+
<% count = count + 1 %>
23+
% if c.returned_facets[facet] and len(c.returned_facets[facet]) > 1:
24+
<div class="facet_results">
25+
<div id="fs${count}" class="subheading"><a id="fh${count}" href="#" >${c.field_names[facet]}</a></div>
26+
<div id="fl${count}" class="facetlist">
27+
<ul>
28+
% for result,value in c.returned_facets[facet]:
29+
<li><span class="label">
30+
<%
31+
from urllib import quote
32+
try:
33+
res = quote(result)
34+
except:
35+
res = result
36+
if len(result) > 40:
37+
res_label = result[:40] + '...'
38+
else:
39+
res_label = result
40+
%>
41+
<a href="${c.add_facet + u'&filter%s="%s"&rows=%d&sort=%s' % (facet, res, c.rows, c.sort)}" title="${result}">${res_label}
42+
<span class="value">(${value})</span> <a/></span></li>
43+
% endfor
44+
</ul>
45+
</div>
46+
</div>
47+
% endif
48+
% endfor
49+
</div>
50+
% endif
51+
</div>
52+
</div>
53+
54+
<![if !IE]>
55+
<script type="text/javascript">
56+
$(document).ready(function() {
57+
58+
function fhAdjust(){
59+
//Make sure facets do not stick beneath footer
60+
$navh = $('#facet_wrapper').height();
61+
$navh = $navh + 211;
62+
$wrah = $('#wrapper').height();
63+
if ($navh > $wrah) {
64+
$('#wrapper').css("height", $navh + 100);
65+
} else {
66+
$mainh = $('#main').height();
67+
$mainh = $mainh + 91;
68+
if ($mainh > $navh){
69+
$('#wrapper').css("height", $mainh + 100);
70+
} else {
71+
$('#wrapper').css("height", $navh + 100);
72+
}
73+
}
74+
}
75+
76+
function bindItem(ind){
77+
$('#fh' + ind).click(function(event) {
78+
event.preventDefault();
79+
if ($('#fl' + ind).hasClass("closed")){
80+
$('#fl' + ind).show();
81+
$('#fl' + ind).removeClass("closed");
82+
$('#fh' + ind).css("background-image", "url('/fminus.png')");
83+
$('#fs' + ind).css("background-color", "#EEE");
84+
} else {
85+
$('#fl' + ind).hide();
86+
$('#fl' + ind).addClass("closed");
87+
$('#fh' + ind).css("background-image", "url('/fplus.png')");
88+
$('#fs' + ind).css("background-color", "#BBB");
89+
}
90+
fhAdjust();
91+
});
92+
}
93+
94+
for (i=1;i<=${count};i++){
95+
//Facet Toggle
96+
$('#fl'+i).hide();
97+
$('#fl'+i).addClass("closed");
98+
bindItem(i);
99+
}
100+
101+
});
102+
</script>
103+
<![endif]>
104+
105+
<!--[if lte IE 8]>
106+
<script type="text/javascript">
107+
$(document).ready(function() {
108+
109+
function fhAdjust(){
110+
//Make sure facets do not stick beneath footer
111+
$navh = $('#facet_wrapper').height();
112+
$navh = $navh + 211;
113+
$wrah = $('#wrapper').height();
114+
if ($navh > $wrah) {
115+
$('#wrapper').css("height", $navh + 100);
116+
} else {
117+
$mainh = $('#main').height();
118+
$mainh = $mainh + 91;
119+
if ($mainh > $navh){
120+
$('#wrapper').css("height", $mainh + 100);
121+
} else {
122+
$('#wrapper').css("height", $navh + 100);
123+
}
124+
}
125+
}
126+
127+
function bindItem(ind){
128+
$('#fh' + ind).click(function(event) {
129+
event.preventDefault();
130+
if ($('#fl' + ind).hasClass("closed")){
131+
$('#fl' + ind).show();
132+
$('#fl' + ind).removeClass("closed");
133+
$('#fh' + ind).css("background-image", "url('/fminus.png')");
134+
$('#fs' + ind).css("background-color", "#EEE");
135+
} else {
136+
$('#fl' + ind).hide();
137+
$('#fl' + ind).addClass("closed");
138+
$('#fh' + ind).css("background-image", "url('/fplus.png')");
139+
$('#fs' + ind).css("background-color", "#BBB");
140+
}
141+
fhAdjust();
142+
});
143+
}
144+
145+
for (i=1;i<=${count};i++){
146+
//Facet Toggle
147+
$('#fl'+i).addClass("closed");
148+
bindItem(i);
149+
}
150+
151+
});
152+
</script>
153+
<![endif]-->

0 commit comments

Comments
 (0)