data/TWiki/FormattedSearch.txt
author Colas Nahaboo <colas@nahaboo.net>
Sat, 26 Jan 2008 15:50:53 +0100
changeset 0 414e01d06fd5
permissions -rw-r--r--
RELEASE 4.2.0 freetown
colas@0
     1
%META:TOPICINFO{author="TWikiContributor" date="1168735492" format="1.1" version="19"}%
colas@0
     2
%META:TOPICPARENT{name="TWikiVariables"}%
colas@0
     3
%STARTINCLUDE%
colas@0
     4
---+ TWiki Formatted Search
colas@0
     5
colas@0
     6
_Inline search feature allows flexible formatting of search result_
colas@0
     7
colas@0
     8
The default output format of a =[[VarSEARCH][%<nop>SEARCH{...}%]]= 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 =%<nop>SEARCH{ "food" format="| $topic | $summary |" }%=. See =[[VarSEARCH][%<nop>SEARCH{...}%]]= for other search parameters, such as =separator=""=.
colas@0
     9
colas@0
    10
%TOC%
colas@0
    11
colas@0
    12
---++ Syntax
colas@0
    13
colas@0
    14
Two parameters can be used to specify a customized search result:
colas@0
    15
colas@0
    16
---+++ 1. =header="..."= parameter
colas@0
    17
colas@0
    18
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="| <nop>*Topic:*<nop> | <nop>*Summary:*<nop> |"=
colas@0
    19
colas@0
    20
Variables that can be used in the header string:
colas@0
    21
colas@0
    22
| *Name:* | *Expands To:* |
colas@0
    23
| =$web= | Name of the web |
colas@0
    24
%INCLUDE{FormatTokens}%
colas@0
    25
colas@0
    26
---+++ 2. =format="..."= parameter
colas@0
    27
colas@0
    28
Use the format parameter to specify the format of one search hit.
colas@0
    29
<br /> Example: =format="| $topic | $summary |"=
colas@0
    30
colas@0
    31
Variables that can be used in the format string:
colas@0
    32
colas@0
    33
| *Name:* | *Expands To:* |
colas@0
    34
| =$web= | Name of the web |
colas@0
    35
| =$topic= | Topic name |
colas@0
    36
| =$topic(20)= | Topic name, "<tt>- </tt>" hyphenated each 20 characters |
colas@0
    37
| =$topic(30, -&lt;br /&gt;)= | Topic name, hyphenated each 30 characters with separator "<tt>-&lt;br /&gt;</tt>" |
colas@0
    38
| =$topic(40, ...)= | Topic name, shortended to 40 characters with "<tt>...</tt>" indication |
colas@0
    39
| =$parent= | Name of parent topic; empty if not set |
colas@0
    40
| =$parent(20)= | Name of parent topic, same hyphenation/shortening like =$topic()= |
colas@0
    41
| =$text= | Formatted topic text. In case of a =multiple="on"= search, it is the line found for each search hit. |
colas@0
    42
| =$locked= | LOCKED flag (if any) |
colas@0
    43
| =$date= | Time stamp of last topic update, e.g. =%GMTIME{"$day $mon $year - $hour:$min"}%= |
colas@0
    44
| =$isodate= | Time stamp of last topic update, e.g. =%GMTIME{"$year-$mo-$dayT$hour:$minZ"}%= |
colas@0
    45
| =$rev= | Number of last topic revision, e.g. =4= |
colas@0
    46
| =$username= | Login name of last topic update, e.g. =jsmith= |
colas@0
    47
| =$wikiname= | Wiki user name of last topic update, e.g. =<nop>JohnSmith= |
colas@0
    48
| =$wikiusername= | Wiki user name of last topic update, like =%USERSWEB%.<nop>JohnSmith= |
colas@0
    49
| =$createdate= | Time stamp of topic revision 1 |
colas@0
    50
| =$createusername= | Login name of topic revision 1, e.g. =jsmith= |
colas@0
    51
| =$createwikiname= | Wiki user name of topic revision 1, e.g. =<nop>JohnSmith= |
colas@0
    52
| =$createwikiusername= | Wiki user name of topic revision 1, e.g. =%USERSWEB%.<nop>JohnSmith= |
colas@0
    53
