Query Language | Zend_Search_Lucene Programmer's Reference Guide | Character Set

# Query Construction API

In 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:

1. $query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);

# Query Parser Exceptions

The query parser may generate two types of exceptions:

- Zend_Search_Lucene_Exception is thrown if something goes wrong in the query parser itself.

- Zend_Search_Lucene_Search_QueryParserException is thrown when there is an error in the query syntax.

It's a good idea to catch Zend_Search_Lucene_Search_QueryParserExceptions and handle them appropriately:

1. try {
2. $query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
3. } catch (Zend_Search_Lucene_Search_QueryParserException $e) {
4. echo "Query syntax error: " . $e->getMessage() . "\n";
5. }

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 Query

Term queries can be used for searching with a single term.

Query string:

1. word1

or

Query construction by API:

1. $term = new Zend_Search_Lucene_Index_Term('word1', 'field1');
2. $query = new Zend_Search_Lucene_Search_Query_Term($term);
3. $hits = $index->find($query);

The term field is optional. Zend_Search_Lucene searches through all indexed fields in each document if the field is not specified:

1. // Search for 'word1' in all indexed fields
2. $term = new Zend_Search_Lucene_Index_Term('word1');
3. $query = new Zend_Search_Lucene_Search_Query_Term($term);
4. $hits = $index->find($query);

# Multi-Term Query

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

- required means that documents not matching this term will not match the query;

- prohibited means that documents matching this term will not match the query;

- neither, in which case matched documents are neither prohibited from, nor required to, match the term. A document must match at least 1 term, however, to match the query.

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:

1. +word1 author:word2 -word3

- '+' is used to define a required term.

- '-' is used to define a prohibited term.

- 'field:' prefix is used to indicate a document field for a search. If it's omitted, then all fields are searched.

or

Query construction by API:

1. $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
3. $query->addTerm(new Zend_Search_Lucene_Index_Term('word1'), true);
4. $query->addTerm(new Zend_Search_Lucene_Index_Term('word2', 'author'),
5. null);
6. $query->addTerm(new Zend_Search_Lucene_Index_Term('word3'), false);
8. $hits = $index->find($query);

It's also possible to specify terms list within MultiTerm query constructor:

1. $terms = array(new Zend_Search_Lucene_Index_Term('word1'),
2. new Zend_Search_Lucene_Index_Term('word2', 'author'),
3. new Zend_Search_Lucene_Index_Term('word3'));
4. $signs = array(true, null, false);
6. $query = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $signs);
8. $hits = $index->find($query);

The $signs array contains information about the term type:

- `TRUE` is used to define required term.

- `FALSE` is used to define prohibited term.

- `NULL` is used to define a term that is neither required nor prohibited.

# Boolean Query

Boolean queries allow to construct query using other queries and boolean operators.

Each subquery in a set can be defined as required, prohibited, or optional.

- required means that documents not matching this subquery will not match the query;

- prohibited means that documents matching this subquery will not match the query;

- optional, in which case matched documents are neither prohibited from, nor required to, match the subquery. A document must match at least 1 subquery, however, to match the query.

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:

1. +(word1 word2 word3) (author:word4 author:word5) -(word6)

- '+' is used to define a required subquery.

- '-' is used to define a prohibited subquery.

- 'field:' prefix is used to indicate a document field for a search. If it's omitted, then all fields are searched.

or

Query construction by API:

1. $query = new Zend_Search_Lucene_Search_Query_Boolean();
3. $subquery1 = new Zend_Search_Lucene_Search_Query_MultiTerm();
4. $subquery1->addTerm(new Zend_Search_Lucene_Index_Term('word1'));
5. $subquery1->addTerm(new Zend_Search_Lucene_Index_Term('word2'));
6. $subquery1->addTerm(new Zend_Search_Lucene_Index_Term('word3'));
8. $subquery2 = new Zend_Search_Lucene_Search_Query_MultiTerm();
9. $subquery2->addTerm(new Zend_Search_Lucene_Index_Term('word4', 'author'));
10. $subquery2->addTerm(new Zend_Search_Lucene_Index_Term('word5', 'author'));
12. $term6 = new Zend_Search_Lucene_Index_Term('word6');
13. $subquery3 = new Zend_Search_Lucene_Search_Query_Term($term6);
15. $query->addSubquery($subquery1, true /* required */);
16. $query->addSubquery($subquery2, null /* optional */);
17. $query->addSubquery($subquery3, false /* prohibited */);
19. $hits = $index->find($query);

It's also possible to specify subqueries list within Boolean query constructor:

1. ...
2. $subqueries = array($subquery1, $subquery2, $subquery3);
3. $signs = array(true, null, false);
5. $query = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);
7. $hits = $index->find($query);

The $signs array contains information about the subquery type:

- `TRUE` is used to define required subquery.

- `FALSE` is used to define prohibited subquery.

- `NULL` is used to define a subquery that is neither required nor prohibited.

Each query which uses boolean operators can be rewritten using signs notation and constructed using API. For example:

1. word1 AND (word2 AND word3 AND NOT word4) OR word5

is equivalent to

1. (+(word1) +(+word2 +word3 -word4)) (word5)

# Wildcard Query

Wildcard 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:

1. field1:test*

or

Query construction by API:

1. $pattern = new Zend_Search_Lucene_Index_Term('test*', 'field1');
2. $query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
3. $hits = $index->find($query);

The term field is optional. Zend_Search_Lucene searches through all fields on each document if a field is not specified:

