程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> 用PHP調用Lucene包來實現全文檢索

用PHP調用Lucene包來實現全文檢索

編輯:PHP綜合

由於工作需要,需要使用PHP實現對網站內大量數量進行全文檢索,而且目前最流行的全文檢索的搜索 引擎庫就是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 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;
  }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved