re sMash Application -> Run,以运行应用程序。将 Web 浏览器指向本地服务器,如 http://localhost:8080/index.php.其外观与图 8 中所示类似。
图 8. “选择目录和文件扩展名”页
8、不要尝试建立任何内容的索引,因为还需要添加其他代码。最后,提交表单时,PHP 脚本将创建 Lucene 搜索索引,并使用目录中具有匹配扩展名的所有文件对其进行填充。也会向下递归到开始目录,并在此过程中添加相应的文件。
9、接下来,将以下 PHP 代码添加到 index.php 中:
<?php
$directory = dirname(__FILE__)."/../index";
if (file_exists($directory) === FALSE) {
mkdir($directory);
}
define("INDEX_DIRECTORY", $directory);
try {
$extension = zget(''/request/params/extension'');
if (strlen($extension) > 0) {
$directory = zget(''/request/params/directory'');
if (strlen($directory) > 0) {
index_directory($directory, $extension);
}
}
} catch (JavaException $exception) {
echo "Index creation failed [".
$exception->getMessage()."]</br>";
}
?>
在WebSphere sMash中集成Java和PHP(9)
时间:2011-07-06 IBM Anthony Phillips,Zoe
10、尚不要运行,因为还没有完成!代码从全局上下文获取表单变量,并检查是否已经填充。如果已经跳出,则会调用 index_directory 函数。此函数将在后面进行说明,负责将任何匹配的文件添加到 Lucene 搜索引擎。
11、接下来,将以下 PHP 代码添加到 index.php 中:
/**
* This creates an index from scratch and adds all the documents
* by recursing from the directory passed in. It also checks
* each candidate file to see if it matches the file extension.
*/
function index_directory($path, $extension) {
echo "Indexing! [".$path.",".$extension."]</br>";
// Uses the SimpleAnalyzer because we will do a performance comparison
with the PHP
// implementation of Lucene in the Zend Framework and it is the closest match
$analyser = new Java("org.apache.lucene.analysis.SimpleAnalyzer");
$policy = new Java("org.apache.lucene.index.KeepOnlyLastCommitDeletionPolicy");
$file = new Java("java.io.File", INDEX_DIRECTORY, FALSE);
$file_directory = new JavaClass("org.apache.lucene.store.FSDirectory");
$directory = $file_directory->getDirectory($file);
$writer = new Java("org.apache.lucene.index.IndexWriter",
$directory, TRUE, $analyser, TRUE, $policy);
$writer->setUseCompoundFile(FALSE);
// Insert some calls to microtime() for comparison
$start_time = get_microtime();
recursive_index_directory($writer, $path, $extension);
$count = $writer->docCount();
// Lucene only matches the first 10,000 tokens by default
$writer->setMaxFieldLength(1000000);
$end_index_time = get_microtime();
$writer->optimize();
$end_time = get_microtime();
$writer->close();
echo "Finished indexing [".$count." documents]</br>";
$t1 = $end_index_time - $start_time;
$t2 = $end_time - $end_index_time;
echo "Time to index = $t1 </br>";
echo "Time to optimize = $t2 </br>";
}
在WebSphere sMash中集 |