home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Lucene example source code file (LuceneCoreQuery.dtd)

This example Lucene source code file (LuceneCoreQuery.dtd) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Lucene tags/keywords

attlist, attlist, cdata, cdata, element, element, filter, filteredquery, query, spannear, spanor, spanterm, spanterm, the

The Lucene LuceneCoreQuery.dtd source code

<!--
	<h3>Background
	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:
	<ol>
	<li>To open up Lucene functionality to clients other than Java
	<li>To offer a form of expressing queries that can easily be
	    <ul>
	        <li>Persisted for logging/auditing purposes
	        <li>Changed by editing text query templates (XSLT) without requiring a recompile/redeploy of applications
	        <li>Serialized across networks (without requiring Java bytecode for Query logic deployed on clients)
	    </ul>
	</li>
	<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>To bridge the growing gap between Lucene query/filtering functionality and the set of functionality accessible throught the standard Lucene QueryParser syntax
	<li>To provide a simply extensible syntax that does not require complex parser skills such as knowledge of JavaCC syntax
	</ol>
	
	
	<h3>Syntax overview
	Search syntax consists of two types of elements:
	<ul>
	<li>Queries
	<li>Filters
	</ul>

	<h4>Queries
	The root of any XML search must be a <i>Query type element used to select content.
	Queries typically score matches on documents using a number of different factors in order to provide relevant results first. 
	One common example of a query tag is the <a href="#UserQuery">UserQuery element which uses the standard 
	Lucene QueryParser to parse Google-style search syntax provided by end users.
	
	<h4>Filters
	Unlike Queries, <i>Filters are not used to select or score content - they are simply used to filter Query output (see FilteredQuery for an example use of query filtering).
	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 for
	subsequent reuse (see <a href="#CachedFilter">CachedFilter tag).

	<h4>Nesting elements
	Many of the the elements can nest other elements to produce queries/filters of an arbitrary depth and complexity. 
	The <a href="#BooleanQuery">BooleanQuery element is one such example which provides a means for combining other queries (including other BooleanQueries) using Boolean 
	logic to determine mandatory or optional elements. 

	
	<h3>Advanced topics	
	<h4>Advanced positional testing - span queries
	The <i>SpanQuery class of queries allow for complex positional tests which not only look for certain combinations of words but in particular 
	positions in relation to each other and the documents containing them.
	
	
	CoreParser.java is the Java class that encapsulates this parser behaviour.
	
	
	@title Core Lucene	
-->

<!-- @hidden Define core types of XML elements -->
<!ENTITY % coreSpanQueries "SpanOr|SpanNear|SpanOrTerms|SpanFirst|SpanNot|SpanTerm|BoostingTermQuery" >
<!ENTITY % coreQueries "BooleanQuery|UserQuery|FilteredQuery|TermQuery|TermsQuery|MatchAllDocsQuery|ConstantScoreQuery|BoostingTermQuery|NumericRangeQuery" >
<!ENTITY % coreFilters "RangeFilter|NumericRangeFilter|CachedFilter" >

<!-- @hidden Allow for extensions -->
<!ENTITY % extendedSpanQueries1 " " >
<!ENTITY % extendedQueries1 " " >
<!ENTITY % extendedFilters1 " " >

<!ENTITY % spanQueries "%coreSpanQueries;%extendedSpanQueries1;" >
<!ENTITY % queries "%coreQueries;|%spanQueries;%extendedQueries1;" >


<!ENTITY % filters "%coreFilters;%extendedFilters1;" >

<!--
	BooleanQuerys implement Boolean logic which controls how multiple Clauses should be interpreted.
	Some clauses may represent optional Query criteria while others represent mandatory criteria.	
	@example 
	        <em>Find articles about banks, preferably talking about mergers but nothing to do with "sumitomo"
	        %	          
            <BooleanQuery fieldName="contents">
	             <Clause occurs="should">
		              <TermQuery>merger
	             </Clause>
	             <Clause occurs="mustnot">
		              <TermQuery>sumitomo
	             </Clause>
	             <Clause occurs="must">
		              <TermQuery>bank
	             </Clause>
            </BooleanQuery>

	         %
