1
+ <cfparam name =" URL.q" default =" *:*" >
2
+ <cfparam name =" URL.page" default =" 1" >
3
+ <cfparam name =" URL.perPage" default =" 10" >
4
+
5
+ <cfset sampleSolrInstance = createObject (" component" ," components.cfsolrlib" ).init (APPLICATION .javaloader ," localhost" ," 8983" ," /solr" ) />
6
+
7
+ <cfif NOT isNumeric (URL .page ) OR abs (round (URL .page )) NEQ URL .page >
8
+ <cfset URL .page = 1 >
9
+ </cfif >
10
+
11
+ <cfif NOT isNumeric (URL .perPage ) OR abs (round (URL .perPage )) NEQ URL .perPage >
12
+ <cfset URL .perPage = 10 >
13
+ </cfif >
14
+
15
+ <cfset start = ((URL .page - 1 ) * URL .perPage ) >
16
+ <cfset rows = URL .perPage >
17
+
18
+ <cfset cleanedUrlQuery = " ?" & reReplaceNoCase (cgi .query_string ," &*\bpage=\d+" ," " ," all" ) >
19
+
20
+ <cfset facetFields = arrayNew (1 ) />
21
+ <cfset arrayAppend (facetFields ," cat" ) />
22
+ <cfset arrayAppend (facetFields ," author_s" ) />
23
+ <cfset arrayAppend (facetFields ," availability_s" ) />
24
+
25
+ <cfset facetFiltersArray = arrayNew (1 ) >
26
+
27
+ <cfif isDefined (" URL.fq" ) >
28
+ <cfset facetFiltersArray = listToArray (urlDecode (URL .fq )) >
29
+ </cfif >
30
+
31
+ <cfset searchResponse = sampleSolrInstance .search (q = URL .q ,start = start ,rows = rows ,facetFields = facetFields ,facetFilters = facetFiltersArray ,facetMinCount = 1 ) />
32
+
33
+ <cfif isStruct (searchResponse ) >
34
+ <cfset searchSuccess = TRUE >
35
+ <cfelse >
36
+ <cfset searchSuccess = FALSE >
37
+ </cfif >
38
+
39
+ <!doctype html>
40
+ <html lang =" en" >
41
+ <head >
42
+ <meta charset =" UTF-8" >
43
+ <title >CFSolrLib 3.0 | Faceted Search Example</title >
44
+ <link href =" //netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel =" stylesheet" >
45
+ </head >
46
+ <body >
47
+ <cfoutput >
48
+ <div class =" container" >
49
+ <div class =" row" >
50
+ <div class =" span12" >
51
+ <h1 >Faceted Search Example</h1 >
52
+ <p class =" lead" >Here is a simple faceted search example.</p >
53
+ <form action =" " method =" GET" class =" form-search form-inline" >
54
+ <div class =" input-append" >
55
+ <input type =" text" name =" q" value =" #URL .q #" class =" input-medium search-query" placeholder =" Search..." ><button type =" submit" class =" btn" >Search</button >
56
+ </div >
57
+ </form >
58
+ <p >Other Search Examples: <a href =" ?q=*:*" >*:*</a > | <a href =" ?q=Charcoal" >Charcoal</a > | <a href =" ?q=Media" >Media</a ></p >
59
+ </div >
60
+ </div >
61
+ <div class =" row" >
62
+ <div class =" span3" >
63
+ <cfif searchSuccess AND structKeyExists (searchResponse ," facetFields" ) AND isArray (searchResponse .facetFields ) >
64
+ <cfoutput >
65
+ <cfset facetDisplayName = structNew () >
66
+ <cfset facetDisplayName [" cat" ] = " Media" >
67
+ <cfset facetDisplayName [" Author_s" ] = " Artist" >
68
+ <cfset facetDisplayName [" Availability_s" ] = " Availability" >
69
+ <cfloop index =" a" array =" #searchResponse .facetFields #" >
70
+ <cfif isArray (a .getValues ()) >
71
+ <h4 style =" text-transform:capitalize;" >#facetDisplayName [a .getName ()] #</h4 >
72
+ <ul >
73
+ <cfloop index =" b" from =" 1" to =" #arrayLen (a .getValues ()) #" >
74
+ <cfif len (a .getValues ()[b ].getName ()) >
75
+ <cfset filterString = " &fq=" & a .getName () & urlEncodedFormat (" :("" " & a .getValues ()[b ].getName ()& " "" )" ) >
76
+ <li ><a href =" #cleanedUrlQuery ##filterString #" >#a .getValues ()[b ].getName () #</a > <small class =" muted" >(#a .getValues ()[b ].getCount () #)</small >
77
+ <cfif isDefined (" URL.fq" ) and findNoCase (filterString ,cleanedUrlQuery ) > <a href =" #replaceNoCase (cleanedUrlQuery ,filterString ," " ) #" class =" label label-important" style =" float:none;" >× </a >
78
+ </cfif >
79
+ </li >
80
+ </cfif >
81
+ </cfloop >
82
+ </ul >
83
+ </cfif >
84
+ </cfloop >
85
+ </cfoutput >
86
+
87
+ </cfif >
88
+ </div >
89
+ <div class =" span9" >
90
+ <cfif searchSuccess AND isArray (searchResponse .results ) >
91
+ <table class =" table table-bordered table-hover" >
92
+ <thead >
93
+ <tr >
94
+ <th >ID</th >
95
+ <th >Title</th >
96
+ <th >Artist</th >
97
+ <th >Media</th >
98
+ <th >Availability</th >
99
+ </tr >
100
+ </thead >
101
+ <tbody >
102
+ <cfloop array =" #searchResponse .results #" index =" currentResult" >
103
+ <tr >
104
+ <td >#currentResult .id #</td >
105
+ <td ><cfif isDefined (" currentResult.title" ) >#currentResult .title #</cfif ></td >
106
+ <td ><cfif isDefined (" currentResult.Author_s" ) >#currentResult .Author_s #</cfif ></td >
107
+ <td ><cfif isDefined (" currentResult.cat" ) >#arrayToList (currentResult .cat ) #</cfif ></td >
108
+ <td ><cfif isDefined (" currentResult.Availability_s" ) >#currentResult .Availability_s #</cfif ></td >
109
+ </tr >
110
+ </cfloop >
111
+ </tbody >
112
+ </table >
113
+
114
+
115
+ <!--- Begin Pagination --->
116
+ <cfif searchResponse .totalResults GT rows >
117
+ <div class =" pagination" >
118
+ <ul >
119
+ <cfoutput >
120
+ <cfset totalPages = Ceiling (searchResponse .totalResults / url .perPage ) >
121
+ <cfif url .page GT 1 >
122
+ <cfset prevLink = cleanedUrlQuery >
123
+ <cfif url .page GT 2 >
124
+ <cfset prevLink = cleanedUrlQuery & " &page=" & (url .page - 1 ) >
125
+ </cfif >
126
+ <li ><a href =" #prevLink #" >« </a ></li >
127
+ <cfelse >
128
+ <li class =" disabled" ><a href =" ## " >« </a ></li >
129
+ </cfif >
130
+
131
+ <cfset pageCount = 1 >
132
+ <cfset pageLink = 1 >
133
+
134
+ <cfloop index =" c" from =" 1" to =" #totalPages #" >
135
+ <cfif c EQ url .page >
136
+ <li class =" active" ><a href =" ## " >#c #</a ></li >
137
+ <cfelseif c NEQ 1 >
138
+ <li ><a href =" #cleanedUrlQuery #& page=#c #" >#c #</a ></li >
139
+ <cfelse >
140
+ <li ><a href =" #cleanedUrlQuery #" >#c #</a ></li >
141
+ </cfif >
142
+ </cfloop >
143
+
144
+ <cfif url .page LT totalPages >
145
+ <cfset nextLink = cleanedUrlQuery & " &page=" & (url .page + 1 ) >
146
+ <li ><a href =" #nextLink #" >» </a ></li >
147
+ <cfelse >
148
+ <li class =" disabled" ><a href =" ## " >» </a ></li >
149
+ </cfif >
150
+ </cfoutput >
151
+ </ul >
152
+ </div >
153
+ </cfif >
154
+ <!--- End Pagination --->
155
+
156
+ </cfif >
157
+ </div >
158
+ </div >
159
+ </div >
160
+ </cfoutput >
161
+ </body >
162
+ </html >
0 commit comments