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]  view plain copy
  1. <?php  
  2.   
  3. require_once("java/Java.inc");  
  4.   
  5. header("content-type:text/html; charset=utf-8");  
  6. // get instance of Java class java.lang.System in PHP  
  7. $system = new Java('java.lang.System');  
  8. $s = new Java("java.lang.String""php-java-bridge config...<br><br>");  
  9. echo $s;  
  10.   
  11. // demonstrate property access  
  12. print 'Java version='.$system->getProperty('java.version').' <br>';  
  13. print 'Java vendor=' .$system->getProperty('java.vendor').' <br>';  
  14. print 'OS='.$system->getProperty('os.name').' '.  
  15. $system->getProperty('os.version').' on '.  
  16. $system->getProperty('os.arch').' <br>';  
  17.   
  18. // java.util.Date example  
  19. $formatter = new Java('java.text.SimpleDateFormat',  
  20. "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");  
  21.   
  22. print $formatter->format(new Java('java.util.Date'));  
[php]  view plain copy
  1. ?>  

结果:

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 plain copy
  1. package TestLucene;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileReader;  
  5. import java.io.Reader;  
  6. import java.util.Date;  
  7.   
  8. import org.apache.lucene.analysis.Analyzer;  
  9. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  10. import org.apache.lucene.document.Document;  
  11. import org.apache.lucene.document.Field;  
  12. import org.apache.lucene.index.IndexWriter;  
  13. import org.apache.lucene.index.Term;  
  14. import org.apache.lucene.search.Hits;  
  15. import org.apache.lucene.search.IndexSearcher;  
  16. import org.apache.lucene.search.TermQuery;  
  17. import org.apache.lucene.store.FSDirectory;  
  18.   
  19. public class TxtFileIndexer ...{  
  20.   
  21.     public String test() ...{  
  22.         return "test is ok hohoho";  
  23.     }  
  24.   
  25.     /**//** 
  26.      * @param args 
  27.      */  
  28.     public String createIndex(String indexDir_path,String dataDir_path) throws Exception ...{  
  29.         String result = "";  
  30.         File indexDir = new File(indexDir_path);  
  31.         File dataDir = new File(dataDir_path);  
  32.         Analyzer luceneAnalyzer = new StandardAnalyzer();  
  33.         File[] dataFiles = dataDir.listFiles();  
  34.         IndexWriter indexWriter = new IndexWriter(indexDir,luceneAnalyzer,true);  
  35.         long startTime = new Date().getTime();  
  36.         for(int i=0; i < dataFiles.length; i++) ...{  
  37.             if(dataFiles[i].isFile() && dataFiles[i].getName().endsWith(".html")) ...{  
  38.                 result += "Indexing file" + dataFiles[i].getCanonicalPath()+"<br />";  
  39.                 Document document = new Document();  
  40.                 Reader txtReader = new FileReader(dataFiles[i]);  
  41.                 document.add(Field.Text("path",dataFiles[i].getCanonicalPath()));  
  42.                 document.add(Field.Text("contents",txtReader));  
  43.                 indexWriter.addDocument(document);  
  44.             }  
  45.         }  
  46.   
  47.         indexWriter.optimize();  
  48.         indexWriter.close();  
  49.         long endTime = new Date().getTime();  
  50.   
  51.         result += "It takes"+(endTime-startTime)  
  52.                 + " milliseconds to create index for the files in directory "  
  53.                 + dataDir.getPath();  
  54.         return result;  
  55.     }  
  56.   
  57.     public String searchword(String ss,String index_path)  throws Exception  ...{  
  58.         String queryStr = ss;  
  59.         String result = "Result:<br />";  
  60.         //This is the directory that hosts the Lucene index  
  61.         File indexDir = new File(index_path);  
  62.         FSDirectory directory = FSDirectory.getDirectory(indexDir,false);  
  63.         IndexSearcher searcher = new IndexSearcher(directory);  
  64.         if(!indexDir.exists())...{  
  65.             result = "The Lucene index is not exist";  
  66.             return result;  
  67.         }  
  68.         Term term = new Term("contents",queryStr.toLowerCase());  
  69.         TermQuery luceneQuery = new TermQuery(term);  
  70.         Hits hits = searcher.search(luceneQuery);  
  71.         for(int i = 0; i < hits.length(); i++)...{  
  72.             Document document = hits.doc(i);  
  73.             result += "<br /><a href='getfile.php?w="+ss+"&f="+document.get("path")+"'>File: " + document.get("path")+"</a>\n";  
  74.         }  
  75.         return result;  
  76.     }  
  77.   
  78. }  


先创建一个我们写的TxtFileIndexer类的实例,

[php]  view plain copy
  1. $tf = new Java('TestLucene.TxtFileIndexer');   

然后就按正常PHP类的调用方法的方式进行调用,首先创建索引:

[php]  view plain copy
  1. $data_path = "F:/test/php_lucene/htdocs/data/manual"//定义被索引内容的目录   
  2. $index_path = "F:/test/php_lucene/htdocs/data/search"//定义生成的索引文件存放目录   
  3. $s = $tf->createIndex($index_path,$data_path); //调用Java类的方法   
  4. print $s//打印返回的结果   


 

[php]  view plain copy
  1. $index_path = "F:/test/php_lucene/htdocs/data/search"//定义生成的索引文件存放目录   
[php]  view plain copy
  1. $s = $tf->searchword("here is keyword for search",$index_path);  
[php]  view plain copy
  1. print $s;   
[php]  view plain copy
  1. <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 plain copy
  1. <span style="color:#008000;">测试代码</span>  
  2. <pre class="php" name="code"><?php  
  3.   
  4.     error_reporting(0);  
  5.   
  6.     java_require("F:/test/php_lucene/htdocs/lib/");  
  7.   
  8.     $tf = new Java('TestLucene.TxtFileIndexer');  
  9.     $s = $tf->test();  
  10.     print "TestLucene.TxtFileIndexer->test()<br />".$s;  
  11.     echo "<hr />";  
  12.   
  13.     $data_path = "F:/test/php_lucene/htdocs/data/manual";  
  14.     $index_path = "F:/test/php_lucene/htdocs/data/search";  
  15.   
  16.     if(<pre class="php" name="code">{1}</pre><br>  
  17. 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>  
  18. GET["w"] != "") ...{ $s = $tf->searchword(<pre class="php" name="code">{1}</pre><br>  
  19. GET["w"],$index_path); print $s; } }?>  
  20. <pre></pre>  
  21. <br>  
  22. <pre></pre>  
  23. 转载:<a href="http://www.lucene.com.cn/php.htm">http://www.lucene.com.cn/php.htm</a><br>  
  24.      
  25. </pre>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值