php引入lucene搜索引擎方法.
2015-11-20 21:45
886 查看
1、lucene包的下载地址:http://apache.etoak.com/lucene/java/3.3.0/
2、下载jdk环境
3、下载JavaBridge URL:http://sourceforge.net/projects/php-java-bridge/
步骤:
1安装好jdk
2下载的JavaBridge.jar拷到php的ext 文件夹下.
3用rar压缩软件打开javaBridge.jar找到java文件夹,该文件夹下大都是后缀是.inc格式文件.把java整个考到www/php_java/下.
4双击javaBridge.jar打开8080端口
5在php_java下写php测试文件
[php] view
plaincopy
<?php
require_once("java/Java.inc");
header("content-type:text/html; charset=utf-8");
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');
$s = new Java("java.lang.String", "php-java-bridge config...<br><br>");
echo $s;
// demonstrate property access
print 'Java version='.$system->getProperty('java.version').' <br>';
print 'Java vendor=' .$system->getProperty('java.vendor').' <br>';
print 'OS='.$system->getProperty('os.name').' '.
$system->getProperty('os.version').' on '.
$system->getProperty('os.arch').' <br>';
// java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
print $formatter->format(new Java('java.util.Date'));
[php] view
plaincopy
?>
结果:
php-java-bridge config...
Java version=1.6.0_10-rc2
Java vendor=Sun Microsystems Inc.
OS=Windows XP 5.1 on x86
星期五, 八月 12, 2011 at 9:38:16 上午 中国标准时间Java version=1.6.0_10-rc2
用eclipse写一个TestLucene包类命名为TxtFileIndexer.java
[java] view
plaincopy
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 class TxtFileIndexer ...{
public String test() ...{
return "test is ok hohoho";
}
/**//**
* @param args
*/
public String createIndex(String indexDir_path,String dataDir_path) throws Exception ...{
String result = "";
File indexDir = new File(indexDir_path);
File dataDir = new File(dataDir_path);
Analyzer luceneAnalyzer = new StandardAnalyzer();
File[] dataFiles = dataDir.listFiles();
IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);
long startTime = new Date().getTime();
for(int i=0; i < dataFiles.length; i++) ...{
if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".html")) ...{
result += "Indexing file" + dataFiles[i].getCanonicalPath()+"<br />";
Document document = new Document();
Reader txtReader = new 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 = new Date().getTime();
result += "It takes"+(endTime-startTime)
+ " milliseconds to create index for the files in directory "
+ dataDir.getPath();
return 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 = new File(index_path);
FSDirectory directory = FSDirectory.getDirectory(indexDir,false);
IndexSearcher searcher = new IndexSearcher(directory);
if(!indexDir.exists())...{
result = "The Lucene index is not exist";
return result;
}
Term term = new Term("contents",queryStr.toLowerCase());
TermQuery luceneQuery = new TermQuery(term);
Hits hits = searcher.search(luceneQuery);
for(int 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";
}
return result;
}
}
先创建一个我们写的TxtFileIndexer类的实例,
[php] view
plaincopy
$tf = new Java('TestLucene.TxtFileIndexer');
然后就按正常PHP类的调用方法的方式进行调用,首先创建索引:
[php] view
plaincopy
$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; //打印返回的结果
[php] view
plaincopy
$index_path = "F:/test/php_lucene/htdocs/data/search"; //定义生成的索引文件存放目录
[php] view
plaincopy
$s = $tf->searchword("here is keyword for search",$index_path);
[php] view
plaincopy
print $s;
[php] view
plaincopy
<span style="color:#000000;">java_require(</span><span style="color:#000000;">"</span><span style="color:#000000;">F:/test/php_lucene/htdocs/lib/</span><span style="color:#000000;">"</span><span style="color:#000000;">); </span><span style="color:#008000;">//</span><span style="color:#008000;">这是个例子,我的类和Lucene都放到这个目录下,这样就可以了,是不是很简单。 </span>
[php] view
plaincopy
<span style="color:#008000;">测试代码</span>
<pre class="php" name="code"><?php
error_reporting(0);
java_require("F:/test/php_lucene/htdocs/lib/");
$tf = new Java('TestLucene.TxtFileIndexer');
$s = $tf->test();
print "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";
if(<pre class="php" name="code">{1}</pre><br>
GET["action"] == "create") ...{ $s = $tf->createIndex($index_path,$data_path); print $s; }else ...{ echo "<form method=get> <input type=text name=w /><input type=submit value=search /><br />"; if(<pre class="php" name="code">{1}</pre><br>
GET["w"] != "") ...{ $s = $tf->searchword(<pre class="php" name="code">{1}</pre><br>
GET["w"],$index_path); print $s; } }?>
<pre></pre>
<br>
<pre></pre>
转载:<a href="http://www.lucene.com.cn/php.htm">http://www.lucene.com.cn/php.htm</a><br>
</pre>
2、下载jdk环境
3、下载JavaBridge URL:http://sourceforge.net/projects/php-java-bridge/
步骤:
1安装好jdk
2下载的JavaBridge.jar拷到php的ext 文件夹下.
3用rar压缩软件打开javaBridge.jar找到java文件夹,该文件夹下大都是后缀是.inc格式文件.把java整个考到www/php_java/下.
4双击javaBridge.jar打开8080端口
5在php_java下写php测试文件
[php] view
plaincopy
<?php
require_once("java/Java.inc");
header("content-type:text/html; charset=utf-8");
// get instance of Java class java.lang.System in PHP
$system = new Java('java.lang.System');
$s = new Java("java.lang.String", "php-java-bridge config...<br><br>");
echo $s;
// demonstrate property access
print 'Java version='.$system->getProperty('java.version').' <br>';
print 'Java vendor=' .$system->getProperty('java.vendor').' <br>';
print 'OS='.$system->getProperty('os.name').' '.
$system->getProperty('os.version').' on '.
$system->getProperty('os.arch').' <br>';
// java.util.Date example
$formatter = new Java('java.text.SimpleDateFormat',
"EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
print $formatter->format(new Java('java.util.Date'));
[php] view
plaincopy
?>
结果:
php-java-bridge config...
Java version=1.6.0_10-rc2
Java vendor=Sun Microsystems Inc.
OS=Windows XP 5.1 on x86
星期五, 八月 12, 2011 at 9:38:16 上午 中国标准时间Java version=1.6.0_10-rc2
用eclipse写一个TestLucene包类命名为TxtFileIndexer.java
[java] view
plaincopy
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 class TxtFileIndexer ...{
public String test() ...{
return "test is ok hohoho";
}
/**//**
* @param args
*/
public String createIndex(String indexDir_path,String dataDir_path) throws Exception ...{
String result = "";
File indexDir = new File(indexDir_path);
File dataDir = new File(dataDir_path);
Analyzer luceneAnalyzer = new StandardAnalyzer();
File[] dataFiles = dataDir.listFiles();
IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);
long startTime = new Date().getTime();
for(int i=0; i < dataFiles.length; i++) ...{
if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".html")) ...{
result += "Indexing file" + dataFiles[i].getCanonicalPath()+"<br />";
Document document = new Document();
Reader txtReader = new 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 = new Date().getTime();
result += "It takes"+(endTime-startTime)
+ " milliseconds to create index for the files in directory "
+ dataDir.getPath();
return 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 = new File(index_path);
FSDirectory directory = FSDirectory.getDirectory(indexDir,false);
IndexSearcher searcher = new IndexSearcher(directory);
if(!indexDir.exists())...{
result = "The Lucene index is not exist";
return result;
}
Term term = new Term("contents",queryStr.toLowerCase());
TermQuery luceneQuery = new TermQuery(term);
Hits hits = searcher.search(luceneQuery);
for(int 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";
}
return result;
}
}
先创建一个我们写的TxtFileIndexer类的实例,
[php] view
plaincopy
$tf = new Java('TestLucene.TxtFileIndexer');
然后就按正常PHP类的调用方法的方式进行调用,首先创建索引:
[php] view
plaincopy
$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; //打印返回的结果
[php] view
plaincopy
$index_path = "F:/test/php_lucene/htdocs/data/search"; //定义生成的索引文件存放目录
[php] view
plaincopy
$s = $tf->searchword("here is keyword for search",$index_path);
[php] view
plaincopy
print $s;
[php] view
plaincopy
<span style="color:#000000;">java_require(</span><span style="color:#000000;">"</span><span style="color:#000000;">F:/test/php_lucene/htdocs/lib/</span><span style="color:#000000;">"</span><span style="color:#000000;">); </span><span style="color:#008000;">//</span><span style="color:#008000;">这是个例子,我的类和Lucene都放到这个目录下,这样就可以了,是不是很简单。 </span>
[php] view
plaincopy
<span style="color:#008000;">测试代码</span>
<pre class="php" name="code"><?php
error_reporting(0);
java_require("F:/test/php_lucene/htdocs/lib/");
$tf = new Java('TestLucene.TxtFileIndexer');
$s = $tf->test();
print "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";
if(<pre class="php" name="code">{1}</pre><br>
GET["action"] == "create") ...{ $s = $tf->createIndex($index_path,$data_path); print $s; }else ...{ echo "<form method=get> <input type=text name=w /><input type=submit value=search /><br />"; if(<pre class="php" name="code">{1}</pre><br>
GET["w"] != "") ...{ $s = $tf->searchword(<pre class="php" name="code">{1}</pre><br>
GET["w"],$index_path); print $s; } }?>
<pre></pre>
<br>
<pre></pre>
转载:<a href="http://www.lucene.com.cn/php.htm">http://www.lucene.com.cn/php.htm</a><br>
</pre>
相关文章推荐
- 一步一步制作yaffs/yaffs2根文件系统(一)---储备好基础知识再打
- PHP设计模式之单例模式
- PHP中命名空间
- Yii2实现读写分离(MySQL主从数据库)
- 【文件】FileOutputStream 出现 FileNotFoundException
- php关于使用iconv(...)函数对字符进行中文转换时,遇到的一个问题
- PHP操作sphinx流程
- html和php编写系统登录和信息查询
- php: Undefined index: HTTP_RAW_POST_DATA
- PHP获取POST数据的几种方法汇总
- 关于thinkphp中自带的分页类中setConfig("last","尾页")无效的解决方法
- PHP Fatal error: Call to undefined function curl_init() in xxx
- php 截取视频图片
- 【ThinkPHP】分页写法
- 考虑 PHP 5.0~5.6 各版本兼容性的 cURL 文件上传
- PHP 代码加密
- 在Dreamweaver下创建一个php文件并用正确使用php“绘图函数”进行绘制图形时,图形却无法显示
- PHP 编译参数
- php日期,时间相关知识整理
- Zend Studio 导入项目:某些项目因位于工作空间目录而被隐藏