php引入lucene搜索引擎方法.

 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

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-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 

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类的实例,

$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_require("F:/test/php_lucene/htdocs/lib/"); //这是个例子,我的类和Lucene都放到这个目录下,这样就可以了,是不是很简单。 
测试代码
<?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(
{1}

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(
{1}

GET["w"] != "") ...{ $s = $tf->searchword(
{1}

GET["w"],$index_path); print $s; } }?>



转载:http://www.lucene.com.cn/php.htm

你可能感兴趣的:(java,PHP,String,搜索引擎,Lucene,Path)