ab8948bbae396bdbf6d69dc59a3ae9334c1a03a1
[openafs-wiki.git] / TWiki / QuerySearch.mdwn
1 # <a name="Query Search"></a> Query Search
2
3 Query searches help you search the contents of forms attached to your topics, as well as the values of other meta-data attached to the topic. Using query searches you can search:
4
5 1. The fields of forms
6 2. Parent relationships
7 3. File attachment information (but **not** the attached files themselves)
8
9 Query searches are defined using a simple SQL-like query language. The language consists of _field specifiers_ and _constants_ joined with _operators_.
10
11 <div>
12   <ul>
13     <li><a href="#Query Search"> Query Search</a><ul>
14         <li><a href="#Field specifiers"> Field specifiers</a></li>
15         <li><a href="#Constants"> Constants</a></li>
16         <li><a href="#Operators"> Operators</a></li>
17         <li><a href="#Putting it all together"> Putting it all together</a></li>
18         <li><a href="#Examples"> Examples</a><ul>
19             <li><a href="#Query examples"> Query examples</a></li>
20             <li><a href="#Search examples"> Search examples</a></li>
21           </ul>
22         </li>
23       </ul>
24     </li>
25   </ul>
26 </div>
27
28 ## <a name="Field specifiers"></a> Field specifiers
29
30 You use field specifiers to say what value from the topic you are interested in.
31
32 All meta-data in a topic is referenced according to a to a simple plan.
33
34 - `name` - name of the topic
35 - `web` - name of the web the topic is within
36 - `text` - the body text of the topic
37 - `META:FILEATTACHMENT`
38   - _for each attachment_
39     - `name`
40     - `attr`
41     - `path`
42     - `size`
43     - `user`
44     - `rev`
45     - `date`
46     - `comment`
47 - `META:TOPICPARENT`
48   - `name`
49 - `META:TOPICINFO`
50   - `author`
51   - `date`
52   - `format`
53   - `version`
54 - `META:TOPICMOVED`
55   - `by`
56   - `date`
57   - `from`
58   - `to`
59 - `META:FORM` - the main form of the topic
60   - `name` (known as the _formname_)
61 - `META:FIELD` - the fields in the form.
62   - _for each field in the form_
63     - `name`
64     - `title`
65     - `value`
66 - `META:PREFERENCE`
67   - _for each preference in the topic_
68     - `name`
69     - `value`
70
71 See [[TWikiMetaData]] for details of what all these entries mean.
72
73 Most things at the top level of the plan - `META:TOPICPARENT`, `META:TOPICINFO` etc - are _structures_ which are indexed by _keys_. For example, `META:TOPICINFO` has 4 entries, which are indexed by the keys `author`, `date`, `format` and `version`. `META:FILEATTACHMENT`, `META:FIELD` and `META:PREFERENCE` are all _arrays_, which means they can have any number of records under them. Arrays are indexed by _numbers_ - for example, the first entry in the `META:FIELD` array is entry 0.
74
75 It's a bit clumsy having to type `META:FILEATTACHMENT` every time you want to refer to the array of attachments in a topic, so there are some predefined aliases that make it a bit less typing:
76
77 - `attachments` means the same as `META:FILEATTACHMENT`
78 - `info` means the same as `META:TOPICINFO`
79 - `parent` means the same as `META:TOPICPARENT`
80 - `moved` means the same as `META:TOPICMOVED`
81 - `form` means the same as `META:FORM`
82 - `fields` means the same as `META:FIELD`, You can also use the name of the form (the value of `form.name` e.g. `PersonForm`)
83 - `preferences` means the same as `META:PREFERENCE`
84
85 This plan is referenced using a simple syntax:
86
87 <table border="1" cellpadding="0" cellspacing="0">
88   <tr>
89     <th bgcolor="#99CCCC"><strong> Syntax </strong></th>
90     <th bgcolor="#99CCCC"><strong> Means </strong></th>
91     <th bgcolor="#99CCCC"><strong> Examples </strong></th>
92   </tr>
93   <tr>
94     <td><code>X</code></td>
95     <td> refers to the field named <code>X</code>. </td>
96     <td><code>info</code>, <code>META:TOPICMOVED</code>, <code>attachments</code>, <code>name</code>. </td>
97   </tr>
98   <tr>
99     <td><code>X.Y</code></td>
100     <td> refers to the entry with the key <code>Y</code> in the structure named <code>X</code></td>
101     <td><code>info.date</code>, <code>moved.by</code>, <code>META:TOPICPARENT.name</code></td>
102   </tr>
103   <tr>
104     <td><code>X[<i>query</i>]</code></td>
105     <td> refers to all the elements of the array <code>X</code> that match <em>query</em>. If <i>query</i> is of the form <code>name='Y'</code> then you can use the same <code>X.Y</code> syntax as is used for accessing structures. </td>
106     <td><code>attachments[size&gt;1024]</code>, <code>DocumentForm[name!='Summary' AND value~'top secret'].value</code>, <code>DocumentForm.Summary</code></td>
107   </tr>
108   <tr>
109     <td><code>X[N]</code></td>
110     <td> where <code>X</code> is an array and <code>N</code> is an integer number &gt;= 0, gets the Nth element of the array <code>X</code></td>
111     <td><code>attachments[3]</code></td>
112   </tr>
113   <tr>
114     <td><code>X/Y</code></td>
115     <td> accesses <code>Y</code> from the topic specified by the <em>value</em> of <code>X</code>. <code>X</code> must evaluate to a topic name </td>
116     <td><code>parent.name/(form.name='ExampleForm')</code> will evaluate to true if (1) the topic has a parent, (2) the parent topic has the main form type <code>ExampleForm</code>. </td>
117   </tr>
118 </table>
119
120 Note: at some point TWiki may support multiple forms in the same topic. For this reason you are recommended **not** to use the `fields` shortcut when accessing form fields, but always use the name of the form instead.
121
122 There is a shortcut for accessing form fields. If you use the name of a field (for example, `LastName`) in the query without a . before it, that is taken to mean "the value of the field named this". This works if and only if the field name isn't the same as of the top level entry names or their aliases described above. For example, the following expressions will all evaluate to the same thing:
123
124 - `PersonForm[name='Lastname'].value`
125 - `Lastname`
126 - `PersonForm.Lastname`
127
128 If `X` would conflict with the name of an entry or alias (e.g. it's `moved` or maybe `parent`), you can prepend the name of the form followed by a dot, as shown in the last example.
129
130 ## <a name="Constants"></a> Constants
131
132 You use constants for the values that you compare with fields. Constants are either strings, or numbers. Strings are always delimited by single-quotes (you can escape a quote using backslash). Numbers can be any integer or floating point number.
133
134 ## <a name="Operators"></a> Operators
135
136 Field specifiers and constants are combined using _operators_ to create queries.
137
138 <table border="1" cellpadding="0" cellspacing="0">
139   <tr>
140     <th bgcolor="#99CCCC"><strong> Operator </strong></th>
141     <th bgcolor="#99CCCC"><strong> Meaning </strong></th>
142   </tr>
143   <tr>
144     <td><code>=</code></td>
145     <td> Left-hand side (LHS) exactly matches the value on the Right-hand side (RHS). Numbers and strings can be compared. </td>
146   </tr>
147   <tr>
148     <td><code>!=</code></td>
149     <td> Inverse of <code>=</code>. </td>
150   </tr>
151   <tr>
152     <td><code>~</code></td>
153     <td> wildcard match ('*' will match any number of characters, '?' will match any single character e.g. "PersonForm.Surname ~ '*Smit?'") Note: Surname ~ 'Smith' is the same as Surname = 'Smith' </td>
154   </tr>
155   <tr>
156     <td><code>&lt;</code></td>
157     <td> LHS is less that RHS. If both sides are numbers, the order is numeric. Otherwise it is alphabetic (applies to all comparison operators) </td>
158   </tr>
159   <tr>
160     <td><code>&gt;</code></td>
161     <td> &gt; </td>
162   </tr>
163   <tr>
164     <td><code>&gt;=</code></td>
165     <td> &amp;gte; </td>
166   </tr>
167   <tr>
168     <td><code>&lt;=</code></td>
169     <td> &amp;lte; </td>
170   </tr>
171   <tr>
172     <td><code>lc(x)</code></td>
173     <td> Converts x to lower case, Use for caseless comparisons. </td>
174   </tr>
175   <tr>
176     <td><code>uc(x)</code></td>
177     <td> Converts x to UPPER CASE. Use for caseless comparisons. </td>
178   </tr>
179   <tr>
180     <td><code>d2n(x)</code></td>
181     <td> Converts a date (expressed in [[Main/TimeSpecifications]]) to a number of seconds since 1st Jan 1970. This is the format dates are stored in inside TWiki, and you have to convert a string date using <code>d2n</code> before you can compare it with - for example - the date an attachment was uploaded. Times without a timezone are assumed to be in server local time. If you have date fields in your forms, note that they are <strong>not</strong> stored in TWiki's internal format, but are stored as text strings. You should still use <code>d2n</code> to convert them to numbers for comparisons, though. </td>
182   </tr>
183   <tr>
184     <td><code>NOT</code></td>
185     <td> Invert the result of the subquery </td>
186   </tr>
187   <tr>
188     <td><code>AND</code></td>
189     <td> Combine two subqueries </td>
190   </tr>
191   <tr>
192     <td><code>OR</code></td>
193     <td> Combine two subqueries </td>
194   </tr>
195   <tr>
196     <td><code>()</code></td>
197     <td> Bracketed subquery </td>
198   </tr>
199 </table>
200
201 %I% The same operators are supported for [[%IF statements|Main/VarIF]].
202
203 ## <a name="Putting it all together"></a> Putting it all together
204
205 When a query is applied to a topic, the goal is to reduce to a TRUE or FALSE value that indicates whether the topic matches that query or not. If the query returns TRUE, then the topic is included in the search results.
206
207 A query matches if the query returns one or more values when it is applied to the topic. So if I have a very simple query, such as `"attachments"`, then this will return TRUE for all topics that have one or more attachments. If I write `"attachments[size>1024 AND name ~ '*.gif']"` then it will return TRUE for all topics that have at least one attachment larger than 1024 bytes with a name ending in `.gif`.
208
209 ## <a name="Examples"></a> Examples
210
211 ### <a name="Query examples"></a> Query examples
212
213 - `attachments[name='purdey.gif']` - true if there is an attachment call `purdey.gif` on the topic
214 - `(fields[name='Firstname'].value='Emma' OR fields[name=Firstname].value='John') AND fields[name='Lastname'].value='Peel'` - true for 'Emma Peel' and 'John Peel' but **not** 'Robert Peel' or 'Emma Thompson'
215 - `(Firstname='Emma' OR Firstname='John') AND Lastname='Peel'` - shortcut form of the previous query
216 - `HistoryForm[name='Age'].value>2` - true if the topic has a `HistoryForm`, and the form has a field called `Age` with a value &gt; 2
217 - `HistoryForm.Age > 2` - shortcut for the previous query
218 - =preferences[name='FaveColour' AND value='Tangerine'] - true if the topic has the given preference setting and value
219 - <code>Person/([[ClothesForm]][name='Headgear'].value ~ '\*Bowler\*' AND attachments[name~'\*hat.gif' AND date &lt; d2n('2007-01-01')])</code> - true if the form attached to the topic has a field called `Person` that has a value that is the name of a topic, and that topic contains the form `ClothesForm`, with a field called `Headgear`, and the value of that field contains the string `'Bowler'`, and the topic also has at least one attachment that has a name matching `*hat.gif` and a date before 1st Jan 2007. (Phew!)
220
221 ### <a name="Search examples"></a> Search examples
222
223 Find all topics that are children of this topic in the current web
224
225     %SEARCH{"parent.name = '%TOPIC%'" web="%WEB%" type="query"}%
226
227 Find all topics that have an attachment called 'grunge.gif'
228
229     %SEARCH{"attachments[name='grunge.gif']" type="query"}%
230
231 Find all topics that have form `ColourForm` where the form field 'Shades' is 'green' or 'yellow' but not 'brown'
232
233     %SEARCH{"(lc(Shades)='green' OR lc(Shades)='yellow') AND NOT(lc(Shades) ~ 'brown')" type="query"}%
234
235 Find all topics that have PNG attachments that have been added since 26th March 2007
236
237     %SEARCH{"attachments[name ~ '*.png' AND date >= d2n('2007-03-26')"}%
238
239 Find all topics that have a field 'Threat' set to 'Amber' and 'cold virus' somewhere in the topic text.
240
241     %SEARCH{"Threat='Amber' AND text ~ '*cold virus*'"}%