1 # <a name="TWiki Formatted Search"></a> TWiki Formatted Search
3 _Inline search feature allows flexible formatting of search result_
5 The default output format of a <code>[[%SEARCH{...}%|Main/VarSEARCH]]</code> is a table consisting of topic names and topic summaries. Use the `format="..."` parameter to customize the search result. The format parameter typically defines a bullet or a table row containing variables, such as `%SEARCH{ "food" format="| $topic | $summary |" }%`. See <code>[[%SEARCH{...}%|Main/VarSEARCH]]</code> for other search parameters, such as `separator=""`.
9 <li><a href="#TWiki Formatted Search"> TWiki Formatted Search</a><ul>
10 <li><a href="#Syntax"> Syntax</a><ul>
11 <li><a href="#1. <code>header="..."</code> parameter"> 1. header="..." parameter</a></li>
12 <li><a href="#2. <code>format="..."</code> parameter"> 2. format="..." parameter</a></li>
15 <li><a href="#Examples"> Examples</a><ul>
16 <li><a href="#Bullet list showing topic name a"> Bullet list showing topic name and summary</a></li>
21 <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
24 <li><a href="#Table showing form field values"> Table showing form field values of topics with a form</a></li>
25 <li><a href="#Extract some text from a topic u"> Extract some text from a topic using regular expression</a></li>
30 <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
33 <li><a href="#Nested Search"> Nested Search</a></li>
38 <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
41 <li><a href="#Most recently changed pages"> Most recently changed pages</a></li>
46 <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
49 <li><a href="#Search with conditional output"> Search with conditional output</a></li>
54 <li><a href="#TWiki Installation Error">TWiki Installation Error</a><ul>
57 <li><a href="#Embedding search forms to return"> Embedding search forms to return a formatted result</a></li>
62 <li><a href="#TWiki Installation Error">TWiki Installation Error</a></li>
66 ## <a name="Syntax"></a> Syntax
68 Two parameters can be used to specify a customized search result:
70 ### <a name="1. <code>header="..."</code> parameter"></a> 1. `header="..."` parameter
72 Use the header parameter to specify the header of a search result. It should correspond to the format of the format parameter. This parameter is optional. <br /> Example: `header="| *Topic:* | *Summary:* |"`
74 Variables that can be used in the header string:
76 <table border="1" cellpadding="0" cellspacing="0">
78 <th bgcolor="#99CCCC"><strong> Name: </strong></th>
79 <th bgcolor="#99CCCC"><strong> Expands To: </strong></th>
82 <td><code>$web</code></td>
83 <td> Name of the web </td>
87 ### <a name="2. <code>format="..."</code> parameter"></a> 2. `format="..."` parameter
89 Use the format parameter to specify the format of one search hit. <br /> Example: `format="| $topic | $summary |"`
91 Variables that can be used in the format string:
93 <table border="1" cellpadding="0" cellspacing="0">
95 <th bgcolor="#99CCCC"><strong> Name: </strong></th>
96 <th bgcolor="#99CCCC"><strong> Expands To: </strong></th>
99 <td><code>$web</code></td>
100 <td> Name of the web </td>
103 <td><code>$topic</code></td>
104 <td> Topic name </td>
107 <td><code>$topic(20)</code></td>
108 <td> Topic name, "<tt>- </tt>" hyphenated each 20 characters </td>
111 <td><code>$topic(30, -<br />)</code></td>
112 <td> Topic name, hyphenated each 30 characters with separator "<tt>-<br /></tt>" </td>
115 <td><code>$topic(40, ...)</code></td>
116 <td> Topic name, shortended to 40 characters with "<tt>...</tt>" indication </td>
119 <td><code>$parent</code></td>
120 <td> Name of parent topic; empty if not set </td>
123 <td><code>$parent(20)</code></td>
124 <td> Name of parent topic, same hyphenation/shortening like <code>$topic()</code></td>
127 <td><code>$text</code></td>
128 <td> Formatted topic text. In case of a <code>multiple="on"</code> search, it is the line found for each search hit. </td>
131 <td><code>$locked</code></td>
132 <td> LOCKED flag (if any) </td>
135 <td><code>$date</code></td>
136 <td> Time stamp of last topic update, e.g. <code>29 Jun 2010 - 16:07</code></td>
139 <td><code>$isodate</code></td>
140 <td> Time stamp of last topic update, e.g. <code>2010-06-29T16:07Z</code></td>
143 <td><code>$rev</code></td>
144 <td> Number of last topic revision, e.g. <code>4</code></td>
147 <td><code>$username</code></td>
148 <td> Login name of last topic update, e.g. <code>jsmith</code></td>
151 <td><code>$wikiname</code></td>
152 <td> Wiki user name of last topic update, e.g. <code>JohnSmith</code></td>
155 <td><code>$wikiusername</code></td>
156 <td> Wiki user name of last topic update, like <code>%USERSWEB%.JohnSmith</code></td>
159 <td><code>$createdate</code></td>
160 <td> Time stamp of topic revision 1 </td>
163 <td><code>$createusername</code></td>
164 <td> Login name of topic revision 1, e.g. <code>jsmith</code></td>
167 <td><code>$createwikiname</code></td>
168 <td> Wiki user name of topic revision 1, e.g. <code>JohnSmith</code></td>
171 <td><code>$createwikiusername</code></td>
172 <td> Wiki user name of topic revision 1, e.g. <code>%USERSWEB%.JohnSmith</code></td>
175 <td><code>$summary</code></td>
176 <td> Topic summary, just the plain text, all formatting and line breaks removed; up to 162 characters </td>
179 <td><code>$summary(50)</code></td>
180 <td> Topic summary, up to 50 characters shown </td>
183 <td><code>$summary(showvarnames)</code></td>
184 <td> Topic summary, with <code>%ALLTWIKI{...}%</code> variables shown as <code>ALLTWIKI{...}</code></td>
187 <td><code>$summary(noheader)</code></td>
188 <td> Topic summary, with leading <code>---+ headers</code> removed%BR% <strong><em>Note:</em></strong> The tokens can be combined, for example <code>$summary(100, showvarnames, noheader)</code></td>
191 <td><code>$changes</code></td>
192 <td> Summary of changes between latest rev and previous rev </td>
195 <td><code>$changes(n)</code></td>
196 <td> Summary of changes between latest rev and rev n </td>
199 <td><code>$formname</code></td>
200 <td> The name of the form attached to the topic; empty if none </td>
203 <td><code>$formfield(name)</code></td>
204 <td> The field value of a form field; for example, <code>$formfield(TopicClassification)</code> would get expanded to <code>PublicFAQ</code>. This applies only to topics that have a [[Main/TWikiForms]]</td>
207 <td><code>$formfield(name, 10)</code></td>
208 <td> Form field value, "<tt>- </tt>" hyphenated each 10 characters </td>
211 <td><code>$formfield(name, 20, -<br />)</code></td>
212 <td> Form field value, hyphenated each 20 characters with separator "<tt>-<br /></tt>" </td>
215 <td><code>$formfield(name, 30, ...)</code></td>
216 <td> Form field value, shortended to 30 characters with "<tt>...</tt>" indication </td>
219 <td><code>$pattern(reg-exp)</code></td>
220 <td> A regular expression pattern to extract some text from a topic (does not search meta data; use <code>$formfield</code> instead). In case of a <code>multiple="on"</code> search, the pattern is applied to the line found in each search hit.%BB% Specify a [[Main/RegularExpression]] that covers the whole text (topic or line), which typically starts with <code>.*</code>, and must end in <code>.*</code> %BB% Put text you want to keep in parenthesis, like <code>$pattern(.*?(from here.*?to here).*)</code> %BB% Example: <code>$pattern(.*?\*.*?Email\:\s*([^\n\r]+).*)</code> extracts the e-mail address from a bullet of format <code>* Email: ...</code> %BB% This example has non-greedy <code>.*?</code> patterns to scan for the first occurance of the Email bullet; use greedy <code>.*</code> patterns to scan for the last occurance %BB% Limitation: Do not use <code>.*)</code> inside the pattern, e.g. <code>$pattern(.*foo(.*)bar.*)</code> does not work, but <code>$pattern(.*foo(.*?)bar.*)</code> does %BB% Note: Make sure that the integrity of a web page is not compromised; for example, if you include an HTML table make sure to include everything including the table end tag </td>
223 <td><code>$count(reg-exp)</code></td>
224 <td> Count of number of times a regular expression pattern appears in the text of a topic (does not search meta data). Follows guidelines for use and limitations outlined above under <code>$pattern(reg-exp)</code>. Example: <code>$count(.*?(---[+][+][+][+]) .*)</code> counts the number of <H4> headers in a page. </td>
228 ## <a name="Examples"></a> Examples
230 Here are some samples of formatted searches. The [[SearchPatternCookbook]] has other examples, such as [[creating a picklist of usernames|Main/SearchPatternCookbook#SearchUsernames]], [[searching for topic children|Main/SearchPatternCookbook#SearchTopicChildren]] and more.
232 <a name="SearchBulletList"></a>
234 ### <a name="Bullet list showing topic name a"></a> Bullet list showing topic name and summary
238 `%SEARCH{ "FAQ" scope="topic" nosearch="on" nototal="on" header=" * *Topic: Summary:*" format=" * [[$topic]]: $summary" }%`
242 # <a name="TWiki Installation Error"></a> TWiki Installation Error
244 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
246 ### <a name="Table showing form field values"></a><a name="Table showing form field values "></a> Table showing form field values of topics with a form
248 In a web where there is a form that contains a `TopicClassification` field, an `OperatingSystem` field and an `OsVersion` field we could write:
250 `| *Topic:* | *OperatingSystem:* | *OsVersion:* |`<br />`%SEARCH{ "[T]opicClassification.*?value=\"[P]ublicFAQ\"" scope="text" type="regex" nosearch="on" nototal="on" format="| [[$topic]] | $formfield(OperatingSystem) | $formfield(OsVersion) |" }%`
254 <table border="1" cellpadding="1" cellspacing="0">
256 <th bgcolor="#99CCCC"><strong>Topic:</strong></th>
257 <th bgcolor="#99CCCC"><strong>OperatingSystem:</strong></th>
258 <th bgcolor="#99CCCC"><strong>OsVersion:</strong></th>
261 <td>[[Sandbox/IncorrectDllVersionW32PTH10DLL]]</td>
262 <td>[[Sandbox/OsWin]]</td>
266 <td>[[Sandbox/WinDoze95Crash]]</td>
267 <td>[[Sandbox/OsWin]]</td>
272 ### <a name="Extract some text from a topic u"></a> Extract some text from a topic using regular expression
276 `%SEARCH{ "__Back to\:__ TWikiFAQ" scope="text" type="regex" nosearch="on" nototal="on" header="TWiki FAQs:" format=" * $pattern(.*?FAQ\:[\n\r]*([^\n\r]+).*) [[$topic][Answer...]]" }%`
280 # <a name="TWiki Installation Error"></a> TWiki Installation Error
282 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
284 ### <a name="Nested Search"></a> Nested Search
286 Search can be nested. For example, search for some topics, then form a new search for each topic found in the first search. The idea is to build the nested search string using a formatted search in the first search.
288 Here is an example. Let's search for all topics that contain the word "culture" (first search), and let's find out where each topic found is linked from (second search).
291 - `%SEARCH{ "culture" format=" * $topic is referenced by: (list all references)" nosearch="on" nototal="on" }%`
292 - Second search. For each hit we want this search:
293 - `%SEARCH{ "(topic found in first search)" format="$topic" nosearch="on" nototal="on" separator=", " }%`
294 - Now let's nest the two. We need to escape the second search, e.g. the first search will build a valid second search string. Note that we escape the second search so that it does not get evaluated prematurely by the first search:
295 - Use `$percnt` to escape the leading percent of the second search
296 - Use `\"` to escape the double quotes
297 - Use `$dollar` to escape the `$` of `$topic`
298 - Use `$nop` to escape the `}%` sequence
302 `%SEARCH{ "culture" format=" * $topic is referenced by:$n * $percntSEARCH{ \"$topic\" format=\"$dollartopic\" nosearch=\"on\" nototal=\"on\" separator=\", \" }$nop%" nosearch="on" nototal="on" }%`
306 # <a name="TWiki Installation Error"></a> TWiki Installation Error
308 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
310 **_Note:_** Nested search can be slow, especially if you nest more then 3 times. Nesting is limited to 16 levels. For each new nesting level you need to "escape the escapes", e.g. write `$dollarpercntSEARCH{` for level three, `$dollardollarpercntSEARCH{` for level four, etc.
312 ### <a name="Most recently changed pages"></a> Most recently changed pages
316 `%SEARCH{ "\.*" scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on" format="| [[$topic]] | $wikiusername | $date |" limit="7" }%`
320 # <a name="TWiki Installation Error"></a> TWiki Installation Error
322 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
324 ### <a name="Search with conditional output"></a> Search with conditional output
326 A regular expression search is flexible, but there are limitations. For example, you cannot show all topics that are up to exactly one week old, or create a report that shows all records with invalid form fields or fields within a certain range, etc. You need some additional logic to format output based on a condition:
328 1. Specify a search which returns more hits then you need
329 2. For each search hit apply a spreadsheet formula to determine if the hit is needed
330 3. If needed, format and output the result
331 4. Else supress the search hit
333 This requires the TWiki:Plugins.SpreadSheetPlugin. The following example shows all topics that are up to exactly one week old.
337 `%CALC{$SET(weekold, $TIMEADD($TIME(), -7, day))}%` %BR% `%SEARCH{ "." scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on" format="$percntCALC{$IF($TIME($date) < $GET(weekold), <nop>, | [[$topic]] | $wikiusername | $date | $rev |)}$percnt" limit="100" }%`
339 - The first line sets the `weekold` variable to the serialized date of exactly one week ago
340 - The SEARCH has a deferred CALC. The `$percnt` makes sure that the CALC gets executed once for each search hit
341 - The CALC compares the date of the topic with the `weekold` date
342 - If topic is older, a `<nop>` is returned, which gets removed at the end of the TWiki rendering process
343 - Otherwise, the search hit is formatted and returned
347 # <a name="TWiki Installation Error"></a> TWiki Installation Error
349 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
351 ### <a name="Embedding search forms to return"></a> Embedding search forms to return a formatted result
353 Use an HTML form and an embedded formatted search on the same topic. You can link them together with an `%URLPARAM{"..."}%` variable. Example:
357 <form action="%SCRIPTURLPATH{"view"}%/%WEB%/%TOPIC%">
359 <input type="text" name="q" size="32" value="%URLPARAM{"q"}%" /> <input type="submit" class="twikiSubmit" value="Search" />
362 %SEARCH{ search="%URLPARAM{"q"}%" format=" * $web.$topic: %BR% $summary" nosearch="on" }%
366 <form action="http://www.dementia.org/twiki/view/%WEB%/%TOPIC%"> Find Topics: <input name="q" size="32" type="text" value="" /> <input type="submit" value="Search" /></form>
370 # <a name="TWiki Installation Error"></a> TWiki Installation Error
372 Incorrect format of searchformat template (missing sections? There should be 4 %SPLIT% tags)
374 **_Related Topics:_** [[UserDocumentationCategory]], [[SearchHelp]], [[TWikiVariables#VarSEARCH]], [[SearchPatternCookbook]], [[RegularExpression]]
376 -- **_Contributors:_** TWiki:Main.PeterThoeny, TWiki:Main.CrawfordCurrie