2
0

lucene-queries.xml 6.9 KB

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