Zend_Search_Lucene-Queries.xml 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.search.lucene.query-api">
  4. <title>Query Construction API</title>
  5. <para>
  6. In addition to parsing a string query automatically it's also possible to construct them
  7. with the query <acronym>API</acronym>.
  8. </para>
  9. <para>
  10. User queries can be combined with queries created through the query <acronym>API</acronym>.
  11. Simply use the query parser to construct a query from a string:
  12. </para>
  13. <programlisting language="php"><![CDATA[
  14. $query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
  15. ]]></programlisting>
  16. <sect2 id="zend.search.lucene.queries.exceptions">
  17. <title>Query Parser Exceptions</title>
  18. <para>
  19. The query parser may generate two types of exceptions:
  20. <itemizedlist>
  21. <listitem>
  22. <para>
  23. <classname>Zend_Search_Lucene_Exception</classname> is thrown if something
  24. goes wrong in the query parser itself.
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. <classname>Zend_Search_Lucene_Search_QueryParserException</classname> is
  30. thrown when there is an error in the query syntax.
  31. </para>
  32. </listitem>
  33. </itemizedlist>
  34. It's a good idea to catch
  35. <classname>Zend_Search_Lucene_Search_QueryParserException</classname>s and handle them
  36. appropriately:
  37. </para>
  38. <programlisting language="php"><![CDATA[
  39. try {
  40. $query = Zend_Search_Lucene_Search_QueryParser::parse($queryString);
  41. } catch (Zend_Search_Lucene_Search_QueryParserException $e) {
  42. echo "Query syntax error: " . $e->getMessage() . "\n";
  43. }
  44. ]]></programlisting>
  45. <para>
  46. The same technique should be used for the find() method of a
  47. <classname>Zend_Search_Lucene</classname> object.
  48. </para>
  49. <para>
  50. Starting in 1.5, query parsing exceptions are suppressed by default. If query doesn't
  51. conform query language, then it's tokenized using current default analyzer and all
  52. tokenized terms are used for searching. Use
  53. <methodname>Zend_Search_Lucene_Search_QueryParser::dontSuppressQueryParsingExceptions()</methodname>
  54. method to turn exceptions on.
  55. <methodname>Zend_Search_Lucene_Search_QueryParser::suppressQueryParsingExceptions()</methodname>
  56. and
  57. <methodname>Zend_Search_Lucene_Search_QueryParser::queryParsingExceptionsSuppressed()</methodname>
  58. methods are also intended to manage exceptions handling behavior.
  59. </para>
  60. </sect2>
  61. <sect2 id="zend.search.lucene.queries.term-query">
  62. <title>Term Query</title>
  63. <para>
  64. Term queries can be used for searching with a single term.
  65. </para>
  66. <para>
  67. Query string:
  68. </para>
  69. <programlisting language="querystring"><![CDATA[
  70. word1
  71. ]]></programlisting>
  72. <para>or</para>
  73. <para>
  74. Query construction by <acronym>API</acronym>:
  75. </para>
  76. <programlisting language="php"><![CDATA[
  77. $term = new Zend_Search_Lucene_Index_Term('word1', 'field1');
  78. $query = new Zend_Search_Lucene_Search_Query_Term($term);
  79. $hits = $index->find($query);
  80. ]]></programlisting>
  81. <para>
  82. The term field is optional. <classname>Zend_Search_Lucene</classname> searches through
  83. all indexed fields in each document if the field is not specified:
  84. </para>
  85. <programlisting language="php"><![CDATA[
  86. // Search for 'word1' in all indexed fields
  87. $term = new Zend_Search_Lucene_Index_Term('word1');
  88. $query = new Zend_Search_Lucene_Search_Query_Term($term);
  89. $hits = $index->find($query);
  90. ]]></programlisting>
  91. </sect2>
  92. <sect2 id="zend.search.lucene.queries.multiterm-query">
  93. <title>Multi-Term Query</title>
  94. <para>
  95. Multi-term queries can be used for searching with a set of terms.
  96. </para>
  97. <para>
  98. Each term in a set can be defined as <emphasis>required</emphasis>,
  99. <emphasis>prohibited</emphasis>, or <emphasis>neither</emphasis>.
  100. <itemizedlist>
  101. <listitem>
  102. <para>
  103. <emphasis>required</emphasis> means that documents not matching this term
  104. will not match the query;
  105. </para>
  106. </listitem>
  107. <listitem>
  108. <para>
  109. <emphasis>prohibited</emphasis> means that documents matching this term will
  110. not match the query;
  111. </para>
  112. </listitem>
  113. <listitem>
  114. <para>
  115. <emphasis>neither</emphasis>, in which case matched documents are neither
  116. prohibited from, nor required to, match the term. A document must match at
  117. least 1 term, however, to match the query.
  118. </para>
  119. </listitem>
  120. </itemizedlist>
  121. </para>
  122. <para>
  123. If optional terms are added to a query with required terms, both queries will have the
  124. same result set but the optional terms may affect the score of the matched documents.
  125. </para>
  126. <para>
  127. Both search methods can be used for multi-term queries.
  128. </para>
  129. <para>
  130. Query string:
  131. </para>
  132. <programlisting language="querystring"><![CDATA[
  133. +word1 author:word2 -word3
  134. ]]></programlisting>
  135. <itemizedlist>
  136. <listitem><para>'+' is used to define a required term.</para></listitem>
  137. <listitem><para>'-' is used to define a prohibited term.</para></listitem>
  138. <listitem>
  139. <para>
  140. 'field:' prefix is used to indicate a document field for a search.
  141. If it's omitted, then all fields are searched.
  142. </para>
  143. </listitem>
  144. </itemizedlist>
  145. <para>or</para>
  146. <para>
  147. Query construction by <acronym>API</acronym>:
  148. </para>
  149. <programlisting language="php"><![CDATA[
  150. $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
  151. $query->addTerm(new Zend_Search_Lucene_Index_Term('word1'), true);
  152. $query->addTerm(new Zend_Search_Lucene_Index_Term('word2', 'author'),
  153. null);
  154. $query->addTerm(new Zend_Search_Lucene_Index_Term('word3'), false);
  155. $hits = $index->find($query);
  156. ]]></programlisting>
  157. <para>
  158. It's also possible to specify terms list within MultiTerm query constructor:
  159. </para>
  160. <programlisting language="php"><![CDATA[
  161. $terms = array(new Zend_Search_Lucene_Index_Term('word1'),
  162. new Zend_Search_Lucene_Index_Term('word2', 'author'),
  163. new Zend_Search_Lucene_Index_Term('word3'));
  164. $signs = array(true, null, false);
  165. $query = new Zend_Search_Lucene_Search_Query_MultiTerm($terms, $signs);
  166. $hits = $index->find($query);
  167. ]]></programlisting>
  168. <para>
  169. The <varname>$signs</varname> array contains information about the term type:
  170. <itemizedlist>
  171. <listitem>
  172. <para>
  173. <constant>TRUE</constant> is used to define required term.
  174. </para>
  175. </listitem>
  176. <listitem>
  177. <para>
  178. <constant>FALSE</constant> is used to define prohibited term.
  179. </para>
  180. </listitem>
  181. <listitem>
  182. <para>
  183. <constant>NULL</constant> is used to define a term that is neither required
  184. nor prohibited.
  185. </para>
  186. </listitem>
  187. </itemizedlist>
  188. </para>
  189. </sect2>
  190. <sect2 id="zend.search.lucene.queries.boolean-query">
  191. <title>Boolean Query</title>
  192. <para>
  193. Boolean queries allow to construct query using other queries and boolean operators.
  194. </para>
  195. <para>
  196. Each subquery in a set can be defined as <emphasis>required</emphasis>,
  197. <emphasis>prohibited</emphasis>, or <emphasis>optional</emphasis>.
  198. <itemizedlist>
  199. <listitem>
  200. <para>
  201. <emphasis>required</emphasis> means that documents not matching this
  202. subquery will not match the query;
  203. </para>
  204. </listitem>
  205. <listitem>
  206. <para>
  207. <emphasis>prohibited</emphasis> means that documents matching this subquery
  208. will not match the query;
  209. </para>
  210. </listitem>
  211. <listitem>
  212. <para>
  213. <emphasis>optional</emphasis>, in which case matched documents are neither
  214. prohibited from, nor required to, match the subquery. A document must match
  215. at least 1 subquery, however, to match the query.
  216. </para>
  217. </listitem>
  218. </itemizedlist>
  219. </para>
  220. <para>
  221. If optional subqueries are added to a query with required subqueries, both queries will
  222. have the same result set but the optional subqueries may affect the score of the matched
  223. documents.
  224. </para>
  225. <para>
  226. Both search methods can be used for boolean queries.
  227. </para>
  228. <para>
  229. Query string:
  230. </para>
  231. <programlisting language="querystring"><![CDATA[
  232. +(word1 word2 word3) (author:word4 author:word5) -(word6)
  233. ]]></programlisting>
  234. <itemizedlist>
  235. <listitem>
  236. <para>
  237. '+' is used to define a required subquery.
  238. </para>
  239. </listitem>
  240. <listitem>
  241. <para>
  242. '-' is used to define a prohibited subquery.
  243. </para>
  244. </listitem>
  245. <listitem>
  246. <para>
  247. 'field:' prefix is used to indicate a document field for a search.
  248. If it's omitted, then all fields are searched.
  249. </para>
  250. </listitem>
  251. </itemizedlist>
  252. <para>or</para>
  253. <para>
  254. Query construction by <acronym>API</acronym>:
  255. </para>
  256. <programlisting language="php"><![CDATA[
  257. $query = new Zend_Search_Lucene_Search_Query_Boolean();
  258. $subquery1 = new Zend_Search_Lucene_Search_Query_MultiTerm();
  259. $subquery1->addTerm(new Zend_Search_Lucene_Index_Term('word1'));
  260. $subquery1->addTerm(new Zend_Search_Lucene_Index_Term('word2'));
  261. $subquery1->addTerm(new Zend_Search_Lucene_Index_Term('word3'));
  262. $subquery2 = new Zend_Search_Lucene_Search_Query_MultiTerm();
  263. $subquery2->addTerm(new Zend_Search_Lucene_Index_Term('word4', 'author'));
  264. $subquery2->addTerm(new Zend_Search_Lucene_Index_Term('word5', 'author'));
  265. $term6 = new Zend_Search_Lucene_Index_Term('word6');
  266. $subquery3 = new Zend_Search_Lucene_Search_Query_Term($term6);
  267. $query->addSubquery($subquery1, true /* required */);
  268. $query->addSubquery($subquery2, null /* optional */);
  269. $query->addSubquery($subquery3, false /* prohibited */);
  270. $hits = $index->find($query);
  271. ]]></programlisting>
  272. <para>
  273. It's also possible to specify subqueries list within Boolean query constructor:
  274. </para>
  275. <programlisting language="php"><![CDATA[
  276. ...
  277. $subqueries = array($subquery1, $subquery2, $subquery3);
  278. $signs = array(true, null, false);
  279. $query = new Zend_Search_Lucene_Search_Query_Boolean($subqueries, $signs);
  280. $hits = $index->find($query);
  281. ]]></programlisting>
  282. <para>
  283. The <varname>$signs</varname> array contains information about the subquery type:
  284. <itemizedlist>
  285. <listitem>
  286. <para>
  287. <constant>TRUE</constant> is used to define required subquery.
  288. </para>
  289. </listitem>
  290. <listitem>
  291. <para>
  292. <constant>FALSE</constant> is used to define prohibited subquery.
  293. </para>
  294. </listitem>
  295. <listitem>
  296. <para>
  297. <constant>NULL</constant> is used to define a subquery that is neither
  298. required nor prohibited.
  299. </para>
  300. </listitem>
  301. </itemizedlist>
  302. </para>
  303. <para>
  304. Each query which uses boolean operators can be rewritten using signs notation and
  305. constructed using <acronym>API</acronym>. For example:
  306. </para>
  307. <programlisting language="querystring"><![CDATA[
  308. word1 AND (word2 AND word3 AND NOT word4) OR word5
  309. ]]></programlisting>
  310. <para>
  311. is equivalent to
  312. </para>
  313. <programlisting language="querystring"><![CDATA[
  314. (+(word1) +(+word2 +word3 -word4)) (word5)
  315. ]]></programlisting>
  316. </sect2>
  317. <sect2 id="zend.search.lucene.queries.wildcard">
  318. <title>Wildcard Query</title>
  319. <para>
  320. Wildcard queries can be used to search for documents containing strings matching
  321. specified patterns.
  322. </para>
  323. <para>
  324. The '?' symbol is used as a single character wildcard.
  325. </para>
  326. <para>
  327. The '*' symbol is used as a multiple character wildcard.
  328. </para>
  329. <para>
  330. Query string:
  331. </para>
  332. <programlisting language="querystring"><![CDATA[
  333. field1:test*
  334. ]]></programlisting>
  335. <para>or</para>
  336. <para>
  337. Query construction by <acronym>API</acronym>:
  338. </para>
  339. <programlisting language="php"><![CDATA[
  340. $pattern = new Zend_Search_Lucene_Index_Term('test*', 'field1');
  341. $query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
  342. $hits = $index->find($query);
  343. ]]></programlisting>
  344. <para>
  345. The term field is optional. <classname>Zend_Search_Lucene</classname> searches through
  346. all fields on each document if a field is not specified:
  347. </para>
  348. <programlisting language="php"><![CDATA[
  349. $pattern = new Zend_Search_Lucene_Index_Term('test*');
  350. $query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
  351. $hits = $index->find($query);
  352. ]]></programlisting>
  353. </sect2>
  354. <sect2 id="zend.search.lucene.queries.fuzzy">
  355. <title>Fuzzy Query</title>
  356. <para>
  357. Fuzzy queries can be used to search for documents containing strings matching terms
  358. similar to specified term.
  359. </para>
  360. <para>
  361. Query string:
  362. </para>
  363. <programlisting language="querystring"><![CDATA[
  364. field1:test~
  365. ]]></programlisting>
  366. <para>
  367. This query matches documents containing 'test' 'text' 'best' words and others.
  368. </para>
  369. <para>or</para>
  370. <para>
  371. Query construction by <acronym>API</acronym>:
  372. </para>
  373. <programlisting language="php"><![CDATA[
  374. $term = new Zend_Search_Lucene_Index_Term('test', 'field1');
  375. $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term);
  376. $hits = $index->find($query);
  377. ]]></programlisting>
  378. <para>
  379. Optional similarity can be specified after "~" sign.
  380. </para>
  381. <para>
  382. Query string:
  383. </para>
  384. <programlisting language="querystring"><![CDATA[
  385. field1:test~0.4
  386. ]]></programlisting>
  387. <para>or</para>
  388. <para>
  389. Query construction by <acronym>API</acronym>:
  390. </para>
  391. <programlisting language="php"><![CDATA[
  392. $term = new Zend_Search_Lucene_Index_Term('test', 'field1');
  393. $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term, 0.4);
  394. $hits = $index->find($query);
  395. ]]></programlisting>
  396. <para>
  397. The term field is optional. <classname>Zend_Search_Lucene</classname> searches through
  398. all fields on each document if a field is not specified:
  399. </para>
  400. <programlisting language="php"><![CDATA[
  401. $term = new Zend_Search_Lucene_Index_Term('test');
  402. $query = new Zend_Search_Lucene_Search_Query_Fuzzy($term);
  403. $hits = $index->find($query);
  404. ]]></programlisting>
  405. </sect2>
  406. <sect2 id="zend.search.lucene.queries.phrase-query">
  407. <title>Phrase Query</title>
  408. <para>
  409. Phrase Queries can be used to search for a phrase within documents.
  410. </para>
  411. <para>
  412. Phrase Queries are very flexible and allow the user or developer to search for exact
  413. phrases as well as 'sloppy' phrases.
  414. </para>
  415. <para>
  416. Phrases can also contain gaps or terms in the same places; they can be generated by
  417. the analyzer for different purposes. For example, a term can be duplicated to increase
  418. the term its weight, or several synonyms can be placed into a single position.
  419. </para>
  420. <programlisting language="php"><![CDATA[
  421. $query1 = new Zend_Search_Lucene_Search_Query_Phrase();
  422. // Add 'word1' at 0 relative position.
  423. $query1->addTerm(new Zend_Search_Lucene_Index_Term('word1'));
  424. // Add 'word2' at 1 relative position.
  425. $query1->addTerm(new Zend_Search_Lucene_Index_Term('word2'));
  426. // Add 'word3' at 3 relative position.
  427. $query1->addTerm(new Zend_Search_Lucene_Index_Term('word3'), 3);
  428. ...
  429. $query2 = new Zend_Search_Lucene_Search_Query_Phrase(
  430. array('word1', 'word2', 'word3'), array(0,1,3));
  431. ...
  432. // Query without a gap.
  433. $query3 = new Zend_Search_Lucene_Search_Query_Phrase(
  434. array('word1', 'word2', 'word3'));
  435. ...
  436. $query4 = new Zend_Search_Lucene_Search_Query_Phrase(
  437. array('word1', 'word2'), array(0,1), 'annotation');
  438. ]]></programlisting>
  439. <para>
  440. A phrase query can be constructed in one step with a class constructor or step by step
  441. with <methodname>Zend_Search_Lucene_Search_Query_Phrase::addTerm()</methodname> method
  442. calls.
  443. </para>
  444. <para>
  445. <classname>Zend_Search_Lucene_Search_Query_Phrase</classname> class constructor takes
  446. three optional arguments:
  447. </para>
  448. <programlisting language="php"><![CDATA[
  449. Zend_Search_Lucene_Search_Query_Phrase(
  450. [array $terms[, array $offsets[, string $field]]]
  451. );
  452. ]]></programlisting>
  453. <para>
  454. The <varname>$terms</varname> parameter is an array of strings that contains a set of
  455. phrase terms. If it's omitted or equal to <constant>NULL</constant>, then an empty query
  456. is constructed.
  457. </para>
  458. <para>
  459. The <varname>$offsets</varname> parameter is an array of integers that contains offsets
  460. of terms in a phrase. If it's omitted or equal to <constant>NULL</constant>, then the
  461. terms' positions are assumed to be sequential with no gaps.
  462. </para>
  463. <para>
  464. The <varname>$field</varname> parameter is a string that indicates the document field
  465. to search. If it's omitted or equal to <constant>NULL</constant>, then the default field
  466. is searched.
  467. </para>
  468. <para>
  469. Thus:
  470. </para>
  471. <programlisting language="php"><![CDATA[
  472. $query =
  473. new Zend_Search_Lucene_Search_Query_Phrase(array('zend', 'framework'));
  474. ]]></programlisting>
  475. <para>
  476. will search for the phrase 'zend framework' in all fields.
  477. </para>
  478. <programlisting language="php"><![CDATA[
  479. $query = new Zend_Search_Lucene_Search_Query_Phrase(
  480. array('zend', 'download'), array(0, 2)
  481. );
  482. ]]></programlisting>
  483. <para>
  484. will search for the phrase 'zend ????? download' and match 'zend platform download',
  485. 'zend studio download', 'zend core download', 'zend framework download', and so on.
  486. </para>
  487. <programlisting language="php"><![CDATA[
  488. $query = new Zend_Search_Lucene_Search_Query_Phrase(
  489. array('zend', 'framework'), null, 'title'
  490. );
  491. ]]></programlisting>
  492. <para>
  493. will search for the phrase 'zend framework' in the 'title' field.
  494. </para>
  495. <para>
  496. <methodname>Zend_Search_Lucene_Search_Query_Phrase::addTerm()</methodname> takes two
  497. arguments, a required <classname>Zend_Search_Lucene_Index_Term</classname> object and an
  498. optional position:
  499. </para>
  500. <programlisting language="php"><![CDATA[
  501. Zend_Search_Lucene_Search_Query_Phrase::addTerm(
  502. Zend_Search_Lucene_Index_Term $term[, integer $position]
  503. );
  504. ]]></programlisting>
  505. <para>
  506. The <varname>$term</varname> parameter describes the next term in the phrase. It must
  507. indicate the same field as previous terms, or an exception will be thrown.
  508. </para>
  509. <para>
  510. The <varname>$position</varname> parameter indicates the term position in the phrase.
  511. </para>
  512. <para>
  513. Thus:
  514. </para>
  515. <programlisting language="php"><![CDATA[
  516. $query = new Zend_Search_Lucene_Search_Query_Phrase();
  517. $query->addTerm(new Zend_Search_Lucene_Index_Term('zend'));
  518. $query->addTerm(new Zend_Search_Lucene_Index_Term('framework'));
  519. ]]></programlisting>
  520. <para>
  521. will search for the phrase 'zend framework'.
  522. </para>
  523. <programlisting language="php"><![CDATA[
  524. $query = new Zend_Search_Lucene_Search_Query_Phrase();
  525. $query->addTerm(new Zend_Search_Lucene_Index_Term('zend'), 0);
  526. $query->addTerm(new Zend_Search_Lucene_Index_Term('framework'), 2);
  527. ]]></programlisting>
  528. <para>
  529. will search for the phrase 'zend ????? download' and match 'zend platform download',
  530. 'zend studio download', 'zend core download', 'zend framework download', and so on.
  531. </para>
  532. <programlisting language="php"><![CDATA[
  533. $query = new Zend_Search_Lucene_Search_Query_Phrase();
  534. $query->addTerm(new Zend_Search_Lucene_Index_Term('zend', 'title'));
  535. $query->addTerm(new Zend_Search_Lucene_Index_Term('framework', 'title'));
  536. ]]></programlisting>
  537. <para>
  538. will search for the phrase 'zend framework' in the 'title' field.
  539. </para>
  540. <para>
  541. The slop factor sets the number of other words permitted between specified words in the
  542. query phrase. If set to zero, then the corresponding query is an exact phrase search.
  543. For larger values this works like the WITHIN or NEAR operators.
  544. </para>
  545. <para>
  546. The slop factor is in fact an edit distance, where the edits correspond to moving terms
  547. in the query phrase. For example, to switch the order of two words requires two moves
  548. (the first move places the words atop one another), so to permit re-orderings of
  549. phrases, the slop factor must be at least two.
  550. </para>
  551. <para>
  552. More exact matches are scored higher than sloppier matches; thus, search results are
  553. sorted by exactness. The slop is zero by default, requiring exact matches.
  554. </para>
  555. <para>
  556. The slop factor can be assigned after query creation:
  557. </para>
  558. <programlisting language="php"><![CDATA[
  559. // Query without a gap.
  560. $query =
  561. new Zend_Search_Lucene_Search_Query_Phrase(array('word1', 'word2'));
  562. // Search for 'word1 word2', 'word1 ... word2'
  563. $query->setSlop(1);
  564. $hits1 = $index->find($query);
  565. // Search for 'word1 word2', 'word1 ... word2',
  566. // 'word1 ... ... word2', 'word2 word1'
  567. $query->setSlop(2);
  568. $hits2 = $index->find($query);
  569. ]]></programlisting>
  570. </sect2>
  571. <sect2 id="zend.search.lucene.queries.range">
  572. <title>Range Query</title>
  573. <para>
  574. <link linkend="zend.search.lucene.query-language.range">Range queries</link> are
  575. intended for searching terms within specified interval.
  576. </para>
  577. <para>
  578. Query string:
  579. </para>
  580. <programlisting language="querystring"><![CDATA[
  581. mod_date:[20020101 TO 20030101]
  582. title:{Aida TO Carmen}
  583. ]]></programlisting>
  584. <para>or</para>
  585. <para>
  586. Query construction by <acronym>API</acronym>:
  587. </para>
  588. <programlisting language="php"><![CDATA[
  589. $from = new Zend_Search_Lucene_Index_Term('20020101', 'mod_date');
  590. $to = new Zend_Search_Lucene_Index_Term('20030101', 'mod_date');
  591. $query = new Zend_Search_Lucene_Search_Query_Range(
  592. $from, $to, true // inclusive
  593. );
  594. $hits = $index->find($query);
  595. ]]></programlisting>
  596. <para>
  597. Term fields are optional. <classname>Zend_Search_Lucene</classname> searches through all
  598. fields if the field is not specified:
  599. </para>
  600. <programlisting language="php"><![CDATA[
  601. $from = new Zend_Search_Lucene_Index_Term('Aida');
  602. $to = new Zend_Search_Lucene_Index_Term('Carmen');
  603. $query = new Zend_Search_Lucene_Search_Query_Range(
  604. $from, $to, false // non-inclusive
  605. );
  606. $hits = $index->find($query);
  607. ]]></programlisting>
  608. <para>
  609. Either (but not both) of the boundary terms may be set to <constant>NULL</constant>.
  610. <classname>Zend_Search_Lucene</classname> searches from the beginning or
  611. up to the end of the dictionary for the specified field(s) in this case:
  612. </para>
  613. <programlisting language="php"><![CDATA[
  614. // searches for ['20020101' TO ...]
  615. $from = new Zend_Search_Lucene_Index_Term('20020101', 'mod_date');
  616. $query = new Zend_Search_Lucene_Search_Query_Range(
  617. $from, null, true // inclusive
  618. );
  619. $hits = $index->find($query);
  620. ]]></programlisting>
  621. </sect2>
  622. </sect1>
  623. <!--
  624. vim:se ts=4 sw=4 et:
  625. -->