-->	
<!ELEMENT BooleanQuery (Clause)+>
<!-- Optional boost for matches on this query. Values > 1 -->
<!ATTLIST BooleanQuery boost CDATA "1.0">
<!-- fieldName can optionally be defined here as a default attribute used by all child elements -->	
<!ATTLIST BooleanQuery fieldName CDATA #IMPLIED>
<!-- 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. -->
<!ATTLIST BooleanQuery disableCoord (true | false) "false">
<!-- The minimum number of optional clauses that should be present in any one document before it is considered to be a match. -->
<!ATTLIST BooleanQuery minimumNumberShouldMatch CDATA "0">

<!-- NOTE: "Clause" tag has 2 modes of use - inside 
               </Query>	
               <Filter>
                  <CachedFilter>
                     <RangeFilter fieldName="date" lowerTerm="19970101" upperTerm="20070101"/>
                  </CachedFilter>
               </Filter>	
            </FilteredQuery>
	         %
	
	-->
<!ELEMENT CachedFilter (%queries;|%filters;)>



<!--
Passes content directly through to the standard LuceneQuery parser see "Lucene Query Syntax"
	@example 
	        <em>Search for documents about John Smith or John Doe using standard LuceneQuerySyntax
	        %	          
               <UserQuery>"John Smith" OR "John Doe"
	         %
		
