用PHP调用Lucene包来实现全文检索
PHP的调用方法如下:
先创建一个我们写的TxtFileIndexer类的实例,
$tf = new Java('TestLucene.TxtFileIndexer');
然后就按正常PHP类的调用方法的方式进行调用,首先创建索引:
$data_path = "F:/test/php_lucene/htdocs/data/manual"; //定义被索引内容的目录
$index_path = "F:/test/php_lucene/htdocs/data/search"; //定义生成的索引文件存放目录
$s = $tf->createIndex($index_path,$data_path); //调用Java类的方法
print $s; //打印返回的结果
这次再试试检索:
$index_path = "F:/test/php_lucene/htdocs/data/search"; //定义生成的索引文件存放目录
$s = $tf->searchword("here is keyword for search",$index_path);
print $s;
另外要注意Java类的路径,可以在PHP里设置
java_require("F:/test/php_lucene/htdocs/lib/"); //
这是个例子,我的类和Lucene都放到这个目录下,这样就可以了,是不是很简单。
PHP源代码:test.php
error_reporting(0);
java_require("F:/test/php_lucene/htdocs/lib/");
$tf = new Java('TestLucene.TxtFileIndexer');
$s = $tf->test();
print "TestLucene.TxtFileIndexer->test()
".$s;
echo "
";
$data_path = "F:/test/php_lucene/htdocs/data/manual";
$index_path = "F:/test/php_lucene/htdocs/data/search";
if($_GET["action"] == "create") ...{
$s = $tf->createIndex($index_path,$data_path);
print $s;
}else ...{
echo "
";
if($_GET["w"] != "") ...{
$s = $tf->searchword($_GET["w"],$index_path);
print $s;
}
}
?>