lucene-searching.xml 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="learning.lucene.searching">
  4. <title>Searching</title>
  5. <para>
  6. Searching is performed by using the <methodname>find()</methodname> method:
  7. </para>
  8. <example id="learning.lucene.searching.search-example">
  9. <title>Searching through the index</title>
  10. <programlisting language="php"><![CDATA[
  11. $hits = $index->find($query);
  12. foreach ($hits as $hit) {
  13. printf(“%d %f %s\n”, $hit->id, $hit->score, $hit->title);
  14. }
  15. ]]></programlisting>
  16. </example>
  17. <para>
  18. This example demonstrates the usage of two special search hit properties -
  19. <varname>id</varname> and <varname>score</varname>.
  20. </para>
  21. <para>
  22. <varname>id</varname> is an internal document identifier used within a Lucene index. It may
  23. be used for a variety of operations, including deleting a document from the index:
  24. </para>
  25. <example id="learning.lucene.searching.delete-example">
  26. <title>Deleting an Indexed Document</title>
  27. <programlisting language="php"><![CDATA[
  28. $index->delete($id);
  29. ]]></programlisting>
  30. </example>
  31. <para>
  32. Or retrieving the document from the index:
  33. </para>
  34. <example id="learning.lucene.searching.retrieve-example">
  35. <title>Retrieving an Indexed Document</title>
  36. <programlisting language="php"><![CDATA[
  37. $doc = $index->getDocument($id);
  38. ]]></programlisting>
  39. </example>
  40. <note id="learning.lucene.searching.identifiers">
  41. <title>Internal Document Identifiers</title>
  42. <para>
  43. Important note! Internal document identifiers may be changed by index optimization or
  44. the auto-optimization process, but it's never changed within a single script's execution
  45. unless the <methodname>addDocument()</methodname> (which may involve an
  46. auto-optimization procedure) or <methodname>optimize()</methodname> methods are called.
  47. </para>
  48. </note>
  49. <para>
  50. The <varname>score</varname> field is a hit score. Search results are ordered by score by
  51. default (best results returned first).
  52. </para>
  53. <para>
  54. It's also possible to order result sets by specific field values. See the <link
  55. linkend="zend.search.lucene.searching.sorting">
  56. <classname>Zend_Search_Lucene</classname> documentation</link> for more details about
  57. this possibility.
  58. </para>
  59. <para>
  60. The example also demonstrates an ability to access stored fields (e.g.,
  61. <code>$hit-&gt;title</code>). At the first access to any hit property other than
  62. <varname>id</varname> or <varname>score</varname>, document stored fields are loaded, and
  63. the corresponding field value is returned.
  64. </para>
  65. <para>
  66. This causes an ambiguity for documents having their own <varname>id</varname> or
  67. <varname>score</varname> fields; as a result, it's not recommended to use these field names
  68. within stored documents. Nevertheless, they still can be accessed via the
  69. <methodname>getDocument()</methodname> method:
  70. </para>
  71. <example id="learning.lucene.searching.id-score-fields">
  72. <title>Accessing the original document's "id" and "score" fields</title>
  73. <programlisting language="php"><![CDATA[
  74. $id = $hit->getDocument()->id;
  75. $score = $hit->getDocument()->score;
  76. ]]></programlisting>
  77. </example>
  78. </sect1>