| =$summary= | Topic summary, just the plain text, all formatting and line breaks removed; up to 162 characters |
colas@0
    54
| =$summary(50)= | Topic summary, up to 50 characters shown |
colas@0
    55
| =$summary(showvarnames)= | Topic summary, with =%<nop>ALLTWIKI{...}%= variables shown as =ALLTWIKI{...}= |
colas@0
    56
| =$summary(noheader)= | Topic summary, with leading =---+ headers= removed%BR% __Note:__ The tokens can be combined, for example =$summary(100, showvarnames, noheader)= |
colas@0
    57
| =$changes= | Summary of changes between latest rev and previous rev |
colas@0
    58
| =$changes(n)= | Summary of changes between latest rev and rev n |
colas@0
    59
| =$formname= | The name of the form attached to the topic; empty if none |
colas@0
    60
| =$formfield(name)= | The field value of a form field; for example, =$formfield(<nop>TopicClassification)= would get expanded to =PublicFAQ=. This applies only to topics that have a [[TWikiForms][TWikiForm]] |
colas@0
    61
| =$formfield(name, 10)= | Form field value, "<tt>- </tt>" hyphenated each 10 characters |
colas@0
    62
| =$formfield(name, 20, -&lt;br /&gt;)= | Form field value, hyphenated each 20 characters with separator "<tt>-&lt;br /&gt;</tt>" |
colas@0
    63
| =$formfield(name, 30, ...)= | Form field value, shortended to 30 characters with "<tt>...</tt>" indication |
colas@0
    64
| =$pattern(reg-exp)= | A regular expression pattern to extract some text from a topic (does not search meta data; use =$formfield= instead). In case of a =multiple="on"= search, the pattern is applied to the line found in each search hit.%BB% Specify a RegularExpression that covers the whole text (topic or line), which typically starts with =.*=, and must end in =.*= %BB% Put text you want to keep in parenthesis, like =$pattern(.*?(from here.*?to here).*)= %BB% Example: =$pattern(.*?\*.*?Email\:\s*([^\n\r]+).*)= extracts the e-mail address from a bullet of format =* Email: ...= %BB% This example has non-greedy =.*?= patterns to scan for the first occurance of the Email bullet; use greedy =.*= patterns to scan for the last occurance %BB% Limitation: Do not use =.*)= inside the pattern, e.g. =$pattern(.*foo(.*)bar.*)= does not work, but =$pattern(.*foo(.*?)bar.*)= 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 |
colas@0
    65
| =$count(reg-exp)= | 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 =$pattern(reg-exp)=. Example: =$count(.*?(---[+][+][+][+]) .*)= counts the number of &lt;H4&gt; headers in a page. |
colas@0
    66
%INCLUDE{FormatTokens}%
colas@0
    67
colas@0
    68
---++ Examples
colas@0
    69
colas@0
    70
