lucene-searching.xml 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. <property>id</property> and <property>score</property>.
  20. </para>
  21. <para>
  22. <property>id</property> is an internal document identifier used within a Lucene index.
  23. It may be used for a variety of operations, including deleting a document from the
  24. index:
  25. </para>
  26. <example id="learning.lucene.searching.delete-example">
  27. <title>Deleting an Indexed Document</title>
  28. <programlisting language="php"><![CDATA[
  29. $index->delete($id);
  30. ]]></programlisting>
  31. </example>
  32. <para>
  33. Or retrieving the document from the index:
  34. </para>
  35. <example id="learning.lucene.searching.retrieve-example">
  36. <title>Retrieving an Indexed Document</title>
  37. <programlisting language="php"><![CDATA[
  38. $doc = $index->getDocument($id);
  39. ]]></programlisting>
  40. </example>
  41. <note id="learning.lucene.searching.identifiers">
  42. <title>Internal Document Identifiers</title>
  43. <para>
  44. Important note! Internal document identifiers may be changed by index optimization or
  45. the auto-optimization process, but it's never changed within a single script's execution
  46. unless the <methodname>addDocument()</methodname> (which may involve an
  47. auto-optimization procedure) or <methodname>optimize()</methodname> methods are called.
  48. </para>
  49. </note>
  50. <para>
  51. The <property>score</property> field is a hit score. Search results are ordered by score by
  52. default (best results returned first).
  53. </para>
  54. <para>
  55. It's also possible to order result sets by specific field values. See the <link
  56. linkend="zend.search.lucene.searching.sorting">
  57. <classname>Zend_Search_Lucene</classname> documentation</link> for more details about
  58. this possibility.
  59. </para>
  60. <para>
  61. The example also demonstrates an ability to access stored fields (e.g.,
  62. <command>$hit->title</command>). At the first access to any hit property other than
  63. <property>id</property> or <property>score</property>, document stored fields are loaded,
  64. and the corresponding field value is returned.
  65. </para>
  66. <para>
  67. This causes an ambiguity for documents having their own <property>id</property> or
  68. <property>score</property> fields; as a result, it's not recommended to use these field
  69. names within stored documents. Nevertheless, they still can be accessed via the
  70. <methodname>getDocument()</methodname> method:
  71. </para>
  72. <example id="learning.lucene.searching.id-score-fields">
  73. <title>Accessing the original document's "id" and "score" fields</title>
  74. <programlisting language="php"><![CDATA[
  75. $id = $hit->getDocument()->id;
  76. $score = $hit->getDocument()->score;
  77. ]]></programlisting>
  78. </example>
  79. </sect1>