head	1.7;
access;
symbols;
locks; strict;
comment	@# @;


1.7
date	2011.04.04.20.00.30;	author TWikiContributor;	state Exp;
branches;
next	1.6;

1.6
date	2010.06.16.18.10.39;	author TWikiContributor;	state Exp;
branches;
next	1.5;

1.5
date	2010.05.29.14.44.53;	author TWikiContributor;	state Exp;
branches;
next	1.4;

1.4
date	2009.09.02.13.43.17;	author TWikiContributor;	state Exp;
branches;
next	1.3;

1.3
date	2009.04.29.20.53.54;	author TWikiContributor;	state Exp;
branches;
next	1.2;

1.2
date	2008.08.03.22.17.44;	author TWikiContributor;	state Exp;
branches;
next	1.1;

1.1
date	2008.01.22.03.21.27;	author TWikiContributor;	state Exp;
branches;
next	;


desc
@buildrelease
@


1.7
log
@buildrelease
@
text
@%META:TOPICINFO{author="TWikiContributor" date="1301947230" format="1.1" version="7"}%
%META:TOPICPARENT{name="TWikiVariables"}%
%STARTINCLUDE%
---+ Query Search

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:
   1 The fields of forms
   1 Parent relationships
   1 File attachment information (but *not* the attached files themselves)

Query searches are defined using a simple SQL-like query language. The language consists of _field specifiers_ and _constants_ joined with _operators_.

%TOC%

---++ Field specifiers

You use field specifiers to say what value from the topic you are interested
in.

All meta-data in a topic is referenced according to a simple plan.
 
   * =name= - name of the topic
   * =web= - name of the web the topic is within
   * =text= - the body text of the topic
   * =META:FILEATTACHMENT=
      * =arraysize=
      * _for each attachment_
         * =name=
         * =attr=
         * =path=
         * =size=
         * =user=
         * =rev=
         * =date=
         * =comment=
   * =META:TOPICPARENT=
      * =name=
   * =META:TOPICINFO=
      * =author=
      * =date=
      * =format=
      * =rev= - topic revision (=12=) to match =%<nop>REVINFO{'$rev'}%= and FormattedSearch =$rev=
      * =version= - internal Store topic version (=1.12= for rcs based Stores)
   * =META:TOPICMOVED=
      * =by=
      * =date=
      * =from=
      * =to=
   * =META:FORM= - the main form of the topic
      * =name= (known as the _formname_)
   * =META:FIELD= - the fields in the form.
      * =arraysize=
      * _for each field in the form_
         * =name=
         * =title=
         * =value=
   * =META:PREFERENCE=
      * =arraysize=
      * _for each preference in the topic_
         * =name=
         * =value=

See TWikiMetaData for details of what all these entries mean.

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. The field specifier =arraysize= returns the number of items in an array, for example, =attachments.arraysize= returns the number of attachments.

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:
   * =attachments= means the same as =META:FILEATTACHMENT=
   * =info= means the same as =META:TOPICINFO=
   * =parent= means the same as =META:TOPICPARENT=
   * =moved= means the same as =META:TOPICMOVED=
   * =form= means the same as =META:FORM=
   * =fields= means the same as =META:FIELD=, You can also use the name of the form (the value of =form.name= e.g. =PersonForm=)
   * =preferences= means the same as =META:PREFERENCE=

This plan is referenced using a simple syntax:
| *Syntax* | *Means* | *Examples* |
| =X= | refers to the field named =X=. | =info=, =META:TOPICMOVED=, =attachments=, =name=. |
| =X.Y= | refers to the entry with the key =Y= in the structure named =X= | =info.date=, =moved.by=, =META:TOPICPARENT.name=, =attachments.arraysize= |
| =X[<i>query</i>]= | refers to all the elements of the array =X= that match _query_. If <i>query</i> is of the form =name='Y'= then you can use the same =X.Y= syntax as is used for accessing structures. | =attachments[size>1024]=, =DocumentForm[name!='Summary' AND value~'top secret'].value=, =DocumentForm.Summary= | 
| =X[N]= | where =X= is an array and =N= is an integer number >= 0, gets the Nth element of the array =X= | =attachments[3]= |
| =X/Y= | accesses =Y= from the topic specified by the _value_ of =X=. =X= must evaluate to a topic name | =parent.name/(form.name='ExampleForm')= will evaluate to true if (1) the topic has a parent, (2) the parent topic has the main form type =ExampleForm=. |

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.

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:
   * =PersonForm[name='Lastname'].value=
   * =Lastname=
   * =PersonForm.Lastname=
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. 

---++ Constants

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. Constants can be supplied by TWikiVariables, such as ='%<nop>TOPIC%'= representing the current topic name.

---++ Operators

Field specifiers and constants are combined using _operators_ to create queries.
| *Operator* | *Meaning* |
| <code>=</code> | Left-hand side (LHS) exactly matches the value on the Right-hand side (RHS). Numbers and strings can be compared. |
| <code>!=</code> | Inverse of <code>=</code>. |
| =~= | 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' |
| <code>&lt;</code> | LHS is less that RHS. If both sides are numbers, the order is numeric. Otherwise it is alphabetic (applies to all comparison operators) |
| <code>&gt;</code> | &gt; |
| <code>&gt;=</code> | &ge; |
| <code>&lt;=</code> | &le; |
| =lc(x)= | Converts x to lower case, Use for caseless comparisons. |
| =uc(x)= | Converts x to UPPER CASE. Use for caseless comparisons. |
| =d2n(x)= | Converts a date (expressed in [[TimeSpecifications][one of the formats that TWiki can parse]]) 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 =d2n= 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 *not* stored in TWiki's internal format, but are stored as text strings. You should still use =d2n= to convert them to numbers for comparisons, though. |
| =NOT= | Invert the result of the subquery |
| =AND= | Combine two subqueries |
| =OR= | Combine two subqueries |
| =()= | Bracketed subquery |