Here are some samples of formatted searches. The SearchPatternCookbook has other examples, such as [[SearchPatternCookbook#SearchUsernames][creating a picklist of usernames]], [[SearchPatternCookbook#SearchTopicChildren][searching for topic children]] and more.
colas@0
    71
colas@0
    72
#SearchBulletList
colas@0
    73
---+++ Bullet list showing topic name and summary
colas@0
    74
colas@0
    75
*Write this:*
colas@0
    76
colas@0
    77
=%<nop>SEARCH{ "FAQ" scope="topic" nosearch="on" nototal="on" header="   * <nop>*Topic: Summary:*" format="   * [<nop>[$topic]]: $summary" }%=
colas@0
    78
colas@0
    79
*To get this:*
colas@0
    80
colas@0
    81
%SEARCH{ "FAQ" scope="topic" nosearch="on" nototal="on" header="   * *Topic: Summary:*" format="   * [[$topic]]: $summary" }%
colas@0
    82
colas@0
    83
colas@0
    84
---+++ Table showing form field values of topics with a form
colas@0
    85
colas@0
    86
In a web where there is a form that contains a =Topic<nop>Classification= field, an =Operating<nop>System= field and an =Os<nop>Version= field we could write:
colas@0
    87
colas@0
    88
=| <nop>*Topic:*<nop> | <nop>*<nop>OperatingSystem:*<nop> | <nop>*<nop>OsVersion:*<nop> |= <br />
colas@0
    89
=%<nop>SEARCH{ "[T]opicClassification.*?value=\"[P]ublicFAQ\"" scope="text" type="regex" nosearch="on" nototal="on" format="| [<nop>[$topic]] | $formfield(<nop>OperatingSystem) | $formfield(<nop>OsVersion) |" }%=
colas@0
    90
colas@0
    91
To get this:
colas@0
    92
colas@0
    93
<table border="1" cellspacing="0" cellpadding="1">
colas@0
    94
<tr>
colas@0
    95
 <th bgcolor="#99CCCC"> <strong>Topic:</strong> </th>
colas@0
    96
 <th bgcolor="#99CCCC"> <strong>OperatingSystem:</strong> </th>
colas@0
    97
 <th bgcolor="#99CCCC"> <strong>OsVersion:</strong> </th></tr>
colas@0
    98
<tr>
colas@0
    99
 <td>  <a href="%SCRIPTURLPATH{"view"}%/Sandbox/IncorrectDllVersionW32PTH10DLL">IncorrectDllVersionW32PTH10DLL</a>  </td><td>  <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OsWin">OsWin</a>  </td><td>  95/98  </td></tr>
colas@0
   100
<tr>
colas@0
   101
 <td>  <a href="%SCRIPTURLPATH{"view"}%/Sandbox/WinDoze95Crash">WinDoze95Crash</a>  </td>
colas@0
   102
 <td>  <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OsWin">OsWin</a>  </td>
colas@0
   103
 <td>  95  </td></tr>
colas@0
   104
</table>
colas@0
   105
colas@0
   106
colas@0
   107
---+++ Extract some text from a topic using regular expression
colas@0
   108
colas@0
   109
*Write this:*
colas@0
   110
colas@0
   111
=%<nop>SEARCH{ "__Back to\:__ <nop>TWikiFAQ" scope="text" type="regex" nosearch="on" nototal="on" header="TWiki FAQs:" format="   * $pattern(.*?FAQ\:[\n\r]*([^\n\r]+).*) [<nop>[$topic][Answer...]]" }%=
colas@0
   112
colas@0
   113
*To get this:*
colas@0
   114
colas@0
   115
%SEARCH{ "__Back to\:__ TWikiFAQ" scope="text" type="regex" nosearch="on" nototal="on" header="TWiki FAQs:" format="   * $pattern(.*?FAQ\:[\n\r]*([^\n\r]+).*) [[$topic][Answer...]]" }%
colas@0
   116
colas@0
   117
colas@0
   118
---+++ Nested Search
colas@0
   119
colas@0
   120
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.
colas@0
   121
colas@0
   122
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).
colas@0
   123
colas@0
   124
   * First search:
colas@0
   125
      * =%<nop>SEARCH{ "culture" format="   * $topic is referenced by: (list all references)" nosearch="on" nototal="on" }%=
colas@0
   126
   * Second search. For each hit we want this search:
colas@0
   127
      * =%<nop>SEARCH{ "(topic found in first search)" format="$topic" nosearch="on" nototal="on" separator=", " }%=
colas@0
   128
   * 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:
colas@0
   129
      * Use =$percnt= to escape the leading percent of the second search
colas@0
   130
      * Use =\"= to escape the double quotes
colas@0
   131
      * Use =$dollar= to escape the =$= of =$topic=
colas@0
   132
      * Use =$nop= to escape the =}%= sequence
colas@0
   133
colas@0
   134
*Write this:*
colas@0
   135
colas@0
   136
=%<nop>SEARCH{ "culture" format="   * $topic is referenced by:$n      * $percntSEARCH{ \"$topic\" format=\"$dollartopic\" nosearch=\"on\" nototal=\"on\" separator=\", \" }$nop%" nosearch="on" nototal="on" }%=
colas@0
   137
colas@0
   138
*To get this:*
colas@0
   139
colas@0
   140
%SEARCH{ "culture" format="   * $topic is referenced by:$n      * $percntSEARCH{ \"$topic\" format=\"$dollartopic\" nosearch=\"on\" nototal=\"on\" separator=\", \" }$nop%" nosearch="on" nototal="on" }%
colas@0
   141
colas@0
   142
__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.
colas@0
   143
colas@0
   144
---+++ Most recently changed pages
colas@0
   145
