`
zhengdl126
  • 浏览: 2530843 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类

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的内部原理,不知道如何改写了。明天继续。。。

 

 

 

 

分享到:
评论

相关推荐

    基于lucene5.5写的电脑文件搜索工具(超好用)

    2.本工具是在windows平台下的文件搜索工具,可以按照指定类型(word,excel,txt,java .. php等文档文本类型)内容进行搜索,其他类型均可按照文件路径,名称,类型搜索。 3.支持文件路径或内容包含标点符号特殊...

    多讯php中文分词扩展v0.1

    专业提供中文分词PHP扩展和中文词库。... 若服务器为Windows系统,复制PHP扩展包中php_duoxun5.dll到php安装 目录下ext文件夹中(默认为此位置,具体以php.ini中extension_dir定义位 置为准) 2。 若服务器为L

    PHP调用Java类库

    在64位Windows 7操作系统上,我们需要下载XAMPP的对应版本并进行安装。安装完成后,可以通过XAMPP控制面板启动和停止Apache、MySQL等服务。 - 为了验证PHP环境是否配置成功,可以通过编写一个简单的`helloworld....

    elasticsearch-8.2.3 windows 版本

    elasticsearch-8.2.3 windows 版本。 Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的...

    php4.3 mongo memcache solr redis.dll扩展包以及配置方法

    在Windows上,你需要将这些DLL文件放入PHP的"ext"目录,然后在php.ini文件中启用它们,例如,添加"extension=php_mongo.dll"。 MongoDB是一个流行的NoSQL数据库,特别适合处理大量非结构化数据。在PHP 4.3中,安装...

    elasticsearch-7.3.0-windows-x86_64.zip

    ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级...

    elasticsearch-7.1.1-windows-x86_64.zip

    Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级...

    多讯中文分词php扩展 v0.1.zip

    2. "php_duoxun5.dll"和"duoxun5.so":这两个文件分别是Windows和Linux环境下PHP扩展的动态链接库,它们实现了PHP与分词引擎的接口,使得PHP代码可以直接调用分词功能。 3. "安装使用.url":这是一个链接文件,通常...

    elasticsearch-7.11.2-windows-x86_64.zip

    Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级...

    windowsjdk和es.zip

    Elasticsearch是一款开源的全文搜索引擎,基于Lucene库构建,被广泛用于大数据分析和实时搜索场景。这里的elasticsearch-7.12.1-windows-x86_64.zip是Elasticsearch 7.12.1的Windows 64位版本。安装或解压这个文件后...

    elasticsearch-8.3.2-windows-x86_64安装包

    Elasticsearch用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET...根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。

    elasticsearch-7.3.0.zip

    Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级...

    elasticsearch 工具包

    在IT行业中,Elasticsearch(简称ES)是一个广泛使用的全文搜索引擎,基于Lucene构建,具有分布式、RESTful接口、实时性等特性。本压缩包工具集合是为安装和配置ES环境而准备的,包含了必要的组件和依赖。让我们逐一...

    参考简历模板三.doc

    - **搜索引擎框架**:熟练使用Lucene创建全文搜索引擎,并熟悉Solr、ElasticSearch。 - **网络爬虫技术**:熟练掌握Heritrix,并能运用Jsoup框架解析HTML文档。 - **C/S程序开发**:能使用JBuilder。 - **数据库...

    计算机专业毕业设计参考题目.pdf

    10. **Web服务与API**:部分设计题目可能涉及Web服务的开发,如“基于Web的招投标系统”或“基于Ajax+Lucene构建搜索引擎”,需要掌握RESTful API设计、XML或JSON数据交换格式。 11. **移动应用开发**:“基于...

    最新ET类计算机专业毕业设计题目.docx

    8. **图形界面设计**:如电子邮件客户端软件,需要掌握Windows Forms或WPF等桌面应用开发技术。 9. **数据库操作**:图书销售管理系统、学籍管理系统,会涉及到数据库设计(ER图)、SQL查询优化和事务处理。 10. *...

    firtex2:高性能搜索平台

    在一个请求中进行多集群搜索各种API HTTP RESTful API 用于多语言的RPC客户端( PHP / Python等) 本机C ++ API 内置中文支持语义词段单字段支持UTF -8 / GBK 跨多平台支持Windows / Ubuntu / RedHat / MAC OS… ...

    史上最好传智播客就业班.net培训教程60G 不下会后悔

    微软推出的Windows Phone平台是微软在移动互联网时代的一个重量级产品,微软对于WindowsPhone7的推广力度非常大,因此很多公司也开始进行Windows Phone7产品的研发,2011年下半年Windows Phone7开发人员的需求将会...

    useful-sites:一名 phper 的网站收藏

    目录GitAndroidMemcacheRedisZshLinux发行版官方网站Linux相关文章DockerVagrantVPN搜索ElasticSearch - ElasticSearch是一个基于Lucene的搜索服务器Solr - Solr是一个独立的企业级搜索应用服务器,它对外提供类似于...

Global site tag (gtag.js) - Google Analytics