Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /tutorials/lucene.searching.html

Size:12052
Storage flags:no_autoload,compress/gzip (29%)

Searching — Zend Framework 2 2.4.2 documentation

SearchingΒΆ

Searching is performed by using the find() method:

Searching through the index

1
2
3
4
5
$hits = $index->find($query);

foreach ($hits as $hit) {
    printf("%d %f %s\n", $hit->id, $hit->score, $hit->title);
}

This example demonstrates the usage of two special search hit properties -id and score.

id is an internal document identifier used within a Lucene index. It may be used for a variety of operations, including deleting a document from the index:

Deleting an Indexed Document

1
$index->delete($id);

Or retrieving the document from the index:

Retrieving an Indexed Document

1
$doc = $index->getDocument($id);

Note

Internal Document Identifiers

Important note! Internal document identifiers may be changed by index optimization or the auto-optimization process, but it’s never changed within a single script’s execution unless the addDocument() (which may involve an auto-optimization procedure) or optimize() methods are called.

The score field is a hit score. Search results are ordered by score by default (best results returned first).

It’s also possible to order result sets by specific field values. See the ZendSearchLucene documentation for more details about this possibility.

The example also demonstrates an ability to access stored fields (e.g., $hit->title). At the first access to any hit property other than id or score, document stored fields are loaded, and the corresponding field value is returned.

This causes an ambiguity for documents having their own id or score fields; as a result, it’s not recommended to use these field names within stored documents. Nevertheless, they still can be accessed via the getDocument() method:

Accessing the original document’s “id” and “score” fields

1
2
$id    = $hit->getDocument()->id;
$score = $hit->getDocument()->score;

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to Searching on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.

For more information about the PHK package format: http://phk.tekwire.net