windows+php+lucene

首先你的安装配置php-java-bridge,见此贴:http://zhengdl126.iteye.com/blog/418574,最好能够理解PHP如何调用自定义的JAVA




下载lucene并解压:
http://www.apache.org/dyn/closer.cgi/lucene/java/

假如我的php.ini 是这样设置的

extension=php_java.dll
[Java]
;java.java = "C:\jdk1.6.0_13\bin\java"
java.class.path = "D:\php\ext\JavaBridge.jar;c:\myclasses"   c:\myclasses就是我存放自己的class文件地址
java.java_home = "C:\jdk1.6.0_13\jre"
java.library = "d:\jdk1.2.2\jre\bin\server\jvm.dll"
java.library.path = "D:\php\ext"



首先我要编译lucene中需要的JAVA文件:
把lucene-2.4.1-src\lucene-2.4.1\src\java 下的org目录整体编译,然后拷贝到c:\myclasses下



接着在c:\myclasses下创建 TxtFileIndexer.java
--------------------------------


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(".txt")){
          System.out.println("Indexing file " + dataFiles[i].getCanonicalPath());
          Document document = new Document();
          Reader txtReader = new FileReader(dataFiles[i]);
//        document.add(Field.Text("path",dataFiles[i].getCanonicalPath()));  版本为1.9的时候用的方法
//           document.add(Field.Text("contents",txtReader));
                document.add(new Field("path",dataFiles[i].getCanonicalPath(),Field.Store.YES,Field.Index.TOKENIZED));
                document.add(new Field("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;
    }

}


--------------------------------

C:\myclasses>javac TxtFileIndexer.java  编译成TxtFileIndexer.class


在web下创建test.php

<?php

    error_reporting(0);

    java_require("C:/myclasses");

    $tf = new Java('TxtFileIndexer');
    $s = $tf->test();
    print "TxtFileIndexer->test()<br />".$s;
    echo "<hr />";

    $data_path = "C:/myclasses/data/manual";
    $index_path = "C:/myclasses/data/search";

    if($_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($_GET["w"] != "")
        {
            $s = $tf->searchword($_GET["w"],$index_path);
            print $s;
        }
    }
?>
最后访问test.php就可以看到正常页面。附件中有整个测试源码和编译后的JAVA文件供下载

 

 

 

 

继续完善:

 

对test.php做部分修改:

 

 

 if($_GET["w"] != "")
        {
                $tf->createIndex($index_path,$data_path);//此行为增加,在$data_path目录下建立几个*.txt文件
       
       
            $s = $tf->searchword($_GET["w"],$index_path);
            print $s;
        }

 

执行完了以后就可以发现$index_path目录下会增加个文件,我想 应该是建立索引的文件,但是当在页面上提交搜索的时候,却没有搜索到的字符,出现这样的内容:

[o(String):"Result:
"]

 

 

不是很了解lucene的内部原理,不知道如何改写了。明天继续。。。

 

 

 

 

你可能感兴趣的:(java,apache,windows,PHP,Lucene)