首页 »PHP教程 » lucene:用PHP调用Lucene包来实现全文检索 »正文
lucene:用PHP调用Lucene包来实现全文检索
来源: 发布时间:星期一, 2009年1月12日 浏览:8次 评论:0
由于工作需要 需要使用PHP实现对网站WebSite内大量数量进行全文检索 而且目前最流行 全文检索 搜索引擎库就是Lucene了 它是Apache Jakarta 个子项目 并且提供了简单实用 API 用这些API 就可以对任何基本文本 数据(包括数据库)进行全文检索 PHP本身就支持 外部Java类 所以先用Java写了 个类 这个类通过 Lucene API 实现了两个思路方法: * public String createIndex(String indexDir_path,String dataDir_path) * public String searchword(String ss,String index_path) 其中createIndex是创建索引思路方法 传入了两个参数分别是indexDir_path(索引文件 目录) dataDir_path(被索引 文件目录) 返回被索引 文件列表 串 另 个是searchword 通过传入 关键字参数(ss)对索引进行检索 index_path就是索引文件 目录 返回所有检索到 文件 这里是源代码 很简单 大家可以参考 下:TxtFileIndexer.java package TestLucene; import java.io.File;import java.io.FileReader; import java.io.Reader; import java.util.Date; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.FSDirectory; public TxtFileIndexer ...{ public String test ...{ "test is ok hohoho"; } /**//** * @param args */ public String createIndex(String indexDir_path,String dataDir_path) throws Exception ...{ String result = ""; File indexDir = File(indexDir_path); File dataDir = File(dataDir_path); Analyzer luceneAnalyzer = StandardAnalyzer; File dataFiles = dataDir.listFiles; IndexWriter indexWriter = IndexWriter(indexDir,luceneAnalyzer,true); long startTime = Date.getTime; for( i=0; i < dataFiles.length; i) ...{ (dataFiles[i].isFile && dataFiles[i].getName.endsWith(".html")) ...{ result "Indexing file" + dataFiles[i].getCanonicalPath+"<br />"; Document document = Document; Reader txtReader = FileReader(dataFiles[i]); document.add(Field.Text("path",dataFiles[i].getCanonicalPath)); document.add(Field.Text("contents",txtReader)); indexWriter.addDocument(document); } } indexWriter.optimize; indexWriter.close; long endTime = Date.getTime; result "It takes"+(endTime-startTime) + " milliseconds to create index for the files in directory " + dataDir.getPath; result; } public String searchword(String ss,String index_path) throws Exception ...{ String queryStr = ss; String result = "Result:<br />"; //This is the directory that hosts the Lucene index File indexDir = File(index_path); FSDirectory directory = FSDirectory.getDirectory(indexDir,false); IndexSearcher searcher = IndexSearcher(directory); (!indexDir.exists)...{ result = "The Lucene index is not exist"; result; } Term term = Term("contents",queryStr.toLowerCase); TermQuery luceneQuery = TermQuery(term); Hits hits = searcher.search(luceneQuery); for( i = 0; i < hits.length; i)...{ Document document = hits.doc(i); result "<br /><a href='getfile.php?w="+ss+"&f="+document.get("path")+"'>File: " + document.get("path")+"</a>n"; } result; } }
而PHP 就 这两个思路方法 实现对Lucene 从而达到全文检索 目 PHP 思路方法如下: 先创建 个我们写 TxtFileIndexer类 例子 $tf = 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类思路方法 pr $s; //打印返回结果 这次再试试检索: $index_path = "F:/test/php_lucene/htdocs/data/search"; //定义生成索引文件存放目录 $s = $tf->searchword("here is keyword for search",$index_path); pr $s; 另外要注意Java类 路径 可以在PHP里设置 java_require("F:/test/php_lucene/htdocs/lib/"); // 这是个例子 我 类和Lucene都放到这个目录下 这样就可以了 是不是很简单 PHP源代码:test.php <?php error_reporting(0); java_require("F:/test/php_lucene/htdocs/lib/"); $tf = Java('TestLucene.TxtFileIndexer'); $s = $tf->test; pr "TestLucene.TxtFileIndexer->test<br />".$s; echo "<hr />"; $data_path = "F:/test/php_lucene/htdocs/data/manual"; $index_path = "F:/test/php_lucene/htdocs/data/search"; ($_GET["action"] "create") ...{ $s = $tf->createIndex($index_path,$data_path); pr $s; } ...{ echo "<form method=get> <input type=text name=w /><input type=submit value=search /><br />"; ($_GET["w"] != "") ...{ $s = $tf->searchword($_GET["w"],$index_path); pr $s; } } ?> 接下来我把环境配置说 下 先需要有Java SDK 是必须 我使用 是1.4.2版 其它版本应该也没问题 PHP5 试过PHP4 应该可以 由于PHP5带 Java扩展没调通 并且以前用过 Java效率很低 很慢 所以使用了 Php/Java Bridge 这个项目 1. 下载JavaBridge URL:http://sourceforge.net/projects/php-java-bridge/ 目前版本是php-java-bridge_3.0.8_j2ee.zip 解包后把 a.JavaBridgeWEB-INFcgijava-x86-windows.dll b.JavaBridgeWEB-INFlibJavaBridge.jar 复制到 c:phpext 目录下 并把java-x86-windows.dll 改名为 php_java.dll 2.修改php.ini(例) 3.重启Apache即可 4.可以找 些文件进行索引 在test.php里可以修改索引文件和数据文件 路径 TxtFileIndexer.java 37行限制了只索引html后缀 文件 有需要也可以修改 根据目前 情况(JavaBridge支持Linux和Freebsd) 完全可以在linux或freebsd/apache2/php4/lucene/JavaBridge环境下运行
相关文章
读者评论
发表评论
|
|