Zend_Search_Lucene-QueryLanguage.xml 19 KB

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