%I% The same operators are supported for [[VarIF][%IF statements]].

---++ Putting it all together

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.

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=.

---++ Gotcha

   * Remember that in the query language, TWiki topic names are _constants_. You cannot write =Main.JohnSmith/UserForm.FirstName= because =Main.JohnSmith= will be interpreted as a form field name. If you want to refer to topics you *must* enclose the topic name in single quotes i.e. ='Main.JohnSmith'/UserForm.FirstName=

---++ Examples

---+++ Query examples

   * =attachments[name='purdey.gif']= - true if there is an attachment call =purdey.gif= on the topic
   * =attachments[name~'*.gif'].arraysize > 3= - true if there are at least 4 =.gif= files attached to the topic
   * =(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'
   * =(Firstname='Emma' OR Firstname='John') AND Lastname='Peel'= - shortcut form of the previous query
   * =History<nop>Form[name='Age'].value>2= - true if the topic has a =HistoryForm=, and the form has a field called =Age= with a value > 2
   * =History<nop>Form.Age > 2= - shortcut for the previous query
   * =preferences[name='FaveColour' AND value='Tangerine']= - true if the topic has the given preference setting and value
   * =Person/(Clothes<nop>Form[name='Headgear'].value ~ '*Bowler*' AND attachments[name~'*hat.gif' AND date < d2n('2007-01-01')])= - 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!)

---+++ Search examples

Find all topics that are children of this topic in the current web
<verbatim>
%SEARCH{"parent.name = '%TOPIC%'" web="%WEB%" type="query"}%
</verbatim>
Find all topics that have an attachment called 'grunge.gif'
<verbatim>
%SEARCH{"attachments[name='grunge.gif']" type="query"}%
</verbatim>
Find all topics that have form =ColourForm= where the form field 'Shades' is 'green' or 'yellow' but not 'brown'
<verbatim>
%SEARCH{"(lc(Shades)='green' OR lc(Shades)='yellow') AND NOT(lc(Shades) ~ 'brown')" type="query"}%
</verbatim>
Find all topics that have PNG attachments that have been added since 26th March 2007
<verbatim>
%SEARCH{"attachments[name ~ '*.png' AND date >= d2n('2007-03-26')]"}%
</verbatim>
Find all topics that have a field 'Threat' set to 'Amber' and 'cold virus' somewhere in the topic text.
<verbatim>
%SEARCH{"Threat='Amber' AND text ~ '*cold virus*'"}%
</verbatim>

__Related Topics:__ IfStatements, SearchHelp, VarIF, VarGET, VarSET, VarSEARCH, FormattedSearch, TWiki:TWiki.QuerySearchPatternCookbook
@


1.6
log
@buildrelease
@
text
@d1 1
a1 1
%META:TOPICINFO{author="TWikiContributor" date="1276711839" format="1.1" version="6"}%
d163 1
a163 1
__Related Topics:__ IfStatements, SearchHelp, VarSEARCH, FormattedSearch, TWiki:TWiki/QuerySearchPatternCookbook
@


1.5
log
@buildrelease
@
text
@d1 1
a1 1
%META:TOPICINFO{author="TWikiContributor" date="1265790321" format="1.1" version="5"}%
d20 1
a20 1
All meta-data in a topic is referenced according to a to a simple plan.
@


1.4
log
@buildrelease
@
text
@d1 1
a1 1
%META:TOPICINFO{author="TWikiContributor" date="1244159340" format="1.1" version="4"}%
d16 1
d26 1
d52 1
d58 1
d65 1
a65 1
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.
d79 1
a79 1
| =X.Y= | refers to the entry with the key =Y= in the structure named =X= | =info.date=, =moved.by=, =META:TOPICPARENT.name= |
d93 2
a94 1
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. 
d118 1
d124 2
a125 1
   * Remember that in the query language, TWiki topic names are _constants_. You cannot write =Main.UserTopic/UserForm.firstName= because =Main.UserTopic= will be interpreted as a form field name. If you want to refer to topics you *must* enclose the topic name in quotes i.e. ='Main.UserTopic'/UserForm.firstName=
d130 1
d132 2
a133 1
   * =(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'
d138 1
a138 1
   * =Person/(ClothesForm[name='Headgear'].value ~ '*Bowler*' AND attachments[name~'*hat.gif' AND date < d2n('2007-01-01')])= - 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!)
d141 1
d163 1
a163 1
__Related Topics:__ SearchHelp, VarSEARCH, FormattedSearch, TWiki:TWiki/QuerySearchPatternCookbook
@


1.3
log
@buildrelease
@
text
@d1 1
a1 1
%META:TOPICINFO{author="TWikiContributor" date="1240610074" format="1.1" version="3"}%
d100 2
a101 2
| <code>&gt;=</code> | &gte; |
| <code>&lt;=</code> | &lte; |
d146 1
a146 1
%SEARCH{"attachments[name ~ '*.png' AND date >= d2n('2007-03-26')"}%
@


1.2
log
@buildrelease
@
text
@d1 1
a1 1
%META:TOPICINFO{author="TWikiContributor" date="1168735492" format="1.1" version="2"}%
d152 2
@


1.1
log
@buildrelease
@
text
@d1 1
a1 1
%META:TOPICINFO{author="TWikiContributor" date="1168735492" format="1.1" version="1"}%
d40 2
a41 1
      * =version=
d117 3
d128 1
a128 1
   * =preferences[name='FaveColour' AND value='Tangerine'] - true if the topic has the given preference setting and value
@
