您的位置:首页 > 编程语言 > PHP开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: