pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / xml-query-parser / LuceneCoreQuery.dtd
1 <!--
2         <h3>Background</h3>
3         This DTD describes the XML syntax used to perform advanced searches using the core Lucene search engine. The motivation behind the XML query syntax is:
4         <ol>
5         <li>To open up Lucene functionality to clients other than Java</li>
6         <li>To offer a form of expressing queries that can easily be
7             <ul>
8                 <li>Persisted for logging/auditing purposes</li>
9                 <li>Changed by editing text query templates (XSLT) without requiring a recompile/redeploy of applications</li>
10                 <li>Serialized across networks (without requiring Java bytecode for Query logic deployed on clients)</li>
11             </ul>
12         </li>
13         <li>To provide a shorthand way of expressing query logic which echos the logical tree structure of query objects more closely than reading procedural Java query construction code</li>
14         <li>To bridge the growing gap between Lucene query/filtering functionality and the set of functionality accessible throught the standard Lucene QueryParser syntax</li>
15         <li>To provide a simply extensible syntax that does not require complex parser skills such as knowledge of JavaCC syntax</li>
16         </ol>
17         
18         
19         <h3>Syntax overview</h3>
20         Search syntax consists of two types of elements:
21         <ul>
22         <li><i>Queries</i></li>
23         <li><i>Filters</i></li>
24         </ul>
25
26         <h4>Queries</h4>
27         The root of any XML search must be a <i>Query</i> type element used to select content.
28         Queries typically score matches on documents using a number of different factors in order to provide relevant results first. 
29         One common example of a query tag is the <a href="#UserQuery">UserQuery</a> element which uses the standard 
30         Lucene QueryParser to parse Google-style search syntax provided by end users.
31         
32         <h4>Filters</h4>
33         Unlike Queries, <i>Filters</i> are not used to select or score content - they are simply used to filter <i>Query</i> output (see <a href="#FilteredQuery">FilteredQuery</a> for an example use of query filtering).
34         Because Filters simply offer a yes/no decision for each document in the index their output can be efficiently cached in memory as a <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/BitSet.html">Bitset</a> for
35         subsequent reuse (see <a href="#CachedFilter">CachedFilter</a> tag).
36
37         <h4>Nesting elements</h4>
38         Many of the the elements can nest other elements to produce queries/filters of an arbitrary depth and complexity. 
39         The <a href="#BooleanQuery">BooleanQuery</a> element is one such example which provides a means for combining other queries (including other BooleanQueries) using Boolean 
40         logic to determine mandatory or optional elements. 
41
42         
43         <h3>Advanced topics</h3>        
44         <h4>Advanced positional testing - span queries</h4>
45         The <i>SpanQuery</i> class of queries allow for complex positional tests which not only look for certain combinations of words but in particular 
46         positions in relation to each other and the documents containing them.
47         
48         
49         CoreParser.java is the Java class that encapsulates this parser behaviour.
50         
51         
52         @title Core Lucene      
53 -->
54
55 <!-- @hidden Define core types of XML elements -->
56 <!ENTITY % coreSpanQueries "SpanOr|SpanNear|SpanOrTerms|SpanFirst|SpanNot|SpanTerm|BoostingTermQuery" >
57 <!ENTITY % coreQueries "BooleanQuery|UserQuery|FilteredQuery|TermQuery|TermsQuery|MatchAllDocsQuery|ConstantScoreQuery|BoostingTermQuery|NumericRangeQuery" >
58 <!ENTITY % coreFilters "RangeFilter|NumericRangeFilter|CachedFilter" >
59
60 <!-- @hidden Allow for extensions -->
61 <!ENTITY % extendedSpanQueries1 " " >
62 <!ENTITY % extendedQueries1 " " >
63 <!ENTITY % extendedFilters1 " " >
64
65 <!ENTITY % spanQueries "%coreSpanQueries;%extendedSpanQueries1;" >
66 <!ENTITY % queries "%coreQueries;|%spanQueries;%extendedQueries1;" >
67
68
69 <!ENTITY % filters "%coreFilters;%extendedFilters1;" >
70
71 <!--
72         BooleanQuerys implement Boolean logic which controls how multiple Clauses should be interpreted.
73         Some clauses may represent optional Query criteria while others represent mandatory criteria.   
74         @example 
75                 <em>Find articles about banks, preferably talking about mergers but nothing to do with "sumitomo"</em>
76                 %                 
77             <BooleanQuery fieldName="contents">
78                      <Clause occurs="should">
79                               <TermQuery>merger</TermQuery>
80                      </Clause>
81                      <Clause occurs="mustnot">
82                               <TermQuery>sumitomo</TermQuery>
83                      </Clause>
84                      <Clause occurs="must">
85                               <TermQuery>bank</TermQuery>
86                      </Clause>
87             </BooleanQuery>
88
89                  %
90 -->     
91 <!ELEMENT BooleanQuery (Clause)+>
92 <!-- Optional boost for matches on this query. Values > 1 -->
93 <!ATTLIST BooleanQuery boost CDATA "1.0">
94 <!-- fieldName can optionally be defined here as a default attribute used by all child elements -->     
95 <!ATTLIST BooleanQuery fieldName CDATA #IMPLIED>
96 <!-- The "Coordination factor" rewards documents that contain more of the optional clauses in this list. This flag can be used to turn off this factor. -->
97 <!ATTLIST BooleanQuery disableCoord (true | false) "false">
98 <!-- The minimum number of optional clauses that should be present in any one document before it is considered to be a match. -->
99 <!ATTLIST BooleanQuery minimumNumberShouldMatch CDATA "0">
100
101 <!-- NOTE: "Clause" tag has 2 modes of use - inside <BooleanQuery> in which case only "query" types can be
102         child elements - while in a <BooleanFilter> clause only "filter" types can be contained.
103         @hidden TODO: Change BooleanFilterBuilder and BooleanQueryBuilder to auto-wrap choice of query or filters. This type of
104               code already exists in CachedFilter so could be reused.
105 -->     
106 <!ELEMENT Clause (%queries;|%filters;)>
107 <!-- Controls if the clause is optional (should), mandatory (must) or unacceptable (mustNot) -->
108 <!ATTLIST Clause occurs (should | must | mustnot) "should">
109
110
111 <!-- Caches any nested query or filter in an LRU (Least recently used) Cache. Cached queries, like filters, are turned into
112         Bitsets at a cost of 1 bit per document in the index. The memory cost of a cached query/filter is therefore numberOfDocsinIndex/8 bytes.
113         Queries that are cached as filters obviously retain none of the scoring information associated with results - they retain just
114         a Boolean yes/no record of which documents matched. 
115         @example 
116                 <em>Search for documents about banks from the last 10 years - caching the commonly-used "last 10 year" filter as a BitSet in 
117         RAM to eliminate the cost of building this filter from disk for every query</em>
118                 %                 
119             <FilteredQuery>
120                <Query>
121                   <UserQuery>bank</UserQuery>
122                </Query> 
123                <Filter>
124                   <CachedFilter>
125                      <RangeFilter fieldName="date" lowerTerm="19970101" upperTerm="20070101"/>
126                   </CachedFilter>
127                </Filter>        
128             </FilteredQuery>
129                  %
130         
131         -->
132 <!ELEMENT CachedFilter (%queries;|%filters;)>
133
134
135
136 <!--
137 Passes content directly through to the standard LuceneQuery parser see "Lucene Query Syntax"
138         @example 
139                 <em>Search for documents about John Smith or John Doe using standard LuceneQuerySyntax</em>
140                 %                 
141                <UserQuery>"John Smith" OR "John Doe"</UserQuery>
142                  %
143                 
144 -->
145 <!ELEMENT UserQuery (#PCDATA)>
146 <!-- Optional boost for matches on this query. Values > 1 -->
147 <!ATTLIST UserQuery boost CDATA "1.0">
148 <!-- fieldName can optionally be defined here to change the default field used in the QueryParser -->   
149 <!ATTLIST UserQuery fieldName CDATA #IMPLIED>
150
151 <!-- A query which is used to match all documents. This has a couple of uses: 
152         <ol>
153         <li> as a Clause in a BooleanQuery who's only other clause
154         is a "mustNot" match (Lucene requires at least one positive clause) and..</li>
155         <li> in a FilteredQuery where a Filter tag is effectively being 
156         used to select content rather than it's usual role of filtering the results of a query.</li>
157         </ol>
158         
159         @example 
160                 <em>Effectively use a Filter as a query </em>
161                 %                 
162                <FilteredQuery>
163                  <Query>
164                     <MatchAllDocsQuery/>
165                  </Query>
166                  <Filter>
167                      <RangeFilter fieldName="date" lowerTerm="19870409" upperTerm="19870412"/>
168                  </Filter>      
169                </FilteredQuery>          
170                %
171         
172 -->
173 <!ELEMENT MatchAllDocsQuery EMPTY>
174
175 <!-- a single term query - no analysis is done of the child text
176         @example 
177                 <em>Match on a primary key</em>
178                 %                 
179                <TermQuery fieldName="primaryKey">13424</TermQuery>
180                %        
181 -->     
182 <!ELEMENT TermQuery (#PCDATA)>
183 <!-- Optional boost for matches on this query. Values > 1 -->
184 <!ATTLIST TermQuery boost CDATA "1.0">
185 <!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->     
186 <!ATTLIST TermQuery fieldName CDATA #IMPLIED>
187
188
189 <!--
190   A boosted term query - no analysis is done of the child text. Also a span member.
191
192   (Text below is copied from the javadocs of BoostingTermQuery)
193    
194   The BoostingTermQuery is very similar to the {@link org.apache.lucene.search.spans.SpanTermQuery} except
195   that it factors in the value of the payload located at each of the positions where the
196   {@link org.apache.lucene.index.Term} occurs.
197
198   In order to take advantage of this, you must override {@link org.apache.lucene.search.Similarity#scorePayload(String, byte[],int,int)}
199   which returns 1 by default.
200
201   Payload scores are averaged across term occurrences in the document.
202
203   @see org.apache.lucene.search.Similarity#scorePayload(String, byte[], int, int)
204 -->
205 <!ELEMENT BoostingTermQuery (#PCDATA)>
206 <!-- Optional boost for matches on this query. Values > 1 -->
207 <!ATTLIST TermQuery boost CDATA "1.0">
208 <!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->
209 <!ATTLIST TermQuery fieldName CDATA #IMPLIED>
210
211
212
213 <!-- 
214         The equivalent of a BooleanQuery with multiple optional TermQuery clauses.
215         Child text is analyzed using a field-specific choice of Analyzer to produce a set of terms that are ORed together in Boolean logic.
216         Unlike UserQuery element, this does not parse any special characters to control fuzzy/phrase/boolean logic and as such is incapable
217         of producing a Query parse error given any user input
218         @example 
219                 <em>Match on text from a database description (which may contain characters that 
220         are illegal characters in the standard Lucene Query syntax used in the UserQuery tag</em>
221                 %                 
222                <TermsQuery fieldName="description">Smith & Sons (Ltd) : incorporated 1982</TermsQuery>
223                %        
224 -->     
225 <!ELEMENT TermsQuery (#PCDATA)>
226 <!-- Optional boost for matches on this query. Values > 1 -->
227 <!ATTLIST TermsQuery boost CDATA "1.0">
228 <!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->     
229 <!ATTLIST TermsQuery fieldName CDATA #IMPLIED>
230 <!-- The "Coordination factor" rewards documents that contain more of the terms in this list. This flag can be used to turn off this factor. -->
231 <!ATTLIST TermsQuery disableCoord (true | false) "false">
232 <!-- The minimum number of terms that should be present in any one document before it is considered to be a match. -->
233 <!ATTLIST TermsQuery minimumNumberShouldMatch CDATA "0">
234
235
236 <!-- 
237         Runs a Query and filters results to only those query matches that also match the Filter element.        
238         @example 
239                 <em>Find all documents about Lucene that have a status of "published"</em>
240                 %                 
241                <FilteredQuery>
242                  <Query>
243                     <UserQuery>Lucene</UserQuery>
244                  </Query>
245                  <Filter>
246                      <TermsFilter fieldName="status">published</TermsFilter>
247                  </Filter>      
248                </FilteredQuery>          
249                %        
250 -->     
251 <!ELEMENT FilteredQuery (Query,Filter)>
252 <!-- Optional boost for matches on this query. Values > 1 -->
253 <!ATTLIST FilteredQuery boost CDATA "1.0">
254 <!-- Used to identify a nested Query element inside another container element. NOT a top-level query tag  -->
255 <!ELEMENT Query (%queries;)>
256 <!-- The choice of Filter that MUST also be matched  -->
257 <!ELEMENT Filter (%filters;)>
258
259 <!--
260         Filter used to limit query results to documents matching a range of field values
261         @example 
262                 <em>Search for documents about banks from the last 10 years</em>
263                 %                 
264             <FilteredQuery>
265                <Query>
266                   <UserQuery>bank</UserQuery>
267                </Query> 
268                <Filter>
269                      <RangeFilter fieldName="date" lowerTerm="19970101" upperTerm="20070101"/>
270                </Filter>        
271             </FilteredQuery>
272                  %
273         -->
274 <!ELEMENT RangeFilter EMPTY>
275 <!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->     
276 <!ATTLIST RangeFilter fieldName CDATA #IMPLIED>
277 <!-- The lower-most term value for this field (must be <= upperTerm) -->
278 <!ATTLIST RangeFilter lowerTerm CDATA #REQUIRED>
279 <!-- The upper-most term value for this field (must be >= lowerTerm) -->
280 <!ATTLIST RangeFilter upperTerm CDATA #REQUIRED>
281 <!-- Controls if the lowerTerm in the range is part of the allowed set of values -->
282 <!ATTLIST RangeFilter includeLower (true | false) "true">
283 <!-- Controls if the upperTerm in the range is part of the allowed set of values -->
284 <!ATTLIST RangeFilter includeUpper (true | false) "true">
285
286 <!--
287         A Query that matches numeric values within a specified range.
288         @example 
289                 <em>Search for documents about people who are aged 20-25</em>
290                 %                 
291             <NumericRangeQuery fieldName="age" lowerTerm="20" upperTerm="25" />
292                  %
293         -->
294 <!ELEMENT NumericRangeQuery EMPTY>
295 <!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->     
296 <!ATTLIST NumericRangeQuery fieldName CDATA #IMPLIED>
297 <!-- The lower-most term value for this field (must be <= upperTerm and a valid native java numeric type) -->
298 <!ATTLIST NumericRangeQuery lowerTerm CDATA #REQUIRED>
299 <!-- The upper-most term value for this field (must be >= lowerTerm and a valid native java numeric type) -->
300 <!ATTLIST NumericRangeQuery upperTerm CDATA #REQUIRED>
301 <!-- The numeric type of this field -->
302 <!ATTLIST NumericRangeQuery type (int | long | float | double) "int">
303 <!-- Controls if the lowerTerm in the range is part of the allowed set of values -->
304 <!ATTLIST NumericRangeQuery includeLower (true | false) "true">
305 <!-- Controls if the upperTerm in the range is part of the allowed set of values -->
306 <!ATTLIST NumericRangeQuery includeUpper (true | false) "true">
307 <!-- Lower step values mean more precisions and so more terms in index (and index gets larger). This value must be an integer -->
308 <!ATTLIST NumericRangeQuery precisionStep CDATA "4">
309
310 <!--
311         A Filter that only accepts numeric values within a specified range
312         @example 
313                 <em>Search for documents about people who are aged 20-25</em>
314                 %                 
315             <FilteredQuery>
316                <Query>
317                   <UserQuery>person</UserQuery>
318                </Query> 
319                <Filter>
320                      <NumericRangeFilter fieldName="age" lowerTerm="20" upperTerm="25"/>
321                </Filter>        
322             </FilteredQuery>
323                  %
324         -->
325 <!ELEMENT NumericRangeFilter EMPTY>
326 <!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->     
327 <!ATTLIST NumericRangeFilter fieldName CDATA #IMPLIED>
328 <!-- The lower-most term value for this field (must be <= upperTerm and a valid native java numeric type) -->
329 <!ATTLIST NumericRangeFilter lowerTerm CDATA #REQUIRED>
330 <!-- The upper-most term value for this field (must be >= lowerTerm and a valid native java numeric type) -->
331 <!ATTLIST NumericRangeFilter upperTerm CDATA #REQUIRED>
332 <!-- The numeric type of this field -->
333 <!ATTLIST NumericRangeFilter type (int | long | float | double) "int">
334 <!-- Controls if the lowerTerm in the range is part of the allowed set of values -->
335 <!ATTLIST NumericRangeFilter includeLower (true | false) "true">
336 <!-- Controls if the upperTerm in the range is part of the allowed set of values -->
337 <!ATTLIST NumericRangeFilter includeUpper (true | false) "true">
338 <!-- Lower step values mean more precisions and so more terms in index (and index gets larger). This value must be an integer -->
339 <!ATTLIST NumericRangeFilter precisionStep CDATA "4">
340
341 <!-- A single term used in a SpanQuery. These clauses are the building blocks for more complex "span" queries which test word proximity
342         @example <em>Find documents using terms close to each other about mining and accidents</em>
343               %
344               <SpanNear slop="8" inOrder="false" fieldName="text">              
345                         <SpanOr>
346                                 <SpanTerm>killed</SpanTerm>
347                                 <SpanTerm>died</SpanTerm>
348                                 <SpanTerm>dead</SpanTerm>
349                         </SpanOr>
350                         <SpanOr>
351                                 <SpanTerm>miner</SpanTerm>
352                                 <SpanTerm>mining</SpanTerm>
353                                 <SpanTerm>miners</SpanTerm>
354                         </SpanOr>
355               </SpanNear>
356               %         
357         -->
358 <!ELEMENT SpanTerm (#PCDATA)>
359 <!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->     
360 <!ATTLIST SpanTerm fieldName CDATA #REQUIRED>
361
362 <!-- A field-specific analyzer is used here to parse the child text provided in this tag. The SpanTerms produced are ORed in terms of Boolean logic 
363         @example <em>Use SpanOrTerms as a more convenient/succinct way of expressing multiple choices of SpanTerms. This example looks for reports 
364         using words describing a fatality near to references to miners</em>
365               %
366               <SpanNear slop="8" inOrder="false" fieldName="text">              
367                         <SpanOrTerms>killed died death dead deaths</SpanOrTerms>
368                         <SpanOrTerms>miner mining miners</SpanOrTerms>
369               </SpanNear>
370               %         
371         -->
372 <!ELEMENT SpanOrTerms (#PCDATA)>
373 <!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->     
374 <!ATTLIST SpanOrTerms fieldName CDATA #REQUIRED>
375
376 <!-- Takes any number of child queries from the Span family 
377         @example <em>Find documents using terms close to each other about mining and accidents</em>
378               %
379               <SpanNear slop="8" inOrder="false" fieldName="text">              
380                         <SpanOr>
381                                 <SpanTerm>killed</SpanTerm>
382                                 <SpanTerm>died</SpanTerm>
383                                 <SpanTerm>dead</SpanTerm>
384                         </SpanOr>
385                         <SpanOr>
386                                 <SpanTerm>miner</SpanTerm>
387                                 <SpanTerm>mining</SpanTerm>
388                                 <SpanTerm>miners</SpanTerm>
389                         </SpanOr>
390               </SpanNear>
391               % 
392         
393         -->
394 <!ELEMENT SpanOr (%spanQueries;)* >
395
396 <!-- Takes any number of child queries from the Span family and tests for proximity
397         @hidden TODO SpanNear missing "boost attr (could add to SpanBuilderBase)
398         -->
399 <!ELEMENT SpanNear (%spanQueries;)* >
400 <!-- defines the maximum distance between Span elements where distance is expressed as word number, not byte offset 
401         @example <em>Find documents using terms within 8 words of each other talking about mining and accidents</em>
402               %
403               <SpanNear slop="8" inOrder="false" fieldName="text">              
404                         <SpanOr>
405                                 <SpanTerm>killed</SpanTerm>
406                                 <SpanTerm>died</SpanTerm>
407                                 <SpanTerm>dead</SpanTerm>
408                         </SpanOr>
409                         <SpanOr>
410                                 <SpanTerm>miner</SpanTerm>
411                                 <SpanTerm>mining</SpanTerm>
412                                 <SpanTerm>miners</SpanTerm>
413                         </SpanOr>
414               </SpanNear>
415               % 
416         -->
417 <!ATTLIST SpanNear slop CDATA #REQUIRED>
418 <!-- Controls if matching terms  have to appear in the order listed or can be reversed -->
419 <!ATTLIST SpanNear inOrder (true | false) "true">
420
421 <!-- Looks for a SpanQuery match occuring near the beginning of a document
422         
423         @example 
424                 <em>Find letters where the first 50 words talk about a resignation:</em>
425                 %                 
426                  <SpanFirst end="50">
427                        <SpanOrTerms fieldName="text">resigning resign leave</SpanOrTerms>
428                  </SpanFirst>
429                  %
430         
431          --> 
432 <!ELEMENT SpanFirst (%spanQueries;) >
433 <!-- Controls the end of the region considered in a document's field (expressed in word number, not byte offset) --> 
434 <!ATTLIST SpanFirst end CDATA #REQUIRED>
435 <!-- Optional boost for matches on this query. Values > 1 -->
436 <!ATTLIST SpanFirst boost CDATA "1.0">
437
438 <!-- Finds documents matching a SpanQuery but not if matching another SpanQuery 
439         @example <em>Find documents talking about social services but not containing the word "public"</em>
440               %
441           <SpanNot fieldName="text">
442              <Include>
443                 <SpanNear slop="2" inOrder="true">              
444                      <SpanTerm>social</SpanTerm>
445                      <SpanTerm>services</SpanTerm>
446                 </SpanNear>                             
447              </Include>
448              <Exclude>
449                 <SpanTerm>public</SpanTerm>
450              </Exclude>
451           </SpanNot>
452               % 
453         
454         -->
455 <!ELEMENT SpanNot (Include,Exclude) >
456 <!-- The SpanQuery to find -->
457 <!ELEMENT Include (%spanQueries;) >
458 <!-- The SpanQuery to be avoided -->
459 <!ELEMENT Exclude (%spanQueries;) >
460
461
462 <!-- a utility tag to wrap any filter as a query 
463         @example <em> Find all documents from the last 10 years </em>
464         %
465      <ConstantScoreQuery>
466            <RangeFilter fieldName="date" lowerTerm="19970101" upperTerm="20070101"/>
467      </ConstantScoreQuery>      
468         %
469         -->
470 <!ELEMENT ConstantScoreQuery (%filters;)* >
471 <!-- Optional boost for matches on this query. Values > 1 -->
472 <!ATTLIST ConstantScoreQuery boost CDATA "1.0">
473
474
475