1. $pattern = new Zend_Search_Lucene_Index_Term('test*');
2. $query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
3. $hits = $index->find($query);

# Fuzzy Query

Fuzzy queries can be used to search for documents containing strings matching terms similar to specified term.

Query string:

1. field1:test~

This query matches documents containing 'test' 'text' 'best' words and others.

or

Query construction by API:

1. $term = new Zend_Search_Lucene_Index_Term('test', 'field1');
2. $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term);
3. $hits = $index->find($query);

Optional similarity can be specified after "~" sign.

Query string:

1. field1:test~0.4

or

Query construction by API:

1. $term = new Zend_Search_Lucene_Index_Term('test', 'field1');
2. $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term, 0.4);
3. $hits = $index->find($query);

The term field is optional. Zend_Search_Lucene searches through all fields on each document if a field is not specified:

1. $term = new Zend_Search_Lucene_Index_Term('test');
2. $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term);
3. $hits = $index->find($query);

# Phrase Query

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

1. $query1 = new Zend_Search_Lucene_Search_Query_Phrase();
3. // Add 'word1' at 0 relative position.
4. $query1->addTerm(new Zend_Search_Lucene_Index_Term('word1'));
6. // Add 'word2' at 1 relative position.
7. $query1->addTerm(new Zend_Search_Lucene_Index_Term('word2'));
9. // Add 'word3' at 3 relative position.
10. $query1->addTerm(new Zend_Search_Lucene_Index_Term('word3'), 3);
12. ...
14. $query2 = new Zend_Search_Lucene_Search_Query_Phrase(
15. array('word1', 'word2', 'word3'), array(0,1,3));
17. ...
19. // Query without a gap.
20. $query3 = new Zend_Search_Lucene_Search_Query_Phrase(
21. array('word1', 'word2', 'word3'));
23. ...
25. $query4 = new Zend_Search_Lucene_Search_Query_Phrase(
26. array('word1', 'word2'), array(0,1), 'annotation');

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:

1. Zend_Search_Lucene_Search_Query_Phrase(
2. [array $terms[, array $offsets[, string $field]]]
3. );

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:

1. $query =
2. new Zend_Search_Lucene_Search_Query_Phrase(array('zend', 'framework'));

will search for the phrase 'zend framework' in all fields.

1. $query = new Zend_Search_Lucene_Search_Query_Phrase(
2. array('zend', 'download'), array(0, 2)
3. );

will search for the phrase 'zend ????? download' and match 'zend platform download', 'zend studio download', 'zend core download', 'zend framework download', and so on.

1. $query = new Zend_Search_Lucene_Search_Query_Phrase(
2. array('zend', 'framework'), null, 'title'
3. );

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:

1. Zend_Search_Lucene_Search_Query_Phrase::addTerm(
2. Zend_Search_Lucene_Index_Term $term[, integer $position]
3. );

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:

1. $query = new Zend_Search_Lucene_Search_Query_Phrase();
2. $query->addTerm(new Zend_Search_Lucene_Index_Term('zend'));
3. $query->addTerm(new Zend_Search_Lucene_Index_Term('framework'));

will search for the phrase 'zend framework'.

1. $query = new Zend_Search_Lucene_Search_Query_Phrase();
2. $query->addTerm(new Zend_Search_Lucene_Index_Term('zend'), 0);
3. $query->addTerm(new Zend_Search_Lucene_Index_Term('framework'), 2);

will search for the phrase 'zend ????? download' and match 'zend platform download', 'zend studio download', 'zend core download', 'zend framework download', and so on.

1. $query = new Zend_Search_Lucene_Search_Query_Phrase();
2. $query->addTerm(new Zend_Search_Lucene_Index_Term('zend', 'title'));
3. $query->addTerm(new Zend_Search_Lucene_Index_Term('framework', 'title'));

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:

1. // Query without a gap.
2. $query =
3. new Zend_Search_Lucene_Search_Query_Phrase(array('word1', 'word2'));
5. // Search for 'word1 word2', 'word1 ... word2'
6. $query->setSlop(1);
7. $hits1 = $index->find($query);
9. // Search for 'word1 word2', 'word1 ... word2',
10. // 'word1 ... ... word2', 'word2 word1'
11. $query->setSlop(2);
12. $hits2 = $index->find($query);

# Range Query

Range queries are intended for searching terms within specified interval.

Query string:

1. mod_date:[20020101 TO 20030101]
2. title:{Aida TO Carmen}

or

Query construction by API:

1. $from = new Zend_Search_Lucene_Index_Term('20020101', 'mod_date');
2. $to = new Zend_Search_Lucene_Index_Term('20030101', 'mod_date');
3. $query = new Zend_Search_Lucene_Search_Query_Range(
4. $from, $to, true // inclusive
5. );
6. $hits = $index->find($query);

Term fields are optional. Zend_Search_Lucene searches through all fields if the field is not specified:

1. $from = new Zend_Search_Lucene_Index_Term('Aida');
2. $to = new Zend_Search_Lucene_Index_Term('Carmen');
3. $query = new Zend_Search_Lucene_Search_Query_Range(
4. $from, $to, false // non-inclusive
5. );
6. $hits = $index->find($query);

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:

1. // searches for ['20020101' TO ...]
2. $from = new Zend_Search_Lucene_Index_Term('20020101', 'mod_date');
3. $query = new Zend_Search_Lucene_Search_Query_Range(
4. $from, null, true // inclusive
5. );
6. $hits = $index->find($query);

Query Language | Zend_Search_Lucene Programmer's Reference Guide | Character Set