lucene-queries.xml 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- EN-Revision: 19777 -->
  3. <!-- Reviewed: no -->
  4. <sect1 id="learning.lucene.queries">
  5. <title>Supported queries</title>
  6. <para>
  7. <classname>Zend_Search_Lucene</classname> and Java Lucene support a powerful query language.
  8. It allows searching for individual terms, phrases, ranges of terms; using wildcards and
  9. fuzzy search; combining queries using boolean operators; and so on.
  10. </para>
  11. <para>
  12. A detailed query language description can be found in the <link
  13. linkend="zend.search.lucene.query-language">
  14. Zend_Search_Lucene component documentation</link>.
  15. </para>
  16. <para>
  17. What follows are examples of some common query types and strategies.
  18. </para>
  19. <example id="learning.lucene.queries.keyword">
  20. <title>Querying for a single word</title>
  21. <programlisting language="text"><![CDATA[
  22. hello
  23. ]]></programlisting>
  24. <para>
  25. Searches for the word "hello" through all document fields.
  26. </para>
  27. </example>
  28. <note>
  29. <title>Default search field</title>
  30. <para>
  31. Important note! Java Lucene searches only through the "contents" field by default, but
  32. <classname>Zend_Search_Lucene</classname> searches through <emphasis>all</emphasis>
  33. fields. This behavior can be modified using the
  34. <methodname>Zend_Search_Lucene::setDefaultSearchField($fieldName)</methodname> method.
  35. </para>
  36. </note>
  37. <example id="learning.lucene.queries.multiple-words">
  38. <title>Querying for multiple words</title>
  39. <programlisting language="text"><![CDATA[
  40. hello dolly
  41. ]]></programlisting>
  42. <para>
  43. Searches for two words. Both words are optional; at least one of them must be present in
  44. the result.
  45. </para>
  46. </example>
  47. <example id="learning.lucene.queries.required-words">
  48. <title>Requiring words in a query</title>
  49. <programlisting language="text"><![CDATA[
  50. +hello dolly
  51. ]]></programlisting>
  52. <para>
  53. Searches for two words; "hello" is required, "dolly" is optional.
  54. </para>
  55. </example>
  56. <example id="learning.lucene.queries.prohibited-words">
  57. <title>Prohibiting words in queried documents</title>
  58. <programlisting language="text"><![CDATA[
  59. +hello -dolly
  60. ]]></programlisting>
  61. <para>
  62. Searches for two words; "hello" is required, 'dolly' is prohibited. In other words, if
  63. the document matches "hello", but contains the word "dolly", it will not be returned in
  64. the set of matches.
  65. </para>
  66. </example>
  67. <example id="learning.lucene.queries.phrases">
  68. <title>Querying for phrases</title>
  69. <programlisting language="text"><![CDATA[
  70. "hello dolly"
  71. ]]></programlisting>
  72. <para>
  73. Searches for the phrase "hello dolly"; a document only matches if that exact string is
  74. present.
  75. </para>
  76. </example>
  77. <example id="learning.lucene.queries.fields">
  78. <title>Querying against specific fields</title>
  79. <programlisting language="text"><![CDATA[
  80. title:"The Right Way" AND text:go
  81. ]]></programlisting>
  82. <para>
  83. Searches for the phrase "The Right Way" within the <property>title</property> field and
  84. the word "go" within the <property>text</property> field.
  85. </para>
  86. </example>
  87. <example id="learning.lucene.queries.fields-and-document">
  88. <title>Querying against specific fields as well as the entire document</title>
  89. <programlisting language="text"><![CDATA[
  90. title:"The Right Way" AND go
  91. ]]></programlisting>
  92. <para>
  93. Searches for the phrase "The Right Way" within the <property>title</property> field and
  94. the word "go" word appearing in any field of the document.
  95. </para>
  96. </example>
  97. <example id="learning.lucene.queries.fields-and-document-alt">
  98. <title>Querying against specific fields as well as the entire document (alternate)</title>
  99. <programlisting language="text"><![CDATA[
  100. title:Do it right
  101. ]]></programlisting>
  102. <para>
  103. Searches for the word "Do" within the <property>title</property> field and the words
  104. "it" and "right" words through all fields; any single one matching will result in
  105. a document match.
  106. </para>
  107. </example>
  108. <example id="learning.lucene.queries.wildcard-question">
  109. <title>Querying with the wildcard "?"</title>
  110. <programlisting language="text"><![CDATA[
  111. te?t
  112. ]]></programlisting>
  113. <para>
  114. Search for words matching the pattern "te?t", where "?" is any single character.
  115. </para>
  116. </example>
  117. <example id="learning.lucene.queries.wildcard-asterisk">
  118. <title>Querying with the wildcard "*"</title>
  119. <programlisting language="text"><![CDATA[
  120. test*
  121. ]]></programlisting>
  122. <para>
  123. Search for words matching the pattern "test*", where "*" is any sequence of zero or more
  124. characters.
  125. </para>
  126. </example>
  127. <example id="learning.lucene.queries.range-inclusive">
  128. <title>Querying for an inclusive range of terms</title>
  129. <programlisting language="text"><![CDATA[
  130. mod_date:[20020101 TO 20030101]
  131. ]]></programlisting>
  132. <para>
  133. Search for the range of terms (inclusive).
  134. </para>
  135. </example>
  136. <example id="learning.lucene.queries.range-exclusive">
  137. <title>Querying for an exclusive range of terms</title>
  138. <programlisting language="text"><![CDATA[
  139. title:{Aida to Carmen}
  140. ]]></programlisting>
  141. <para>
  142. Search for the range of terms (exclusive).
  143. </para>
  144. </example>
  145. <example id="learning.lucene.queries.fuzzy">
  146. <title>Fuzzy searches</title>
  147. <programlisting language="text"><![CDATA[
  148. roam~
  149. ]]></programlisting>
  150. <para>
  151. Fuzzy search for the word "roam".
  152. </para>
  153. </example>
  154. <example id="learning.lucene.queries.boolean">
  155. <title>Boolean searches</title>
  156. <programlisting language="text"><![CDATA[
  157. (framework OR library) AND php
  158. ]]></programlisting>
  159. <para>
  160. Boolean query.
  161. </para>
  162. </example>
  163. <para>
  164. All supported queries can be constructed through <classname>Zend_Search_Lucene</classname>'s
  165. <link linkend="zend.search.lucene.query-api"> query
  166. construction API</link>. Moreover, query parsing and query constructing may be
  167. combined:
  168. </para>
  169. <example id="learning.lucene.queries.combining">
  170. <title>Combining parsed and constructed queries</title>
  171. <programlisting language="php"><![CDATA[
  172. $userQuery = Zend_Search_Lucene_Search_QueryParser::parse($queryStr);
  173. $query = new Zend_Search_Lucene_Search_Query_Boolean();
  174. $query->addSubquery($userQuery, true /* required */);
  175. $query->addSubquery($constructedQuery, true /* required */);
  176. ]]></programlisting>
  177. </example>
  178. </sect1>