Wiki source code of LuceneSearch

Last modified by Administrator on 2008/06/25 16:43

Show last authors
1 ## ===================
2 ## Lucene search
3 ## ===================
4 ## Inputs : $request.text
5 ## Outputs : $list, $isScored
6 ## ===================
7 ## Uncomment this line to overload the maximum list of wiki where to search (also the one listed in the wikis combobox)
8 ## #set($allwikinamelist = ["xwiki", "wiki1", "wiki2"])
9 ##
10 ## Uncomment this line to overload the wikis where to search
11 ## #set($wikinames = "xwiki,wiki,wiki")
12 ##
13 {pre}
14 #set ($displayUI = true)
15 #if ($request.xpage && $request.xpage == "rdf")
16 #set ($displayUI = false)
17 #end
18 ## ---------------
19 ## Title
20 ## ---------------
21 #if ($displayUI)
22 {/pre}
23 1 Search
24 {pre}
25 #end
26 ## ---------------
27 ## Space filtering
28 ## ---------------
29 #if($request.space && $request.space != "")
30 #set($space = $request.space)
31 #set($reqspace = " AND web:${space}")
32 #else
33 #set($space = "")
34 #end
35 #set($spacesText = {})
36 #set($spaces = $xwiki.spaces)
37 #set($ok = $spacesText.put("All",""))
38 #foreach($space in $spaces)
39 #set($ok = $spacesText.put($space,$space))
40 #end
41 ## ---------------
42 ## Space macros
43 ## ---------------
44 #macro(spaceoption $space $selectspace $spacesText)
45 <option value="$spacesText.get($space)" #if($selectspace == $spacesText.get($space))selected="selected"#end>$space</option>
46 #end
47 #macro(spaceselect $selectspace $spaces $spacesText)
48 <select name="space">
49 #spaceoption("All" $selectspace $spacesText)
50 #foreach($space in $spaces)
51 #spaceoption($space $selectspace $spacesText)
52 #end
53 </select>
54 #end
55 ## ---------------
56 ## Wikis filtering
57 ## ---------------
58 #set($wikinamelist = [])
59 #if(!$wikinames)
60 #set($wikinametable = $request.getParameterValues("wikinames"))
61 #if(!$wikinametable || $request.wikinames == "")
62 #set($wikinametable = $allwikinamelist)
63 #end
64 #set($wikinames = "")
65 #foreach($wikiname in $wikinametable)
66 #set($ok = $wikinamelist.add($wikiname))
67 #if($wikinames != "")
68 #set($wikinames = $wikinames + ",")
69 #end
70 #set($wikinames = $wikinames + $wikiname)
71 #end
72 #else
73 #set($wikinametable = $wikinames.split(", "))
74 #foreach($wikiname in $wikinametable)
75 #set($ok = $wikinamelist.add($wikiname))
76 #end
77 #end
78 ##
79 #if($xwiki.isVirtualMode())
80 #if(!$allwikinamelist)
81 #set($currentwikiname = $context.database)
82 #set($ok = $context.setDatabase($context.mainWikiName))
83 #set($allwikilist = $xwiki.searchDocuments(", BaseObject as obj, StringProperty as prop where doc.fullName=obj.name and obj.className='XWiki.XWikiServerClass' and prop.id.id=obj.id and prop.id.name='server'"))
84 #set($ok = $context.setDatabase($currentwikiname))
85 ##
86 #set($allwikinamelist = [])
87 #foreach($wiki in $allwikilist)
88 #set($ok = $allwikinamelist.add($wiki.substring(17).toLowerCase()))
89 #end
90 #if(!$allwikinamelist.contains($context.mainWikiName))
91 #set($ok = $allwikinamelist.add($context.mainWikiName))
92 #end
93 #end
94 ## ---------------
95 ## Wikis macros
96 ## ---------------
97 #macro(wikinamesoption $wikiname $wikinamelist)
98 <option value="$wikiname" #if($wikinamelist.contains($wikiname))selected="selected"#end>$wikiname</option>
99 #end
100 #macro(wikinamesselect $allwikinamelist $wikinamelist)
101 <select name="wikinames">
102 <option value="" selected="selected">All</option>
103 #foreach($wikiname in $allwikinamelist)
104 #wikinamesoption($wikiname $wikinamelist)
105 #end
106 </select>
107 #end
108 #else
109 #if($wikinames == "")
110 #set($wikinames = $context.mainWikiName)
111 #end
112 #end
113 ## ---------------
114 ## Query preparation
115 ## ---------------
116 #set($text = "$!request.getParameter('text')")
117 #set($query = $text)
118 #set($utext = $util.encodeURI($query))
119 #set($itemsPerPage = "30")
120 ## ---------------
121 ## RSS link
122 ## ---------------
123 #if($space == "")
124 #set($url = $xwiki.getURL($doc.fullName, "view", "xpage=rdf&amp;text=${utext}" ))
125 #else
126 #set($url = $xwiki.getURL($doc.fullName, "view", "xpage=rdf&space=$space&text=${utext}"))
127 #end
128 #if ($displayUI)
129 <div style="float: right;">
130 <a href="$url"><img src="$xwiki.getSkinFile("icons/black-rss.png")" style="border:0px" alt="rss icon" /></a>
131 </div>
132 ## ---------------
133 ## Query form
134 ## ---------------
135 <form action="$doc.name" method="get">
136 <div class="centered">
137 Query <input type="text" name="text" value="$xwiki.getFormEncoded($query)" /> in space #spaceselect($space $spaces $spacesText)#if($xwiki.isVirtualMode()) in wikis #wikinamesselect($util.sort($allwikinamelist) $wikinamelist)#end <input type="submit" value="Search"/>
138 </div>
139 </form>
140 #end
141 {/pre}
142 ## ---------------
143 ## Results processing
144 ## ---------------
145 #if($query != "")
146 #set($lucene = $xwiki.getPlugin("lucene"))
147 #if($lucene)
148 ## ---------------
149 ## Lucene search
150 ## ---------------
151 #set($languages = "default,en,de")
152 #set($firstIndex = $request.getParameter("firstIndex"))
153 #if(!$firstIndex)
154 #set($firstIndex = "1")
155 #end
156 #set($searchresults = $lucene.getSearchResults("${query}$!{reqspace}", $wikinames, $languages, $xwiki))
157 #set($results = $searchresults.getResults($firstIndex,$itemsPerPage))
158 #if($searchresults.getHitcount()>0)
159 ## -----------------
160 ## Results numbers
161 ## -----------------
162 #set($lastIndex=$searchresults.getEndIndex($firstIndex, $itemsPerPage))
163 #if ($displayUI)
164 #if($searchresults.getHitcount()==1)
165 One result:
166 #else
167 Results $firstIndex - $lastIndex of ${searchresults.getHitcount()}:
168 #end
169 #end
170 ## ---------------
171 ## Previous page
172 ## ---------------
173 #if($searchresults.hasPrevious($firstIndex) && $displayUI)
174 #set($linkfirstIndex = $searchresults.getPreviousIndex($firstIndex,$itemsPerPage))
175 #set($link = "${doc.name}?text=${query}&firstIndex=${linkfirstIndex}")
176 {pre}
177 <a href="$link"><img src="${doc.getAttachmentURL("previous.png")}" alt="previous" />previous page</a>
178 {/pre}
179 #end
180 ## -------------
181 ## Next page
182 ## -------------
183 #if($searchresults.hasNext($firstIndex,$itemsPerPage) && $displayUI)
184 #set($linkfirstIndex = $searchresults.getNextIndex($firstIndex,$itemsPerPage))
185 #set($link = "${doc.name}?text=${query}&firstIndex=${linkfirstIndex}")
186 {pre}
187 <a href="$link"><img src="${doc.getAttachmentURL("next.png")}" alt="next" />next page</a>
188 {/pre}
189 #end
190 ## -----------------
191 ## Display results
192 ## -----------------
193 #if ($request.xpage && $request.xpage == "rdf")
194 #set ($baseurl = "http://${request.serverName}")
195 #set ($description = "RSS feed for search on '$text'")
196 #set ($list = $util.arrayList)
197 #foreach ($item in $results)
198 #if ($item.type != "attachment")
199 #set ($ok = $list.add(0, "${item.web}.${item.name}"))
200 #end
201 #end
202 #set ($ok = $response.setContentType("text/xml"))
203 #includeInContext("XWiki.WebRssCode")
204 #else
205 #set ($list = $results)
206 #set ($isScored = true)
207 #includeInContext("XWiki.Results")
208 #end
209 #end
210 #else
211 #error("Lucene plugin not found. Make sure it's defined in your xwiki.cfg file.")
212 #end
213 #end
214 #if ($displayUI)
215 {pre}
216 ## ---------------
217 ## Rebuild processing
218 ## ---------------
219 #set($lucene = $xwiki.getPlugin("lucene"))
220 #if($lucene)
221 #set($doRebuild = "$!{request.getParameter('rebuild')}")
222 #if($doRebuild == "yes")
223 #set($documentCount = $lucene.rebuildIndex())
224 #if(${documentCount} >= 0)
225 {/pre}#info("Started index rebuild.\\
226 Will take some time depending on the number of pages/attachments."){pre}
227 #elseif(${documentCount} == -1)
228 #error("You must have administrator rights to rebuild the index.")
229 #elseif(${documentCount} == -2)
230 #error("Another rebuild is in progress.")
231 #else
232 #error("Index rebuild failed.")
233 #end
234 #else ## !doRebuild
235 ## ---------------
236 ## Queue & rebuild option
237 ## ---------------
238 #set ($queuesize = $xwiki.lucene.getQueueSize())
239 #if ($queuesize > 2)
240 #set($info1 = "Lucene is currently building its index, ${queuesize} documents in queue.")
241 #else
242 #set($info1 = "Lucene index is up to date.")
243 #end
244 #if($hasAdmin)
245 #set($info2 = " [Rebuild the Lucene index>${doc.space}.${doc.name}?rebuild=yes].")
246 #else
247 #set($info2 = "")
248 #end
249 {/pre}#info("${info1}${info2}"){pre}
250 #end ## doRebuild
251 ## ---------------
252 ## Comment
253 ## ---------------
254 {/pre}
255 #warning("This is the new experimental Lucene search engine. \\
256 You can still use the XWiki [default search engine>WebSearch?text=$utext].")
257 #else ## !lucene exists
258 #warning("The Lucene plugin is not enabled. You can use the XWiki [default search engine>WebSearch?text=$utext].")
259 #end ## lucene exists
260 #end ## displayUI