Query Construction APIIn addition to parsing a string query automatically it's also possible to construct them with the query API. User queries can be combined with queries created through the query API. Simply use the query parser to construct a query from a string: Query Parser ExceptionsThe query parser may generate two types of exceptions:
The same technique should be used for the find() method of a Zend_Search_Lucene object. Starting in 1.5, query parsing exceptions are suppressed by default. If query doesn't conform query language, then it's tokenized using current default analyzer and all tokenized terms are used for searching. Use Zend_Search_Lucene_Search_QueryParser::dontSuppressQueryParsingExceptions() method to turn exceptions on. Zend_Search_Lucene_Search_QueryParser::suppressQueryParsingExceptions() and Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed() methods are also intended to manage exceptions handling behavior. Term QueryTerm queries can be used for searching with a single term. Query string:
or Query construction by API:
The term field is optional. Zend_Search_Lucene searches through all indexed fields in each document if the field is not specified:
Multi-Term QueryMulti-term queries can be used for searching with a set of terms. Each term in a set can be defined as required, prohibited, or neither.
If optional terms are added to a query with required terms, both queries will have the same result set but the optional terms may affect the score of the matched documents. Both search methods can be used for multi-term queries. Query string:
or Query construction by API:
It's also possible to specify terms list within MultiTerm query constructor:
The $signs array contains information about the term type:
Boolean QueryBoolean queries allow to construct query using other queries and boolean operators. Each subquery in a set can be defined as required, prohibited, or optional.
If optional subqueries are added to a query with required subqueries, both queries will have the same result set but the optional subqueries may affect the score of the matched documents. Both search methods can be used for boolean queries. Query string:
or Query construction by API:
It's also possible to specify subqueries list within Boolean query constructor: The $signs array contains information about the subquery type:
Each query which uses boolean operators can be rewritten using signs notation and constructed using API. For example:
is equivalent to
Wildcard QueryWildcard queries can be used to search for documents containing strings matching specified patterns. The '?' symbol is used as a single character wildcard. The '*' symbol is used as a multiple character wildcard. Query string:
or Query construction by API:
The term field is optional. Zend_Search_Lucene searches through all fields on each document if a field is not specified:
Fuzzy QueryFuzzy queries can be used to search for documents containing strings matching terms similar to specified term. Query string:
This query matches documents containing 'test' 'text' 'best' words and others. or Query construction by API:
Optional similarity can be specified after "~" sign. Query string:
or Query construction by API:
The term field is optional. Zend_Search_Lucene searches through all fields on each document if a field is not specified:
Phrase QueryPhrase Queries can be used to search for a phrase within documents. Phrase Queries are very flexible and allow the user or developer to search for exact phrases as well as 'sloppy' phrases. Phrases can also contain gaps or terms in the same places; they can be generated by the analyzer for different purposes. For example, a term can be duplicated to increase the term its weight, or several synonyms can be placed into a single position.
A phrase query can be constructed in one step with a class constructor or step by step with Zend_Search_Lucene_Search_Query_Phrase::addTerm() method calls. Zend_Search_Lucene_Search_Query_Phrase class constructor takes three optional arguments: The $terms parameter is an array of strings that contains a set of phrase terms. If it's omitted or equal to NULL, then an empty query is constructed. The $offsets parameter is an array of integers that contains offsets of terms in a phrase. If it's omitted or equal to NULL, then the terms' positions are assumed to be sequential with no gaps. The $field parameter is a string that indicates the document field to search. If it's omitted or equal to NULL, then the default field is searched. Thus:
will search for the phrase 'zend framework' in all fields.
will search for the phrase 'zend ????? download' and match 'zend platform download', 'zend studio download', 'zend core download', 'zend framework download', and so on.
will search for the phrase 'zend framework' in the 'title' field. Zend_Search_Lucene_Search_Query_Phrase::addTerm() takes two arguments, a required Zend_Search_Lucene_Index_Term object and an optional position: The $term parameter describes the next term in the phrase. It must indicate the same field as previous terms, or an exception will be thrown. The $position parameter indicates the term position in the phrase. Thus:
will search for the phrase 'zend framework'.
will search for the phrase 'zend ????? download' and match 'zend platform download', 'zend studio download', 'zend core download', 'zend framework download', and so on.
will search for the phrase 'zend framework' in the 'title' field. The slop factor sets the number of other words permitted between specified words in the query phrase. If set to zero, then the corresponding query is an exact phrase search. For larger values this works like the WITHIN or NEAR operators. The slop factor is in fact an edit distance, where the edits correspond to moving terms in the query phrase. For example, to switch the order of two words requires two moves (the first move places the words atop one another), so to permit re-orderings of phrases, the slop factor must be at least two. More exact matches are scored higher than sloppier matches; thus, search results are sorted by exactness. The slop is zero by default, requiring exact matches. The slop factor can be assigned after query creation:
Range QueryRange queries are intended for searching terms within specified interval. Query string:
or Query construction by API:
Term fields are optional. Zend_Search_Lucene searches through all fields if the field is not specified:
Either (but not both) of the boundary terms may be set to NULL. Zend_Search_Lucene searches from the beginning or up to the end of the dictionary for the specified field(s) in this case:
|