-->
<!ELEMENT UserQuery (#PCDATA)>
<!-- Optional boost for matches on this query. Values > 1 -->
<!ATTLIST UserQuery boost CDATA "1.0">
<!-- fieldName can optionally be defined here to change the default field used in the QueryParser -->	
<!ATTLIST UserQuery fieldName CDATA #IMPLIED>

<!-- A query which is used to match all documents. This has a couple of uses: 
	<ol>
	<li> as a Clause in a BooleanQuery who's only other clause
	is a "mustNot" match (Lucene requires at least one positive clause) and..</li>
	<li> in a FilteredQuery where a Filter tag is effectively being 
	used to select content rather than it's usual role of filtering the results of a query.</li>
	</ol>
	
	@example 
	        <em>Effectively use a Filter as a query 
	        %	          
               <FilteredQuery>
                 <Query>
                    <MatchAllDocsQuery/>
                 </Query>
                 <Filter>
                     <RangeFilter fieldName="date" lowerTerm="19870409" upperTerm="19870412"/>
                 </Filter>	
               </FilteredQuery>	         
	       %
	
-->
<!ELEMENT MatchAllDocsQuery EMPTY>

<!-- a single term query - no analysis is done of the child text
	@example 
	        <em>Match on a primary key
	        %	          
               <TermQuery fieldName="primaryKey">13424
	       %	
-->	
<!ELEMENT TermQuery (#PCDATA)>
<!-- Optional boost for matches on this query. Values > 1 -->
<!ATTLIST TermQuery boost CDATA "1.0">
<!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->	
<!ATTLIST TermQuery fieldName CDATA #IMPLIED>


<!--
  A boosted term query - no analysis is done of the child text. Also a span member.

  (Text below is copied from the javadocs of BoostingTermQuery)
   
  The BoostingTermQuery is very similar to the {@link org.apache.lucene.search.spans.SpanTermQuery} except
  that it factors in the value of the payload located at each of the positions where the
  {@link org.apache.lucene.index.Term} occurs.

  In order to take advantage of this, you must override {@link org.apache.lucene.search.Similarity#scorePayload(String, byte[],int,int)}
  which returns 1 by default.

  Payload scores are averaged across term occurrences in the document.

  @see org.apache.lucene.search.Similarity#scorePayload(String, byte[], int, int)
-->
<!ELEMENT BoostingTermQuery (#PCDATA)>
<!-- Optional boost for matches on this query. Values > 1 -->
<!ATTLIST TermQuery boost CDATA "1.0">
<!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->
<!ATTLIST TermQuery fieldName CDATA #IMPLIED>



<!-- 
	The equivalent of a BooleanQuery with multiple optional TermQuery clauses.
	Child text is analyzed using a field-specific choice of Analyzer to produce a set of terms that are ORed together in Boolean logic.
	Unlike UserQuery element, this does not parse any special characters to control fuzzy/phrase/boolean logic and as such is incapable
	of producing a Query parse error given any user input
	@example 
	        <em>Match on text from a database description (which may contain characters that 
	are illegal characters in the standard Lucene Query syntax used in the UserQuery tag</em>
	        %	          
               <TermsQuery fieldName="description">Smith & Sons (Ltd) : incorporated 1982
	       %	
-->	
<!ELEMENT TermsQuery (#PCDATA)>
<!-- Optional boost for matches on this query. Values > 1 -->
<!ATTLIST TermsQuery boost CDATA "1.0">
<!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->	
<!ATTLIST TermsQuery fieldName CDATA #IMPLIED>
<!-- The "Coordination factor" rewards documents that contain more of the terms in this list. This flag can be used to turn off this factor. -->
<!ATTLIST TermsQuery disableCoord (true | false) "false">
<!-- The minimum number of terms that should be present in any one document before it is considered to be a match. -->
<!ATTLIST TermsQuery minimumNumberShouldMatch CDATA "0">


<!-- 
	Runs a Query and filters results to only those query matches that also match the Filter element.	
	@example 
	        <em>Find all documents about Lucene that have a status of "published"
	        %	          
               <FilteredQuery>
                 <Query>
                    <UserQuery>Lucene
                 </Query>
                 <Filter>
                     <TermsFilter fieldName="status">published
                 </Filter>	
               </FilteredQuery>	         
	       %	
-->	
<!ELEMENT FilteredQuery (Query,Filter)>
<!-- Optional boost for matches on this query. Values > 1 -->
<!ATTLIST FilteredQuery boost CDATA "1.0">
<!-- Used to identify a nested Query element inside another container element. NOT a top-level query tag  -->
<!ELEMENT Query (%queries;)>
<!-- The choice of Filter that MUST also be matched  -->
<!ELEMENT Filter (%filters;)>

<!--
	Filter used to limit query results to documents matching a range of field values
	@example 
	        <em>Search for documents about banks from the last 10 years
	        %	          
            <FilteredQuery>
               <Query>
                  <UserQuery>bank
               </Query>	
               <Filter>
                     <RangeFilter fieldName="date" lowerTerm="19970101" upperTerm="20070101"/>
               </Filter>	
            </FilteredQuery>
	         %
	-->
<!ELEMENT RangeFilter EMPTY>
<!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->	
<!ATTLIST RangeFilter fieldName CDATA #IMPLIED>
<!-- The lower-most term value for this field (must be <= upperTerm) -->
<!ATTLIST RangeFilter lowerTerm CDATA #REQUIRED>
<!-- The upper-most term value for this field (must be >= lowerTerm) -->
<!ATTLIST RangeFilter upperTerm CDATA #REQUIRED>
<!-- Controls if the lowerTerm in the range is part of the allowed set of values -->
<!ATTLIST RangeFilter includeLower (true | false) "true">
<!-- Controls if the upperTerm in the range is part of the allowed set of values -->
<!ATTLIST RangeFilter includeUpper (true | false) "true">

<!--
	A Query that matches numeric values within a specified range.
	@example 
	        <em>Search for documents about people who are aged 20-25
	        %	          
            <NumericRangeQuery fieldName="age" lowerTerm="20" upperTerm="25" />
	         %
	-->
<!ELEMENT NumericRangeQuery EMPTY>
<!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->	
<!ATTLIST NumericRangeQuery fieldName CDATA #IMPLIED>
<!-- The lower-most term value for this field (must be <= upperTerm and a valid native java numeric type) -->
<!ATTLIST NumericRangeQuery lowerTerm CDATA #REQUIRED>
<!-- The upper-most term value for this field (must be >= lowerTerm and a valid native java numeric type) -->
<!ATTLIST NumericRangeQuery upperTerm CDATA #REQUIRED>
<!-- The numeric type of this field -->
<!ATTLIST NumericRangeQuery type (int | long | float | double) "int">
<!-- Controls if the lowerTerm in the range is part of the allowed set of values -->
<!ATTLIST NumericRangeQuery includeLower (true | false) "true">
<!-- Controls if the upperTerm in the range is part of the allowed set of values -->
<!ATTLIST NumericRangeQuery includeUpper (true | false) "true">
<!-- Lower step values mean more precisions and so more terms in index (and index gets larger). This value must be an integer -->
<!ATTLIST NumericRangeQuery precisionStep CDATA "4">

<!--
	A Filter that only accepts numeric values within a specified range
	@example 
	        <em>Search for documents about people who are aged 20-25
	        %	          
            <FilteredQuery>
               <Query>
                  <UserQuery>person
               </Query>	
               <Filter>
                     <NumericRangeFilter fieldName="age" lowerTerm="20" upperTerm="25"/>
               </Filter>	
            </FilteredQuery>
	         %
	-->
<!ELEMENT NumericRangeFilter EMPTY>
<!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->	
<!ATTLIST NumericRangeFilter fieldName CDATA #IMPLIED>
<!-- The lower-most term value for this field (must be <= upperTerm and a valid native java numeric type) -->
<!ATTLIST NumericRangeFilter lowerTerm CDATA #REQUIRED>
<!-- The upper-most term value for this field (must be >= lowerTerm and a valid native java numeric type) -->
<!ATTLIST NumericRangeFilter upperTerm CDATA #REQUIRED>
<!-- The numeric type of this field -->
<!ATTLIST NumericRangeFilter type (int | long | float | double) "int">
<!-- Controls if the lowerTerm in the range is part of the allowed set of values -->
<!ATTLIST NumericRangeFilter includeLower (true | false) "true">
<!-- Controls if the upperTerm in the range is part of the allowed set of values -->
<!ATTLIST NumericRangeFilter includeUpper (true | false) "true">
<!-- Lower step values mean more precisions and so more terms in index (and index gets larger). This value must be an integer -->
<!ATTLIST NumericRangeFilter precisionStep CDATA "4">

<!-- A single term used in a SpanQuery. These clauses are the building blocks for more complex "span" queries which test word proximity
	@example <em>Find documents using terms close to each other about mining and accidents
	      %
	      <SpanNear slop="8" inOrder="false" fieldName="text">		
			<SpanOr>
				<SpanTerm>killed
				<SpanTerm>died
				<SpanTerm>dead
			</SpanOr>
			<SpanOr>
				<SpanTerm>miner
				<SpanTerm>mining
				<SpanTerm>miners
			</SpanOr>
	      </SpanNear>
	      % 	
	-->
<!ELEMENT SpanTerm (#PCDATA)>
<!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->	
<!ATTLIST SpanTerm fieldName CDATA #REQUIRED>

<!-- 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 
	@example <em>Use SpanOrTerms as a more convenient/succinct way of expressing multiple choices of SpanTerms. This example looks for reports 
	using words describing a fatality near to references to miners</em>
	      %
	      <SpanNear slop="8" inOrder="false" fieldName="text">		
			<SpanOrTerms>killed died death dead deaths
			<SpanOrTerms>miner mining miners
	      </SpanNear>
	      % 	
	-->
<!ELEMENT SpanOrTerms (#PCDATA)>
<!-- fieldName must be defined here or is taken from the most immediate parent XML element that defines a "fieldName" attribute -->	
<!ATTLIST SpanOrTerms fieldName CDATA #REQUIRED>

<!-- Takes any number of child queries from the Span family 
	@example <em>Find documents using terms close to each other about mining and accidents
	      %
	      <SpanNear slop="8" inOrder="false" fieldName="text">		
			<SpanOr>
				<SpanTerm>killed
				<SpanTerm>died
				<SpanTerm>dead
			</SpanOr>
			<SpanOr>
				<SpanTerm>miner
				<SpanTerm>mining
				<SpanTerm>miners
			</SpanOr>
	      </SpanNear>
	      %	
	
	-->
<!ELEMENT SpanOr (%spanQueries;)* >

<!-- Takes any number of child queries from the Span family and tests for proximity
	@hidden TODO SpanNear missing "boost attr (could add to SpanBuilderBase)
	-->
<!ELEMENT SpanNear (%spanQueries;)* >
<!-- defines the maximum distance between Span elements where distance is expressed as word number, not byte offset 
	@example <em>Find documents using terms within 8 words of each other talking about mining and accidents
	      %
	      <SpanNear slop="8" inOrder="false" fieldName="text">		
			<SpanOr>
				<SpanTerm>killed
				<SpanTerm>died
				<SpanTerm>dead
			</SpanOr>
			<SpanOr>
				<SpanTerm>miner
				<SpanTerm>mining
				<SpanTerm>miners
			</SpanOr>
	      </SpanNear>
	      %	
	-->
<!ATTLIST SpanNear slop CDATA #REQUIRED>
<!-- Controls if matching terms  have to appear in the order listed or can be reversed -->
<!ATTLIST SpanNear inOrder (true | false) "true">

<!-- Looks for a SpanQuery match occuring near the beginning of a document
	
	@example 
	        <em>Find letters where the first 50 words talk about a resignation:
	        %	          
	         <SpanFirst end="50">
	               <SpanOrTerms fieldName="text">resigning resign leave
	         </SpanFirst>
	         %
	
	 --> 
<!ELEMENT SpanFirst (%spanQueries;) >
<!-- Controls the end of the region considered in a document's field (expressed in word number, not byte offset) --> 
<!ATTLIST SpanFirst end CDATA #REQUIRED>
<!-- Optional boost for matches on this query. Values > 1 -->
<!ATTLIST SpanFirst boost CDATA "1.0">

<!-- Finds documents matching a SpanQuery but not if matching another SpanQuery 
	@example <em>Find documents talking about social services but not containing the word "public"
	      %
          <SpanNot fieldName="text">
             <Include>
                <SpanNear slop="2" inOrder="true">		
                     <SpanTerm>social
                     <SpanTerm>services
                </SpanNear>				
             </Include>
             <Exclude>
                <SpanTerm>public
             </Exclude>
          </SpanNot>
	      %	
	
	-->
<!ELEMENT SpanNot (Include,Exclude) >
<!-- The SpanQuery to find -->
<!ELEMENT Include (%spanQueries;) >
<!-- The SpanQuery to be avoided -->
<!ELEMENT Exclude (%spanQueries;) >


<!-- a utility tag to wrap any filter as a query 
	@example <em> Find all documents from the last 10 years 
	%
     <ConstantScoreQuery>
           <RangeFilter fieldName="date" lowerTerm="19970101" upperTerm="20070101"/>
     </ConstantScoreQuery>	
	%
	-->
<!ELEMENT ConstantScoreQuery (%filters;)* >
<!-- Optional boost for matches on this query. Values > 1 -->
<!ATTLIST ConstantScoreQuery boost CDATA "1.0">



Other Lucene examples (source code examples)

Here is a short list of links related to this Lucene LuceneCoreQuery.dtd source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.