colas@0
   146
*Write this:*
colas@0
   147
colas@0
   148
=%<nop>SEARCH{ "\.*" scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on"  format="| [<nop>[$topic]] | $wikiusername  | $date |" limit="7" }%=
colas@0
   149
colas@0
   150
*To get this:*
colas@0
   151
colas@0
   152
%SEARCH{ "\.*" scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on"  format="| [[$topic]] | $wikiusername  | $date |" limit="7" }%
colas@0
   153
colas@0
   154
---+++ Search with conditional output
colas@0
   155
colas@0
   156
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:
colas@0
   157
   1. Specify a search which returns more hits then you need
colas@0
   158
   1. For each search hit apply a spreadsheet formula to determine if the hit is needed
colas@0
   159
   1. If needed, format and output the result
colas@0
   160
   1. Else supress the search hit
colas@0
   161
colas@0
   162
This requires the TWiki:Plugins.SpreadSheetPlugin. The following example shows all topics that are up to exactly one week old.
colas@0
   163
colas@0
   164
*Write this:*
colas@0
   165
colas@0
   166
=%<nop>CALC{$SET(weekold, $TIMEADD($TIME(), -7, day))}%= %BR%
colas@0
   167
=%<nop>SEARCH{ "." scope="topic" type="regex" nosearch="on" nototal="on" order="modified" reverse="on" format="$percntCALC{$IF($TIME($date) &lt; $GET(weekold), &lt;nop&gt;, | [<nop>[$topic]] | $wikiusername | $date | $rev |)}$percnt" limit="100" }%=
colas@0
   168
colas@0
   169
   * The first line sets the =weekold= variable to the serialized date of exactly one week ago
colas@0
   170
   * The SEARCH has a deferred CALC. The =$percnt= makes sure that the CALC gets executed once for each search hit
colas@0
   171
   * The CALC compares the date of the topic with the =weekold= date
colas@0
   172
   * If topic is older, a =&lt;nop&gt;= is returned, which gets removed at the end of the TWiki rendering process
colas@0
   173
   * Otherwise, the search hit is formatted and returned
colas@0
   174
colas@0
   175
*To get this:*
colas@0
   176
colas@0
   177
%CALC{$SET(weekold, $TIMEADD($TIME(), -7, day))}%
colas@0
   178
%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" }%
colas@0
   179
colas@0
   180
---+++ Embedding search forms to return a formatted result
colas@0
   181
colas@0
   182
Use an HTML form and an embedded formatted search on the same topic. You can link them together with an =%<nop>URLPARAM{"..."}%= variable. Example:
colas@0
   183
colas@0
   184
*Write this:*
colas@0
   185
colas@0
   186
<verbatim>
colas@0
   187
<form action="%SCRIPTURLPATH{"view"}%/%WEB%/%TOPIC%">
colas@0
   188
Find Topics: 
colas@0
   189
<input type="text" name="q" size="32" value="%URLPARAM{"q"}%" />&nbsp;<input type="submit" class="twikiSubmit" value="Search" />
colas@0
   190
</form>
colas@0
   191
Result:
colas@0
   192
%SEARCH{ search="%URLPARAM{"q"}%" format="   * $web.$topic: %BR% $summary" nosearch="on" }%
colas@0
   193
</verbatim>
colas@0
   194
colas@0
   195
*To get this:*
colas@0
   196
colas@0
   197
<form action="%SCRIPTURLPATH{"view"}%/%WEB%/%TOPIC%">
colas@0
   198
Find Topics: 
colas@0
   199
<input type="text" name="q" size="32" value="%URLPARAM{"q"}%" />&nbsp;<input type="submit" class="twikiSubmit" value="Search" />
colas@0
   200
</form>
colas@0
   201
Result:
colas@0
   202
%SEARCH{ search="%URLPARAM{"q"}%" format="   * $web.$topic: %BR% $summary" nosearch="on" }%
colas@0
   203
colas@0
   204
__Related Topics:__ UserDocumentationCategory, SearchHelp, TWikiVariables#VarSEARCH, SearchPatternCookbook, RegularExpression
colas@0
   205
colas@0
   206
-- __Contributors:__ TWiki:Main.PeterThoeny, TWiki:Main.CrawfordCurrie