1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.search.lucene.query-api">
4 <title>Query Construction API</title>
7 In addition to parsing a string query automatically it's also possible to construct them
8 with the query <acronym>API</acronym>.
12 User queries can be combined with queries created through the query <acronym>API</acronym>.
13 Simply use the query parser to construct a query from a string:
16 <programlisting language="php"><![CDATA[
17 $query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
20 <sect2 id="zend.search.lucene.queries.exceptions">
21 <title>Query Parser Exceptions</title>
24 The query parser may generate two types of exceptions:
29 <classname>Zend_Search_Lucene_Exception</classname> is thrown if something
30 goes wrong in the query parser itself.
36 <classname>Zend_Search_Lucene_Search_QueryParserException</classname> is
37 thrown when there is an error in the query syntax.
42 It's a good idea to catch
43 <classname>Zend_Search_Lucene_Search_QueryParserException</classname>s and handle them
47 <programlisting language="php"><![CDATA[
49 $query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
50 } catch (Zend_Search_Lucene_Search_QueryParserException $e) {
51 echo "Query syntax error: " . $e->getMessage() . "\n";
56 The same technique should be used for the find() method of a
57 <classname>Zend_Search_Lucene</classname> object.
61 Starting in 1.5, query parsing exceptions are suppressed by default. If query doesn't
62 conform query language, then it's tokenized using current default analyzer and all
63 tokenized terms are used for searching. Use
64 <methodname>Zend_Search_Lucene_Search_QueryParser::dontSuppressQueryParsingExceptions()</methodname>
65 method to turn exceptions on.
66 <methodname>Zend_Search_Lucene_Search_QueryParser::suppressQueryParsingExceptions()</methodname>
68 <methodname>Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed()</methodname>
69 methods are also intended to manage exceptions handling behavior.
73 <sect2 id="zend.search.lucene.queries.term-query">
74 <title>Term Query</title>
77 Term queries can be used for searching with a single term.
84 <programlisting language="querystring"><![CDATA[
91 Query construction by <acronym>API</acronym>:
94 <programlisting language="php"><![CDATA[
95 $term = new Zend_Search_Lucene_Index_Term('word1', 'field1');
96 $query = new Zend_Search_Lucene_Search_Query_Term($term);
97 $hits = $index->find($query);
101 The term field is optional. <classname>Zend_Search_Lucene</classname> searches through
102 all indexed fields in each document if the field is not specified:
105 <programlisting language="php"><![CDATA[
106 // Search for 'word1' in all indexed fields
107 $term = new Zend_Search_Lucene_Index_Term('word1');
108 $query = new Zend_Search_Lucene_Search_Query_Term($term);
109 $hits = $index->find($query);
113 <sect2 id="zend.search.lucene.queries.multiterm-query">
114 <title>Multi-Term Query</title>
117 Multi-term queries can be used for searching with a set of terms.
121 Each term in a set can be defined as <emphasis>required</emphasis>,
122 <emphasis>prohibited</emphasis>, or <emphasis>neither</emphasis>.
127 <emphasis>required</emphasis> means that documents not matching this term
128 will not match the query;
134 <emphasis>prohibited</emphasis> means that documents matching this term will
141 <emphasis>neither</emphasis>, in which case matched documents are neither
142 prohibited from, nor required to, match the term. A document must match at
143 least 1 term, however, to match the query.
150 If optional terms are added to a query with required terms, both queries will have the
151 same result set but the optional terms may affect the score of the matched documents.
155 Both search methods can be used for multi-term queries.
162 <programlisting language="querystring"><![CDATA[
163 +word1 author:word2 -word3
167 <listitem><para>'+' is used to define a required term.</para></listitem>
168 <listitem><para>'-' is used to define a prohibited term.</para></listitem>
172 'field:' prefix is used to indicate a document field for a search.
173 If it's omitted, then all fields are searched.
181 Query construction by <acronym>API</acronym>:
184 <programlisting language="php"><![CDATA[
185 $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
187 $query->addTerm(new Zend_Search_Lucene_Index_Term('word1'), true);
188 $query->addTerm(new Zend_Search_Lucene_Index_Term('word2', 'author'),
190 $query->addTerm(new Zend_Search_Lucene_Index_Term('word3'), false);
192 $hits = $index->find($query);
196 It's also possible to specify terms list within MultiTerm query constructor:
199 <programlisting language="php"><![CDATA[
200 $terms = array(new Zend_Search_Lucene_Index_Term('word1'),
201 new Zend_Search_Lucene_Index_Term('word2', 'author'),
202 new Zend_Search_Lucene_Index_Term('word3'));
203 $signs = array(true, null, false);
205 $query = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $signs);
207 $hits = $index->find($query);
211 The <varname>$signs</varname> array contains information about the term type:
216 <constant>TRUE</constant> is used to define required term.
222 <constant>FALSE</constant> is used to define prohibited term.
228 <constant>NULL</constant> is used to define a term that is neither required
236 <sect2 id="zend.search.lucene.queries.boolean-query">
237 <title>Boolean Query</title>
240 Boolean queries allow to construct query using other queries and boolean operators.
244 Each subquery in a set can be defined as <emphasis>required</emphasis>,
245 <emphasis>prohibited</emphasis>, or <emphasis>optional</emphasis>.
250 <emphasis>required</emphasis> means that documents not matching this
251 subquery will not match the query;
257 <emphasis>prohibited</emphasis> means that documents matching this subquery
258 will not match the query;
264 <emphasis>optional</emphasis>, in which case matched documents are neither
265 prohibited from, nor required to, match the subquery. A document must match
266 at least 1 subquery, however, to match the query.
273 If optional subqueries are added to a query with required subqueries, both queries will
274 have the same result set but the optional subqueries may affect the score of the matched
279 Both search methods can be used for boolean queries.
286 <programlisting language="querystring"><![CDATA[
287 +(word1 word2 word3) (author:word4 author:word5) -(word6)
293 '+' is used to define a required subquery.
299 '-' is used to define a prohibited subquery.
305 'field:' prefix is used to indicate a document field for a search.
306 If it's omitted, then all fields are searched.
314 Query construction by <acronym>API</acronym>:
317 <programlisting language="php"><![CDATA[
318 $query = new Zend_Search_Lucene_Search_Query_Boolean();
320 $subquery1 = new Zend_Search_Lucene_Search_Query_MultiTerm();
321 $subquery1->addTerm(new Zend_Search_Lucene_Index_Term('word1'));
322 $subquery1->addTerm(new Zend_Search_Lucene_Index_Term('word2'));
323 $subquery1->addTerm(new Zend_Search_Lucene_Index_Term('word3'));
325 $subquery2 = new Zend_Search_Lucene_Search_Query_MultiTerm();
326 $subquery2->addTerm(new Zend_Search_Lucene_Index_Term('word4', 'author'));
327 $subquery2->addTerm(new Zend_Search_Lucene_Index_Term('word5', 'author'));
329 $term6 = new Zend_Search_Lucene_Index_Term('word6');
330 $subquery3 = new Zend_Search_Lucene_Search_Query_Term($term6);
332 $query->addSubquery($subquery1, true /* required */);
333 $query->addSubquery($subquery2, null /* optional */);
334 $query->addSubquery($subquery3, false /* prohibited */);
336 $hits = $index->find($query);
340 It's also possible to specify subqueries list within Boolean query constructor:
343 <programlisting language="php"><![CDATA[
345 $subqueries = array($subquery1, $subquery2, $subquery3);
346 $signs = array(true, null, false);
348 $query = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);
350 $hits = $index->find($query);
354 The <varname>$signs</varname> array contains information about the subquery type:
359 <constant>TRUE</constant> is used to define required subquery.
365 <constant>FALSE</constant> is used to define prohibited subquery.
371 <constant>NULL</constant> is used to define a subquery that is neither
372 required nor prohibited.
379 Each query which uses boolean operators can be rewritten using signs notation and
380 constructed using <acronym>API</acronym>. For example:
383 <programlisting language="querystring"><![CDATA[
384 word1 AND (word2 AND word3 AND NOT word4) OR word5
391 <programlisting language="querystring"><![CDATA[
392 (+(word1) +(+word2 +word3 -word4)) (word5)
396 <sect2 id="zend.search.lucene.queries.wildcard">
397 <title>Wildcard Query</title>
400 Wildcard queries can be used to search for documents containing strings matching
405 The '?' symbol is used as a single character wildcard.
409 The '*' symbol is used as a multiple character wildcard.
416 <programlisting language="querystring"><![CDATA[
423 Query construction by <acronym>API</acronym>:
426 <programlisting language="php"><![CDATA[
427 $pattern = new Zend_Search_Lucene_Index_Term('test*', 'field1');
428 $query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
429 $hits = $index->find($query);
433 The term field is optional. <classname>Zend_Search_Lucene</classname> searches through
434 all fields on each document if a field is not specified:
437 <programlisting language="php"><![CDATA[
438 $pattern = new Zend_Search_Lucene_Index_Term('test*');
439 $query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
440 $hits = $index->find($query);
444 <sect2 id="zend.search.lucene.queries.fuzzy">
445 <title>Fuzzy Query</title>
448 Fuzzy queries can be used to search for documents containing strings matching terms
449 similar to specified term.
456 <programlisting language="querystring"><![CDATA[
461 This query matches documents containing 'test' 'text' 'best' words and others.
467 Query construction by <acronym>API</acronym>:
470 <programlisting language="php"><![CDATA[
471 $term = new Zend_Search_Lucene_Index_Term('test', 'field1');
472 $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term);
473 $hits = $index->find($query);
477 Optional similarity can be specified after "~" sign.
484 <programlisting language="querystring"><![CDATA[
491 Query construction by <acronym>API</acronym>:
494 <programlisting language="php"><![CDATA[
495 $term = new Zend_Search_Lucene_Index_Term('test', 'field1');
496 $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term, 0.4);
497 $hits = $index->find($query);
501 The term field is optional. <classname>Zend_Search_Lucene</classname> searches through
502 all fields on each document if a field is not specified:
505 <programlisting language="php"><![CDATA[
506 $term = new Zend_Search_Lucene_Index_Term('test');
507 $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term);
508 $hits = $index->find($query);
512 <sect2 id="zend.search.lucene.queries.phrase-query">
513 <title>Phrase Query</title>
516 Phrase Queries can be used to search for a phrase within documents.
520 Phrase Queries are very flexible and allow the user or developer to search for exact
521 phrases as well as 'sloppy' phrases.
525 Phrases can also contain gaps or terms in the same places; they can be generated by
526 the analyzer for different purposes. For example, a term can be duplicated to increase
527 the term its weight, or several synonyms can be placed into a single position.
530 <programlisting language="php"><![CDATA[
531 $query1 = new Zend_Search_Lucene_Search_Query_Phrase();
533 // Add 'word1' at 0 relative position.
534 $query1->addTerm(new Zend_Search_Lucene_Index_Term('word1'));
536 // Add 'word2' at 1 relative position.
537 $query1->addTerm(new Zend_Search_Lucene_Index_Term('word2'));
539 // Add 'word3' at 3 relative position.
540 $query1->addTerm(new Zend_Search_Lucene_Index_Term('word3'), 3);
544 $query2 = new Zend_Search_Lucene_Search_Query_Phrase(
545 array('word1', 'word2', 'word3'), array(0,1,3));
549 // Query without a gap.
550 $query3 = new Zend_Search_Lucene_Search_Query_Phrase(
551 array('word1', 'word2', 'word3'));
555 $query4 = new Zend_Search_Lucene_Search_Query_Phrase(
556 array('word1', 'word2'), array(0,1), 'annotation');
560 A phrase query can be constructed in one step with a class constructor or step by step
561 with <methodname>Zend_Search_Lucene_Search_Query_Phrase::addTerm()</methodname> method
566 <classname>Zend_Search_Lucene_Search_Query_Phrase</classname> class constructor takes
567 three optional arguments:
570 <programlisting language="php"><![CDATA[
571 Zend_Search_Lucene_Search_Query_Phrase(
572 [array $terms[, array $offsets[, string $field]]]
577 The <varname>$terms</varname> parameter is an array of strings that contains a set of
578 phrase terms. If it's omitted or equal to <constant>NULL</constant>, then an empty query
583 The <varname>$offsets</varname> parameter is an array of integers that contains offsets
584 of terms in a phrase. If it's omitted or equal to <constant>NULL</constant>, then the
585 terms' positions are assumed to be sequential with no gaps.
589 The <varname>$field</varname> parameter is a string that indicates the document field
590 to search. If it's omitted or equal to <constant>NULL</constant>, then the default field
598 <programlisting language="php"><![CDATA[
600 new Zend_Search_Lucene_Search_Query_Phrase(array('zend', 'framework'));
604 will search for the phrase 'zend framework' in all fields.
607 <programlisting language="php"><![CDATA[
608 $query = new Zend_Search_Lucene_Search_Query_Phrase(
609 array('zend', 'download'), array(0, 2)
614 will search for the phrase 'zend ????? download' and match 'zend platform download',
615 'zend studio download', 'zend core download', 'zend framework download', and so on.
618 <programlisting language="php"><![CDATA[
619 $query = new Zend_Search_Lucene_Search_Query_Phrase(
620 array('zend', 'framework'), null, 'title'
625 will search for the phrase 'zend framework' in the 'title' field.
629 <methodname>Zend_Search_Lucene_Search_Query_Phrase::addTerm()</methodname> takes two
630 arguments, a required <classname>Zend_Search_Lucene_Index_Term</classname> object and an
634 <programlisting language="php"><![CDATA[
635 Zend_Search_Lucene_Search_Query_Phrase::addTerm(
636 Zend_Search_Lucene_Index_Term $term[, integer $position]
641 The <varname>$term</varname> parameter describes the next term in the phrase. It must
642 indicate the same field as previous terms, or an exception will be thrown.
646 The <varname>$position</varname> parameter indicates the term position in the phrase.
653 <programlisting language="php"><![CDATA[
654 $query = new Zend_Search_Lucene_Search_Query_Phrase();
655 $query->addTerm(new Zend_Search_Lucene_Index_Term('zend'));
656 $query->addTerm(new Zend_Search_Lucene_Index_Term('framework'));
660 will search for the phrase 'zend framework'.
663 <programlisting language="php"><![CDATA[
664 $query = new Zend_Search_Lucene_Search_Query_Phrase();
665 $query->addTerm(new Zend_Search_Lucene_Index_Term('zend'), 0);
666 $query->addTerm(new Zend_Search_Lucene_Index_Term('framework'), 2);
670 will search for the phrase 'zend ????? download' and match 'zend platform download',
671 'zend studio download', 'zend core download', 'zend framework download', and so on.
674 <programlisting language="php"><![CDATA[
675 $query = new Zend_Search_Lucene_Search_Query_Phrase();
676 $query->addTerm(new Zend_Search_Lucene_Index_Term('zend', 'title'));
677 $query->addTerm(new Zend_Search_Lucene_Index_Term('framework', 'title'));
681 will search for the phrase 'zend framework' in the 'title' field.
685 The slop factor sets the number of other words permitted between specified words in the
686 query phrase. If set to zero, then the corresponding query is an exact phrase search.
687 For larger values this works like the WITHIN or NEAR operators.
691 The slop factor is in fact an edit distance, where the edits correspond to moving terms
692 in the query phrase. For example, to switch the order of two words requires two moves
693 (the first move places the words atop one another), so to permit re-orderings of
694 phrases, the slop factor must be at least two.
698 More exact matches are scored higher than sloppier matches; thus, search results are
699 sorted by exactness. The slop is zero by default, requiring exact matches.
703 The slop factor can be assigned after query creation:
706 <programlisting language="php"><![CDATA[
707 // Query without a gap.
709 new Zend_Search_Lucene_Search_Query_Phrase(array('word1', 'word2'));
711 // Search for 'word1 word2', 'word1 ... word2'
713 $hits1 = $index->find($query);
715 // Search for 'word1 word2', 'word1 ... word2',
716 // 'word1 ... ... word2', 'word2 word1'
718 $hits2 = $index->find($query);
722 <sect2 id="zend.search.lucene.queries.range">
723 <title>Range Query</title>
726 <link linkend="zend.search.lucene.query-language.range">Range queries</link> are
727 intended for searching terms within specified interval.
734 <programlisting language="querystring"><![CDATA[
735 mod_date:[20020101 TO 20030101]
736 title:{Aida TO Carmen}
742 Query construction by <acronym>API</acronym>:
745 <programlisting language="php"><![CDATA[
746 $from = new Zend_Search_Lucene_Index_Term('20020101', 'mod_date');
747 $to = new Zend_Search_Lucene_Index_Term('20030101', 'mod_date');
748 $query = new Zend_Search_Lucene_Search_Query_Range(
749 $from, $to, true // inclusive
751 $hits = $index->find($query);
755 Term fields are optional. <classname>Zend_Search_Lucene</classname> searches through all
756 fields if the field is not specified:
759 <programlisting language="php"><![CDATA[
760 $from = new Zend_Search_Lucene_Index_Term('Aida');
761 $to = new Zend_Search_Lucene_Index_Term('Carmen');
762 $query = new Zend_Search_Lucene_Search_Query_Range(
763 $from, $to, false // non-inclusive
765 $hits = $index->find($query);
769 Either (but not both) of the boundary terms may be set to <constant>NULL</constant>.
770 <classname>Zend_Search_Lucene</classname> searches from the beginning or
771 up to the end of the dictionary for the specified field(s) in this case:
774 <programlisting language="php"><![CDATA[
775 // searches for ['20020101' TO ...]
776 $from = new Zend_Search_Lucene_Index_Term('20020101', 'mod_date');
777 $query = new Zend_Search_Lucene_Search_Query_Range(
778 $from, null, true // inclusive
780 $hits = $index->find($query);