Zend_Search_Lucene-QueryLanguage.xml 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.search.lucene.query-language">
  4. <title>Query Language</title>
  5. <para>
  6. Java Lucene and <classname>Zend_Search_Lucene</classname> provide quite powerful query
  7. languages.
  8. </para>
  9. <para>
  10. These languages are mostly the same with some minor differences, which are mentioned below.
  11. </para>
  12. <para>
  13. Full Java Lucene query language syntax documentation can be found
  14. <ulink url="http://lucene.apache.org/java/2_3_0/queryparsersyntax.html">here</ulink>.
  15. </para>
  16. <sect2 id="zend.search.lucene.query-language.terms">
  17. <title>Terms</title>
  18. <para>
  19. A query is broken up into terms and operators. There are three types of terms: Single
  20. Terms, Phrases, and Subqueries.
  21. </para>
  22. <para>
  23. A Single Term is a single word such as "test" or "hello".
  24. </para>
  25. <para>
  26. A Phrase is a group of words surrounded by double quotes such as "hello dolly".
  27. </para>
  28. <para>
  29. A Subquery is a query surrounded by parentheses such as "(hello dolly)".
  30. </para>
  31. <para>
  32. Multiple terms can be combined together with boolean operators to form complex queries
  33. (see below).
  34. </para>
  35. </sect2>
  36. <sect2 id="zend.search.lucene.query-language.fields">
  37. <title>Fields</title>
  38. <para>
  39. Lucene supports fields of data. When performing a search you can either specify a field,
  40. or use the default field. The field names depend on indexed data and default field is
  41. defined by current settings.
  42. </para>
  43. <para>
  44. The first and most significant difference from Java Lucene is that terms are searched
  45. through <emphasis>all fields</emphasis> by default.
  46. </para>
  47. <para>
  48. There are two static methods in the <classname>Zend_Search_Lucene</classname> class
  49. which allow the developer to configure these settings:
  50. </para>
  51. <programlisting language="php"><![CDATA[
  52. $defaultSearchField = Zend_Search_Lucene::getDefaultSearchField();
  53. ...
  54. Zend_Search_Lucene::setDefaultSearchField('contents');
  55. ]]></programlisting>
  56. <para>
  57. The <constant>NULL</constant> value indicated that the search is performed across all
  58. fields. It's the default setting.
  59. </para>
  60. <para>
  61. You can search specific fields by typing the field name followed by a colon ":" followed
  62. by the term you are looking for.
  63. </para>
  64. <para>
  65. As an example, let's assume a Lucene index contains two fields- title and text- with
  66. text as the default field. If you want to find the document entitled "The Right Way"
  67. which contains the text "don't go this way", you can enter:
  68. </para>
  69. <programlisting language="querystring"><![CDATA[
  70. title:"The Right Way" AND text:go
  71. ]]></programlisting>
  72. <para>
  73. or
  74. </para>
  75. <programlisting language="querystring"><![CDATA[
  76. title:"Do it right" AND go
  77. ]]></programlisting>
  78. <para>
  79. Because "text" is the default field, the field indicator is not required.
  80. </para>
  81. <para>
  82. Note: The field is only valid for the term, phrase or subquery that it directly
  83. precedes, so the query
  84. </para>
  85. <programlisting language="querystring"><![CDATA[
  86. title:Do it right
  87. ]]></programlisting>
  88. <para>
  89. Will only find "Do" in the title field. It will find "it" and "right" in the default
  90. field (if the default field is set) or in all indexed fields (if the default field is
  91. set to <constant>NULL</constant>).
  92. </para>
  93. </sect2>
  94. <sect2 id="zend.search.lucene.query-language.wildcard">
  95. <title>Wildcards</title>
  96. <para>
  97. Lucene supports single and multiple character wildcard searches within single terms (but
  98. not within phrase queries).
  99. </para>
  100. <para>
  101. To perform a single character wildcard search use the "?" symbol.
  102. </para>
  103. <para>
  104. To perform a multiple character wildcard search use the "*" symbol.
  105. </para>
  106. <para>
  107. The single character wildcard search looks for string that match the term with the "?"
  108. replaced by any single character. For example, to search for "text" or "test" you can
  109. use the search:
  110. </para>
  111. <programlisting language="querystring"><![CDATA[
  112. te?t
  113. ]]></programlisting>
  114. <para>
  115. Multiple character wildcard searches look for 0 or more characters when matching strings
  116. against terms. For example, to search for test, tests or tester, you can use the search:
  117. </para>
  118. <programlisting language="querystring"><![CDATA[
  119. test*
  120. ]]></programlisting>
  121. <para>
  122. You can use "?", "*" or both at any place of the term:
  123. </para>
  124. <programlisting language="querystring"><![CDATA[
  125. *wr?t*
  126. ]]></programlisting>
  127. <para>
  128. It searches for "write", "wrote", "written", "rewrite", "rewrote" and so on.
  129. </para>
  130. <para>
  131. Starting from ZF 1.7.7 wildcard patterns need some non-wildcard prefix. Default prefix
  132. length is 3 (like in Java Lucene). So "*", "te?t", "*wr?t*" terms will cause an
  133. exception
  134. <footnote>
  135. <para>
  136. Please note, that it's not a
  137. <code>Zend_Search_Lucene_Search_QueryParserException</code>, but a
  138. <code>Zend_Search_Lucene_Exception</code>. It's thrown during query rewrite
  139. (execution) operation.
  140. </para>
  141. </footnote>.
  142. </para>
  143. <para>
  144. It can be altered using
  145. <code>Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength()</code> and
  146. <code>Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength()</code> methods.
  147. </para>
  148. </sect2>
  149. <sect2 id="zend.search.lucene.query-language.modifiers">
  150. <title>Term Modifiers</title>
  151. <para>
  152. Lucene supports modifying query terms to provide a wide range of searching options.
  153. </para>
  154. <para>
  155. "~" modifier can be used to specify proximity search for phrases or fuzzy search for
  156. individual terms.
  157. </para>
  158. </sect2>
  159. <sect2 id="zend.search.lucene.query-language.range">
  160. <title>Range Searches</title>
  161. <para>
  162. Range queries allow the developer or user to match documents whose field(s) values are
  163. between the lower and upper bound specified by the range query. Range Queries can be
  164. inclusive or exclusive of the upper and lower bounds. Sorting is performed
  165. lexicographically.
  166. </para>
  167. <programlisting language="querystring"><![CDATA[
  168. mod_date:[20020101 TO 20030101]
  169. ]]></programlisting>
  170. <para>
  171. This will find documents whose mod_date fields have values between 20020101 and
  172. 20030101, inclusive. Note that Range Queries are not reserved for date fields. You could
  173. also use range queries with non-date fields:
  174. </para>
  175. <programlisting language="querystring"><![CDATA[
  176. title:{Aida TO Carmen}
  177. ]]></programlisting>
  178. <para>
  179. This will find all documents whose titles would be sorted between Aida and Carmen, but
  180. not including Aida and Carmen.
  181. </para>
  182. <para>
  183. Inclusive range queries are denoted by square brackets. Exclusive range queries are
  184. denoted by curly brackets.
  185. </para>
  186. <para>
  187. If field is not specified then <classname>Zend_Search_Lucene</classname> searches for
  188. specified interval through all fields by default.
  189. </para>
  190. <programlisting language="querystring"><![CDATA[
  191. {Aida TO Carmen}
  192. ]]></programlisting>
  193. </sect2>
  194. <sect2 id="zend.search.lucene.query-language.fuzzy">
  195. <title>Fuzzy Searches</title>
  196. <para>
  197. <classname>Zend_Search_Lucene</classname> as well as Java Lucene supports fuzzy searches
  198. based on the Levenshtein Distance, or Edit Distance algorithm. To do a fuzzy search use
  199. the tilde, "~", symbol at the end of a Single word Term. For example to search for a
  200. term similar in spelling to "roam" use the fuzzy search:
  201. </para>
  202. <programlisting language="querystring"><![CDATA[
  203. roam~
  204. ]]></programlisting>
  205. <para>
  206. This search will find terms like foam and roams. Additional (optional) parameter can
  207. specify the required similarity. The value is between 0 and 1, with a value closer to 1
  208. only terms with a higher similarity will be matched. For example:
  209. </para>
  210. <programlisting language="querystring"><![CDATA[
  211. roam~0.8
  212. ]]></programlisting>
  213. <para>
  214. The default that is used if the parameter is not given is 0.5.
  215. </para>
  216. </sect2>
  217. <sect2 id="zend.search.lucene.query-language.matched-terms-limitations">
  218. <title>Matched terms limitation</title>
  219. <para>
  220. Wildcard, range and fuzzy search queries may match too many terms. It may cause
  221. incredible search performance downgrade.
  222. </para>
  223. <para>
  224. So <classname>Zend_Search_Lucene</classname> sets a limit of matching terms per query
  225. (subquery). This limit can be retrieved and set using
  226. <code>Zend_Search_Lucene::getTermsPerQueryLimit()</code>/<code>Zend_Search_Lucene::setTermsPerQueryLimit($limit)</code>
  227. methods.
  228. </para>
  229. <para>
  230. Default matched terms per query limit is 1024.
  231. </para>
  232. </sect2>
  233. <sect2 id="zend.search.lucene.query-language.proximity-search">
  234. <title>Proximity Searches</title>
  235. <para>
  236. Lucene supports finding words from a phrase that are within a specified word distance in
  237. a string. To do a proximity search use the tilde, "~", symbol at the end of the phrase.
  238. For example to search for a "Zend" and "Framework" within 10 words of each other in a
  239. document use the search:
  240. </para>
  241. <programlisting language="querystring"><![CDATA[
  242. "Zend Framework"~10
  243. ]]></programlisting>
  244. </sect2>
  245. <sect2 id="zend.search.lucene.query-language.boosting">
  246. <title>Boosting a Term</title>
  247. <para>
  248. Java Lucene and <classname>Zend_Search_Lucene</classname> provide the relevance level of
  249. matching documents based on the terms found. To boost the relevance of a term use the
  250. caret, "^", symbol with a boost factor (a number) at the end of the term you are
  251. searching. The higher the boost factor, the more relevant the term will be.
  252. </para>
  253. <para>
  254. Boosting allows you to control the relevance of a document by boosting individual terms.
  255. For example, if you are searching for
  256. </para>
  257. <programlisting language="querystring"><![CDATA[
  258. PHP framework
  259. ]]></programlisting>
  260. <para>
  261. and you want the term "PHP" to be more relevant boost it using the ^ symbol along with
  262. the boost factor next to the term. You would type:
  263. </para>
  264. <programlisting language="querystring"><![CDATA[
  265. PHP^4 framework
  266. ]]></programlisting>
  267. <para>
  268. This will make documents with the term <acronym>PHP</acronym> appear more relevant. You
  269. can also boost phrase terms and subqueries as in the example:
  270. </para>
  271. <programlisting language="querystring"><![CDATA[
  272. "PHP framework"^4 "Zend Framework"
  273. ]]></programlisting>
  274. <para>
  275. By default, the boost factor is 1. Although the boost factor must be positive,
  276. it may be less than 1 (e.g. 0.2).
  277. </para>
  278. </sect2>
  279. <sect2 id="zend.search.lucene.query-language.boolean">
  280. <title>Boolean Operators</title>
  281. <para>
  282. Boolean operators allow terms to be combined through logic operators.
  283. Lucene supports AND, "+", OR, NOT and "-" as Boolean operators.
  284. Java Lucene requires boolean operators to be ALL CAPS.
  285. <classname>Zend_Search_Lucene</classname> does not.
  286. </para>
  287. <para>
  288. AND, OR, and NOT operators and "+", "-" defines two different styles to construct
  289. boolean queries. Unlike Java Lucene, <classname>Zend_Search_Lucene</classname> doesn't
  290. allow these two styles to be mixed.
  291. </para>
  292. <para>
  293. If the AND/OR/NOT style is used, then an AND or OR operator must be present between all
  294. query terms. Each term may also be preceded by NOT operator. The AND operator has higher
  295. precedence than the OR operator. This differs from Java Lucene behavior.
  296. </para>
  297. <sect3 id="zend.search.lucene.query-language.boolean.and">
  298. <title>AND</title>
  299. <para>
  300. The AND operator means that all terms in the "AND group" must match some part of the
  301. searched field(s).
  302. </para>
  303. <para>
  304. To search for documents that contain "PHP framework" and "Zend Framework" use the
  305. query:
  306. </para>
  307. <programlisting language="querystring"><![CDATA[
  308. "PHP framework" AND "Zend Framework"
  309. ]]></programlisting>
  310. </sect3>
  311. <sect3 id="zend.search.lucene.query-language.boolean.or">
  312. <title>OR</title>
  313. <para>
  314. The OR operator divides the query into several optional terms.
  315. </para>
  316. <para>
  317. To search for documents that contain "PHP framework" or "Zend Framework" use the
  318. query:
  319. </para>
  320. <programlisting language="querystring"><![CDATA[
  321. "PHP framework" OR "Zend Framework"
  322. ]]></programlisting>
  323. </sect3>
  324. <sect3 id="zend.search.lucene.query-language.boolean.not">
  325. <title>NOT</title>
  326. <para>
  327. The NOT operator excludes documents that contain the term after NOT. But an "AND
  328. group" which contains only terms with the NOT operator gives an empty result set
  329. instead of a full set of indexed documents.
  330. </para>
  331. <para>
  332. To search for documents that contain "PHP framework" but not "Zend Framework" use
  333. the query:
  334. </para>
  335. <programlisting language="querystring"><![CDATA[
  336. "PHP framework" AND NOT "Zend Framework"
  337. ]]></programlisting>
  338. </sect3>
  339. <sect3 id="zend.search.lucene.query-language.boolean.other-form">
  340. <title>&amp;&amp;, ||, and ! operators</title>
  341. <para>
  342. &amp;&amp;, ||, and ! may be used instead of AND, OR, and NOT notation.
  343. </para>
  344. </sect3>
  345. <sect3 id="zend.search.lucene.query-language.boolean.plus">
  346. <title>+</title>
  347. <para>
  348. The "+" or required operator stipulates that the term after the "+" symbol must
  349. match the document.
  350. </para>
  351. <para>
  352. To search for documents that must contain "Zend" and may contain "Framework" use the
  353. query:
  354. </para>
  355. <programlisting language="querystring"><![CDATA[
  356. +Zend Framework
  357. ]]></programlisting>
  358. </sect3>
  359. <sect3 id="zend.search.lucene.query-language.boolean.minus">
  360. <title>-</title>
  361. <para>
  362. The "-" or prohibit operator excludes documents that match the term after the "-"
  363. symbol.
  364. </para>
  365. <para>
  366. To search for documents that contain "PHP framework" but not "Zend Framework" use
  367. the query:
  368. </para>
  369. <programlisting language="querystring"><![CDATA[
  370. "PHP framework" -"Zend Framework"
  371. ]]></programlisting>
  372. </sect3>
  373. <sect3 id="zend.search.lucene.query-language.boolean.no-operator">
  374. <title>No Operator</title>
  375. <para>
  376. If no operator is used, then the search behavior is defined by the "default boolean
  377. operator".
  378. </para>
  379. <para>
  380. This is set to <code>OR</code> by default.
  381. </para>
  382. <para>
  383. That implies each term is optional by default. It may or may not be present within
  384. document, but documents with this term will receive a higher score.
  385. </para>
  386. <para>
  387. To search for documents that requires "PHP framework" and may contain "Zend
  388. Framework" use the query:
  389. </para>
  390. <programlisting language="querystring"><![CDATA[
  391. +"PHP framework" "Zend Framework"
  392. ]]></programlisting>
  393. <para>
  394. The default boolean operator may be set or retrieved with the
  395. <classname>Zend_Search_Lucene_Search_QueryParser::setDefaultOperator($operator)</classname>
  396. and
  397. <classname>Zend_Search_Lucene_Search_QueryParser::getDefaultOperator()</classname>
  398. methods, respectively.
  399. </para>
  400. <para>
  401. These methods operate with the
  402. <classname>Zend_Search_Lucene_Search_QueryParser::B_AND</classname> and
  403. <classname>Zend_Search_Lucene_Search_QueryParser::B_OR</classname> constants.
  404. </para>
  405. </sect3>
  406. </sect2>
  407. <sect2 id="zend.search.lucene.query-language.grouping">
  408. <title>Grouping</title>
  409. <para>
  410. Java Lucene and <classname>Zend_Search_Lucene</classname> support using parentheses to
  411. group clauses to form sub queries. This can be useful if you want to control the
  412. precedence of boolean logic operators for a query or mix different boolean query styles:
  413. </para>
  414. <programlisting language="querystring"><![CDATA[
  415. +(framework OR library) +php
  416. ]]></programlisting>
  417. <para>
  418. <classname>Zend_Search_Lucene</classname> supports subqueries nested to any level.
  419. </para>
  420. </sect2>
  421. <sect2 id="zend.search.lucene.query-language.field-grouping">
  422. <title>Field Grouping</title>
  423. <para>
  424. Lucene also supports using parentheses to group multiple clauses to a single field.
  425. </para>
  426. <para>
  427. To search for a title that contains both the word "return" and the phrase "pink panther"
  428. use the query:
  429. </para>
  430. <programlisting language="querystring"><![CDATA[
  431. title:(+return +"pink panther")
  432. ]]></programlisting>
  433. </sect2>
  434. <sect2 id="zend.search.lucene.query-language.escaping">
  435. <title>Escaping Special Characters</title>
  436. <para>
  437. Lucene supports escaping special characters that are used in query syntax. The current
  438. list of special characters is:
  439. </para>
  440. <para>
  441. + - &amp;&amp; || ! ( ) { } [ ] ^ " ~ * ? : \
  442. </para>
  443. <para>
  444. + and - inside single terms are automatically treated as common characters.
  445. </para>
  446. <para>
  447. For other instances of these characters use the \ before each special character you'd
  448. like to escape. For example to search for (1+1):2 use the query:
  449. </para>
  450. <programlisting language="querystring"><![CDATA[
  451. \(1\+1\)\:2
  452. ]]></programlisting>
  453. </sect2>
  454. </sect1>