Package Home

Zend Framework 2 Documentation (Manual)

PHK Home

File: /modules/zendsearch.lucene.index-creation.html

Size:26649
Storage flags:no_autoload,compress/gzip (23%)

Building Indexes — Zend Framework 2 2.4.2 documentation

Building Indexes

Creating a New Index

Index creation and updating capabilities are implemented within the Zend\Search\Lucene component, as well as the Java Lucene project. You can use either of these options to create indexes that Zend\Search\Lucene can search.

The PHP code listing below provides an example of how to index a file using Zend\Search\Lucene indexing API:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// Create index
$index = Zend\Search\Lucene::create('/data/my-index');

$doc = new Zend\Search\Lucene\Document();

// Store document URL to identify it in the search results
$doc->addField(Zend\Search\Lucene\Field::Text('url', $docUrl));

// Index document contents
$doc->addField(Zend\Search\Lucene\Field::UnStored('contents', $docContent));

// Add document to the index
$index->addDocument($doc);

Newly added documents are immediately searchable in the index.

Updating Index

The same procedure is used to update an existing index. The only difference is that the open() method is called instead of the create() method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// Open existing index
$index = Zend\Search\Lucene::open('/data/my-index');

$doc = new Zend\Search\Lucene\Document();
// Store document URL to identify it in search result.
$doc->addField(Zend\Search\Lucene\Field::Text('url', $docUrl));
// Index document content
$doc->addField(Zend\Search\Lucene\Field::UnStored('contents',
                                                  $docContent));

// Add document to the index.
$index->addDocument($doc);

Updating Documents

The Lucene index file format doesn’t support document updating. Documents should be removed and re-added to the index to effectively update them.

Zend\Search\Lucene::delete() method operates with an internal index document id. It can be retrieved from a query hit by ‘id’ property:

1
2
3
4
5
$removePath = ...;
$hits = $index->find('path:' . $removePath);
foreach ($hits as $hit) {
    $index->delete($hit->id);
}

Retrieving Index Size

There are two methods to retrieve the size of an index in Zend\Search\Lucene.

Zend\Search\Lucene::maxDoc() returns one greater than the largest possible document number. It’s actually the overall number of the documents in the index including deleted documents, so it has a synonym: Zend\Search\Lucene::count().

Zend\Search\Lucene::numDocs() returns the total number of non-deleted documents.

1
2
$indexSize = $index->count();
$documents = $index->numDocs();

Zend\Search\Lucene::isDeleted($id) method may be used to check if a document is deleted.

1
2
3
4
5
for ($count = 0; $count < $index->maxDoc(); $count++) {
    if ($index->isDeleted($count)) {
        echo "Document #$id is deleted.\n";
    }
}

Index optimization removes deleted documents and squeezes documents’ IDs in to a smaller range. A document’s internal id may therefore change during index optimization.

Index optimization

A Lucene index consists of many segments. Each segment is a completely independent set of data.

Lucene index segment files can’t be updated by design. A segment update needs full segment reorganization. See Lucene index file formats for details (http://lucene.apache.org/java/2_3_0/fileformats.html) [1]. New documents are added to the index by creating new segment.

Increasing number of segments reduces quality of the index, but index optimization restores it. Optimization essentially merges several segments into a new one. This process also doesn’t update segments. It generates one new large segment and updates segment list (‘segments’ file).

Full index optimization can be trigger by calling the Zend\Search\Lucene::optimize() method. It merges all index segments into one new segment:

1
2
3
4
5
// Open existing index
$index = Zend\Search\Lucene::open('/data/my-index');

// Optimize index.
$index->optimize();

Automatic index optimization is performed to keep indexes in a consistent state.

Automatic optimization is an iterative process managed by several index options. It merges very small segments into larger ones, then merges these larger segments into even larger segments and so on.

MaxBufferedDocs auto-optimization option

MaxBufferedDocs is a minimal number of documents required before the buffered in-memory documents are written into a new segment.

MaxBufferedDocs can be retrieved or set by $index->getMaxBufferedDocs() or $index->setMaxBufferedDocs($maxBufferedDocs) calls.

Default value is 10.

MaxMergeDocs auto-optimization option

MaxMergeDocs is a largest number of documents ever merged by addDocument(). Small values (e.g., less than 10.000) are best for interactive indexing, as this limits the length of pauses while indexing to a few seconds. Larger values are best for batched indexing and speedier searches.

MaxMergeDocs can be retrieved or set by $index->getMaxMergeDocs() or $index->setMaxMergeDocs($maxMergeDocs) calls.

Default value is PHP_INT_MAX.

MergeFactor auto-optimization option

MergeFactor determines how often segment indices are merged by addDocument(). With smaller values, less RAM is used while indexing, and searches on unoptimized indices are faster, but indexing speed is slower. With larger values, more RAM is used during indexing, and while searches on unoptimized indices are slower, indexing is faster. Thus larger values (> 10) are best for batch index creation, and smaller values (< 10) for indices that are interactively maintained.

MergeFactor is a good estimation for average number of segments merged by one auto-optimization pass. Too large values produce large number of segments while they are not merged into new one. It may be a cause of “failed to open stream: Too many open files” error message. This limitation is system dependent.

MergeFactor can be retrieved or set by $index->getMergeFactor() or $index->setMergeFactor($mergeFactor) calls.

Default value is 10.

Lucene Java and Luke (Lucene Index Toolbox -http://www.getopt.org/luke/) can also be used to optimize an index. Latest Luke release (v0.8) is based on Lucene v2.3 and compatible with current implementation of Zend\Search\Lucene component (Zend Framework 1.6). Earlier versions of Zend\Search\Lucene implementations need another versions of Java Lucene tools to be compatible:

Permissions

By default, index files are available for reading and writing by everyone.

It’s possible to override this with the Zend\Search\Lucene\Storage\Directory\Filesystem::setDefaultFilePermissions() method:

1
2
3
4
5
6
// Get current default file permissions
$currentPermissions =
    Zend\Search\Lucene\Storage\Directory\Filesystem::getDefaultFilePermissions();

// Give read-writing permissions only for current user and group
Zend\Search\Lucene\Storage\Directory\Filesystem::setDefaultFilePermissions(0660);

Limitations

Index size

Index size is limited by 2GB for 32-bit platforms.

Use 64-bit platforms for larger indices.

Supported Filesystems

Zend\Search\Lucene uses flock() to provide concurrent searching, index updating and optimization.

According to the PHP documentation, “flock() will not work on NFS and many other networked file systems”.

Do not use networked file systems with Zend\Search\Lucene.

[1]The currently supported Lucene index file format is version 2.3 (starting from Zend Framework 1.6).
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 Building Indexes 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