Zend_Search_Lucene-Queries.xml 25 KB

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